ESPHome 2026.3.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
29 public:
30 std::string state;
31
32#pragma GCC diagnostic push
33#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
35 ESPDEPRECATED("Use get_raw_state() instead of .raw_state. Will be removed in 2026.6.0", "2025.12.0")
36 std::string raw_state;
37
38 TextSensor() = default;
39 ~TextSensor() = default;
40#pragma GCC diagnostic pop
41
43 const std::string &get_state() const;
45 const std::string &get_raw_state() const;
46
47 void publish_state(const std::string &state);
48 void publish_state(const char *state);
49 void publish_state(const char *state, size_t len);
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 void add_on_state_callback(std::function<void(const std::string &)> callback);
67 void add_on_raw_state_callback(std::function<void(const std::string &)> callback);
68
69 // ========== INTERNAL METHODS ==========
70 // (In most use cases you won't need these)
71
72 void internal_send_state_to_frontend(const std::string &state);
73 void internal_send_state_to_frontend(const char *state, size_t len);
74
75 protected:
77 void notify_frontend_();
78 LazyCallbackManager<void(const std::string &)> raw_callback_;
79 LazyCallbackManager<void(const std::string &)> callback_;
80
81#ifdef USE_TEXT_SENSOR_FILTER
82 Filter *filter_list_{nullptr};
83#endif
84};
85
86} // namespace esphome::text_sensor
ESPDEPRECATED("Use make_entity_preference<T>() instead, or preferences won't be migrated. " "See https://github.com/esphome/backlog/issues/85. Will be removed in 2027.1.0.", "2026.7.0") uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
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)
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
Getter-syntax for .raw_state.
void add_on_state_callback(std::function< void(const std::string &)> callback)
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:82
LazyCallbackManager< void(const std::string &)> raw_callback_
Storage for raw state callbacks.
Definition text_sensor.h:78
LazyCallbackManager< void(const std::string &)> callback_
Storage for filtered state callbacks.
Definition text_sensor.h:79
void add_on_raw_state_callback(std::function< void(const std::string &)> callback)
Add a callback that will be called every time the sensor sends a raw value.
const std::string & get_state() const
Getter-syntax for .state.
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)
std::string size_t len
Definition helpers.h:817