ESPHome 2025.6.3
Loading...
Searching...
No Matches
text_sensor.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <vector>
9#include <memory>
10
11namespace esphome {
12namespace text_sensor {
13
14#define LOG_TEXT_SENSOR(prefix, type, obj) \
15 if ((obj) != nullptr) { \
16 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
17 if (!(obj)->get_device_class().empty()) { \
18 ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
19 } \
20 if (!(obj)->get_icon().empty()) { \
21 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
22 } \
23 if (!(obj)->unique_id().empty()) { \
24 ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, (obj)->unique_id().c_str()); \
25 } \
26 }
27
28#define SUB_TEXT_SENSOR(name) \
29 protected: \
30 text_sensor::TextSensor *name##_text_sensor_{nullptr}; \
31\
32 public: \
33 void set_##name##_text_sensor(text_sensor::TextSensor *text_sensor) { this->name##_text_sensor_ = text_sensor; }
34
36 public:
37 TextSensor() = default;
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(const std::vector<Filter *> &filters);
51
53 void set_filters(const std::vector<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 std::string state;
63 std::string raw_state;
64
65 // ========== INTERNAL METHODS ==========
66 // (In most use cases you won't need these)
71 virtual std::string unique_id();
72
73 void internal_send_state_to_frontend(const std::string &state);
74
75 protected:
76 std::unique_ptr<CallbackManager<void(std::string)>>
78 CallbackManager<void(std::string)> callback_;
79
80 Filter *filter_list_{nullptr};
81};
82
83} // namespace text_sensor
84} // namespace esphome
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.
CallbackManager< void(std::string)> callback_
Storage for filtered state callbacks.
Definition text_sensor.h:78
std::unique_ptr< CallbackManager< void(std::string)> > raw_callback_
Storage for raw state callbacks (lazy allocated).
Definition text_sensor.h:77
void clear_filters()
Clear the entire filter chain.
void add_filters(const std::vector< Filter * > &filters)
Add a list of vectors to the back of the filter chain.
std::string get_state() const
Getter-syntax for .state.
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Filter * filter_list_
Store all active filters.
Definition text_sensor.h:80
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 set_filters(const std::vector< Filter * > &filters)
Clear the filters and replace them by filters.
void add_on_state_callback(std::function< void(std::string)> callback)
std::string get_raw_state() const
Getter-syntax for .raw_state.
void publish_state(const std::string &state)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7