ESPHome 2026.12.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#ifdef USE_TEXT_SENSOR_FILTER_RESET
72 void attach_filters(Filter *filters);
73#endif
74#endif
75
76 template<typename F> void add_on_state_callback(F &&callback) { this->callback_.add(std::forward<F>(callback)); }
80 template<typename F> void add_on_raw_state_callback(F &&callback) {
81#ifdef USE_TEXT_SENSOR_FILTER
82 this->raw_callback_.add(std::forward<F>(callback));
83#else
84 this->callback_.add(std::forward<F>(callback));
85#endif
86 }
87
88 // ========== INTERNAL METHODS ==========
89 // (In most use cases you won't need these)
90
91 void internal_send_state_to_frontend(const std::string &state) {
92 this->internal_send_state_to_frontend(state.data(), state.size());
93 }
94 void internal_send_state_to_frontend(const char *state, size_t len);
95
96 protected:
98 void notify_frontend_();
99#ifdef USE_TEXT_SENSOR_FILTER
100 std::string raw_state_;
101 LazyCallbackManager<void(const std::string &)> raw_callback_;
102#endif
103 LazyCallbackManager<void(const std::string &)> callback_;
104
105#ifdef USE_TEXT_SENSOR_FILTER
107#endif
108};
109
110} // namespace esphome::text_sensor
Apply a filter to text sensor values such as to_upper.
Definition filter.h:19
void internal_send_state_to_frontend(const std::string &state)
Definition text_sensor.h:91
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.
void add_on_state_callback(F &&callback)
Definition text_sensor.h:76
LazyCallbackManager< void(const std::string &)> raw_callback_
Storage for raw state callbacks.
LazyCallbackManager< void(const std::string &)> callback_
Storage for filtered state callbacks.
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:80
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.
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
Filter * detach_filters()
Reset the filter chain and take it out of use, so values publish unfiltered.
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 attach_filters(Filter *filters)
Put back a chain from detach_filters() and run the current state through it, without calling the raw ...
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
void const char * tag
Definition log.h:110
const __FlashStringHelper * ProgmemStr
Definition progmem.h:33