ESPHome 2025.12.0
Loading...
Searching...
No Matches
text_sensor.cpp
Go to the documentation of this file.
1#include "text_sensor.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace text_sensor {
8
9static const char *const TAG = "text_sensor";
10
11void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj) {
12 if (obj == nullptr) {
13 return;
14 }
15
16 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
17
18 if (!obj->get_device_class_ref().empty()) {
19 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
20 }
21
22 if (!obj->get_icon_ref().empty()) {
23 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
24 }
25}
26
27void TextSensor::publish_state(const std::string &state) {
28// Suppress deprecation warning - we need to populate raw_state for backwards compatibility
29#pragma GCC diagnostic push
30#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
31 this->raw_state = state;
32#pragma GCC diagnostic pop
33 if (this->raw_callback_) {
34 this->raw_callback_->call(state);
35 }
36
37 ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), state.c_str());
38
39 if (this->filter_list_ == nullptr) {
41 } else {
42 this->filter_list_->input(state);
43 }
44}
45
47 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
48 // filters
49 ESP_LOGVV(TAG, "TextSensor(%p)::add_filter(%p)", this, filter);
50 if (this->filter_list_ == nullptr) {
51 this->filter_list_ = filter;
52 } else {
53 Filter *last_filter = this->filter_list_;
54 while (last_filter->next_ != nullptr)
55 last_filter = last_filter->next_;
56 last_filter->initialize(this, filter);
57 }
58 filter->initialize(this, nullptr);
59}
60void TextSensor::add_filters(std::initializer_list<Filter *> filters) {
61 for (Filter *filter : filters) {
62 this->add_filter(filter);
63 }
64}
65void TextSensor::set_filters(std::initializer_list<Filter *> filters) {
66 this->clear_filters();
67 this->add_filters(filters);
68}
70 if (this->filter_list_ != nullptr) {
71 ESP_LOGVV(TAG, "TextSensor(%p)::clear_filters()", this);
72 }
73 this->filter_list_ = nullptr;
74}
75
76void TextSensor::add_on_state_callback(std::function<void(std::string)> callback) {
77 this->callback_.add(std::move(callback));
78}
79void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
80 if (!this->raw_callback_) {
81 this->raw_callback_ = make_unique<CallbackManager<void(std::string)>>();
82 }
83 this->raw_callback_->add(std::move(callback));
84}
85
86std::string TextSensor::get_state() const { return this->state; }
87std::string TextSensor::get_raw_state() const {
88// Suppress deprecation warning - get_raw_state() is the replacement API
89#pragma GCC diagnostic push
90#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
91 return this->raw_state;
92#pragma GCC diagnostic pop
93}
95 this->state = state;
96 this->set_has_state(true);
97 ESP_LOGD(TAG, "'%s': Sending state '%s'", this->name_.c_str(), state.c_str());
98 this->callback_.call(state);
99#if defined(USE_TEXT_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
101#endif
102}
103
104} // namespace text_sensor
105} // namespace esphome
static void notify_text_sensor_update(text_sensor::TextSensor *obj)
StringRef get_device_class_ref() const
Get the device class as StringRef.
const StringRef & get_name() const
StringRef get_icon_ref() const
Definition entity_base.h:72
void set_has_state(bool state)
Definition entity_base.h:96
constexpr const char * c_str() const
Definition string_ref.h:69
constexpr bool empty() const
Definition string_ref.h:71
Apply a filter to text sensor values such as to_upper.
Definition filter.h:16
void input(const std::string &value)
Definition filter.cpp:12
virtual void initialize(TextSensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition filter.cpp:27
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
bool state
Definition fan.h:0
const char *const TAG
Definition spi.cpp:8
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