ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
text_sensor.h
Go to the documentation of this file.
1#pragma once
2
7#ifdef USE_TEXT_SENSOR_FILTER
9#endif
10
11#include <initializer_list>
12#include <memory>
13
14namespace esphome::text_sensor {
15
16class TextSensor;
17
18void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj);
19
20#define LOG_TEXT_SENSOR(prefix, type, obj) log_text_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
21
22#define SUB_TEXT_SENSOR(name) \
23 protected: \
24 text_sensor::TextSensor *name##_text_sensor_{nullptr}; \
25\
26 public: \
27 void set_##name##_text_sensor(text_sensor::TextSensor *text_sensor) { this->name##_text_sensor_ = text_sensor; }
28
29class TextSensor : public EntityBase {
30 public:
31 std::string state;
32
33 // User provided, not "= default": `new(p) TextSensor()` would zero-fill .bss that is already zero.
35 ~TextSensor() = default;
36
38 const std::string &get_state() const;
40 const std::string &get_raw_state() const;
41
42 void publish_state(const std::string &state) { this->publish_state(state.data(), state.size()); }
43 void publish_state(const char *state) { this->publish_state(state, strlen(state)); }
44 void publish_state(const char *state, size_t len);
45#ifdef USE_ESP8266
49#endif
50
51#ifdef USE_TEXT_SENSOR_FILTER
53 void add_filter(Filter *filter);
54
56 void add_filters(std::initializer_list<Filter *> filters);
57
59 void set_filters(std::initializer_list<Filter *> filters);
60
62 void clear_filters();
63#endif
64
65 template<typename F> void add_on_state_callback(F &&callback) { this->callback_.add(std::forward<F>(callback)); }
69 template<typename F> void add_on_raw_state_callback(F &&callback) {
70#ifdef USE_TEXT_SENSOR_FILTER
71 this->raw_callback_.add(std::forward<F>(callback));
72#else
73 this->callback_.add(std::forward<F>(callback));
74#endif
75 }
76
77 // ========== INTERNAL METHODS ==========
78 // (In most use cases you won't need these)
79
80 void internal_send_state_to_frontend(const std::string &state) {
81 this->internal_send_state_to_frontend(state.data(), state.size());
82 }
83 void internal_send_state_to_frontend(const char *state, size_t len);
84
85 protected:
87 void notify_frontend_();
88#ifdef USE_TEXT_SENSOR_FILTER
89 std::string raw_state_;
90 LazyCallbackManager<void(const std::string &)> raw_callback_;
91#endif
92 LazyCallbackManager<void(const std::string &)> callback_;
93
94#ifdef USE_TEXT_SENSOR_FILTER
95 Filter *filter_list_{nullptr};
96#endif
97};
98
99} // namespace esphome::text_sensor
Apply a filter to text sensor values such as to_upper.
Definition filter.h:18
void internal_send_state_to_frontend(const std::string &state)
Definition text_sensor.h:80
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:95
void add_on_state_callback(F &&callback)
Definition text_sensor.h:65
LazyCallbackManager< void(const std::string &)> raw_callback_
Storage for raw state callbacks.
Definition text_sensor.h:90
LazyCallbackManager< void(const std::string &)> callback_
Storage for filtered state callbacks.
Definition text_sensor.h:92
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:69
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:89
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:43
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:42
uint8_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:68
const __FlashStringHelper * ProgmemStr
Definition progmem.h:31