ESPHome 2026.6.0-dev
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 TextSensor() = default;
33 ~TextSensor() = default;
34
36 const std::string &get_state() const;
38 const std::string &get_raw_state() const;
39
40 void publish_state(const std::string &state);
41 void publish_state(const char *state);
42 void publish_state(const char *state, size_t len);
43
44#ifdef USE_TEXT_SENSOR_FILTER
46 void add_filter(Filter *filter);
47
49 void add_filters(std::initializer_list<Filter *> filters);
50
52 void set_filters(std::initializer_list<Filter *> filters);
53
55 void clear_filters();
56#endif
57
58 template<typename F> void add_on_state_callback(F &&callback) { this->callback_.add(std::forward<F>(callback)); }
62 template<typename F> void add_on_raw_state_callback(F &&callback) {
63#ifdef USE_TEXT_SENSOR_FILTER
64 this->raw_callback_.add(std::forward<F>(callback));
65#else
66 this->callback_.add(std::forward<F>(callback));
67#endif
68 }
69
70 // ========== INTERNAL METHODS ==========
71 // (In most use cases you won't need these)
72
73 void internal_send_state_to_frontend(const std::string &state);
74 void internal_send_state_to_frontend(const char *state, size_t len);
75
76 protected:
78 void notify_frontend_();
79#ifdef USE_TEXT_SENSOR_FILTER
80 std::string raw_state_;
81 LazyCallbackManager<void(const std::string &)> raw_callback_;
82#endif
83 LazyCallbackManager<void(const std::string &)> callback_;
84
85#ifdef USE_TEXT_SENSOR_FILTER
86 Filter *filter_list_{nullptr};
87#endif
88};
89
90} // 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)
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:86
void add_on_state_callback(F &&callback)
Definition text_sensor.h:58
LazyCallbackManager< void(const std::string &)> raw_callback_
Storage for raw state callbacks.
Definition text_sensor.h:81
LazyCallbackManager< void(const std::string &)> callback_
Storage for filtered state callbacks.
Definition text_sensor.h:83
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:62
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:80
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
void notify_frontend_()
Notify frontend that state has changed (assumes this->state is already set)
void publish_state(const std::string &state)
uint16_t type
void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj)
const char * tag
Definition log.h:74
const void size_t len
Definition hal.h:64