ESPHome 2026.10.2
Loading...
Searching...
No Matches
text_sensor.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_TEXT_SENSOR_FILTER
8#endif
9
10#include <initializer_list>
11#include <memory>
12
13namespace esphome::text_sensor {
14
15class TextSensor;
16
17void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj);
18
19#define LOG_TEXT_SENSOR(prefix, type, obj) log_text_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
20
21#define SUB_TEXT_SENSOR(name) \
22 protected: \
23 text_sensor::TextSensor *name##_text_sensor_{nullptr}; \
24\
25 public: \
26 void set_##name##_text_sensor(text_sensor::TextSensor *text_sensor) { this->name##_text_sensor_ = text_sensor; }
27
28class TextSensor : public EntityBase {
29 public:
30 std::string state;
31
32 // User provided, not "= default": `new(p) TextSensor()` would zero-fill .bss that is already zero.
34 ~TextSensor() = default;
35
37 const std::string &get_state() const;
39 const std::string &get_raw_state() const;
40
41 void publish_state(const std::string &state) { this->publish_state(state.data(), state.size()); }
42 void publish_state(const char *state) { this->publish_state(state, strlen(state)); }
43 void publish_state(const char *state, size_t len);
44
45#ifdef USE_TEXT_SENSOR_FILTER
47 void add_filter(Filter *filter);
48
50 void add_filters(std::initializer_list<Filter *> filters);
51
53 void set_filters(std::initializer_list<Filter *> filters);
54
56 void clear_filters();
57#endif
58
59 template<typename F> void add_on_state_callback(F &&callback) { this->callback_.add(std::forward<F>(callback)); }
63 template<typename F> void add_on_raw_state_callback(F &&callback) {
64#ifdef USE_TEXT_SENSOR_FILTER
65 this->raw_callback_.add(std::forward<F>(callback));
66#else
67 this->callback_.add(std::forward<F>(callback));
68#endif
69 }
70
71 // ========== INTERNAL METHODS ==========
72 // (In most use cases you won't need these)
73
74 void internal_send_state_to_frontend(const std::string &state) {
75 this->internal_send_state_to_frontend(state.data(), state.size());
76 }
77 void internal_send_state_to_frontend(const char *state, size_t len);
78
79 protected:
81 void notify_frontend_();
82#ifdef USE_TEXT_SENSOR_FILTER
83 std::string raw_state_;
84 LazyCallbackManager<void(const std::string &)> raw_callback_;
85#endif
86 LazyCallbackManager<void(const std::string &)> callback_;
87
88#ifdef USE_TEXT_SENSOR_FILTER
89 Filter *filter_list_{nullptr};
90#endif
91};
92
93} // namespace esphome::text_sensor
Apply a filter to text sensor values such as to_upper.
Definition filter.h:20
void internal_send_state_to_frontend(const std::string &state)
Definition text_sensor.h:74
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
const std::string & get_raw_state() const
Returns the raw (pre-filter) state.
void clear_filters()
Clear the entire filter chain.
void set_filters(std::initializer_list< Filter * > filters)
Clear the filters and replace them by filters.
Filter * filter_list_
Store all active filters.
Definition text_sensor.h:89
void add_on_state_callback(F &&callback)
Definition text_sensor.h:59
LazyCallbackManager< void(const std::string &)> raw_callback_
Storage for raw state callbacks.
Definition text_sensor.h:84
LazyCallbackManager< void(const std::string &)> callback_
Storage for filtered state callbacks.
Definition text_sensor.h:86
void add_on_raw_state_callback(F &&callback)
Add a callback that will be called every time the sensor sends a raw value.
Definition text_sensor.h:63
const std::string & get_state() const
Getter-syntax for .state.
std::string raw_state_
Backing storage for the raw (pre-filter) value. Only used when a filter is attached.
Definition text_sensor.h:83
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
void publish_state(const char *state)
Definition text_sensor.h:42
void notify_frontend_()
Notify frontend that state has changed (assumes this->state is already set)
void publish_state(const std::string &state)
Definition text_sensor.h:41
uint16_t type
void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj)
const void size_t len
Definition hal.h:64