ESPHome 2025.12.0
Loading...
Searching...
No Matches
text_sensor.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <initializer_list>
9#include <memory>
10
11namespace esphome {
12namespace text_sensor {
13
14void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj);
15
16#define LOG_TEXT_SENSOR(prefix, type, obj) log_text_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
17
18#define SUB_TEXT_SENSOR(name) \
19 protected: \
20 text_sensor::TextSensor *name##_text_sensor_{nullptr}; \
21\
22 public: \
23 void set_##name##_text_sensor(text_sensor::TextSensor *text_sensor) { this->name##_text_sensor_ = text_sensor; }
24
26 public:
27 std::string state;
28
29#pragma GCC diagnostic push
30#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32 ESPDEPRECATED("Use get_raw_state() instead of .raw_state. Will be removed in 2026.6.0", "2025.12.0")
33 std::string raw_state;
34
35 TextSensor() = default;
36 ~TextSensor() = default;
37#pragma GCC diagnostic pop
38
40 std::string get_state() const;
42 std::string get_raw_state() const;
43
44 void publish_state(const std::string &state);
45
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
58 void add_on_state_callback(std::function<void(std::string)> callback);
60 void add_on_raw_state_callback(std::function<void(std::string)> callback);
61
62 // ========== INTERNAL METHODS ==========
63 // (In most use cases you won't need these)
64
65 void internal_send_state_to_frontend(const std::string &state);
66
67 protected:
68 std::unique_ptr<CallbackManager<void(std::string)>>
70 CallbackManager<void(std::string)> callback_;
71
72 Filter *filter_list_{nullptr};
73};
74
75} // namespace text_sensor
76} // namespace esphome
Apply a filter to text sensor values such as to_upper.
Definition filter.h:16
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.
CallbackManager< void(std::string)> callback_
Storage for filtered state callbacks.
Definition text_sensor.h:70
std::unique_ptr< CallbackManager< void(std::string)> > raw_callback_
Storage for raw state callbacks (lazy allocated).
Definition text_sensor.h:69
void clear_filters()
Clear the entire filter chain.
void set_filters(std::initializer_list< Filter * > filters)
Clear the filters and replace them by filters.
std::string get_state() const
Getter-syntax for .state.
Filter * filter_list_
Store all active filters.
Definition text_sensor.h:72
void add_on_raw_state_callback(std::function< void(std::string)> callback)
Add a callback that will be called every time the sensor sends a raw value.
void add_on_state_callback(std::function< void(std::string)> callback)
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
std::string get_raw_state() const
Getter-syntax for .raw_state.
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
struct ESPDEPRECATED("Use std::index_sequence instead. Removed in 2026.6.0", "2025.12.0") seq
Definition automation.h:24