ESPHome 2026.3.0-dev
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#include <cstring>
6
7namespace esphome::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 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
18 LOG_ENTITY_ICON(tag, prefix, *obj);
19}
20
21void TextSensor::publish_state(const std::string &state) { this->publish_state(state.data(), state.size()); }
22
23void TextSensor::publish_state(const char *state) { this->publish_state(state, strlen(state)); }
24
25void TextSensor::publish_state(const char *state, size_t len) {
26#ifdef USE_TEXT_SENSOR_FILTER
27 if (this->filter_list_ == nullptr) {
28#endif
29 // No filters: raw_state == state, store once and use for both callbacks
30 // Only assign if changed to avoid heap allocation
31 if (len != this->state.size() || memcmp(state, this->state.data(), len) != 0) {
32 this->state.assign(state, len);
33 }
34 this->raw_callback_.call(this->state);
35 ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), this->state.c_str());
36 this->notify_frontend_();
37#ifdef USE_TEXT_SENSOR_FILTER
38 } else {
39 // Has filters: need separate raw storage
40#pragma GCC diagnostic push
41#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
42 // Only assign if changed to avoid heap allocation
43 if (len != this->raw_state.size() || memcmp(state, this->raw_state.data(), len) != 0) {
44 this->raw_state.assign(state, len);
45 }
46 this->raw_callback_.call(this->raw_state);
47 ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), this->raw_state.c_str());
48 this->filter_list_->input(this->raw_state);
49#pragma GCC diagnostic pop
50 }
51#endif
52}
53
54#ifdef USE_TEXT_SENSOR_FILTER
56 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
57 // filters
58 ESP_LOGVV(TAG, "TextSensor(%p)::add_filter(%p)", this, filter);
59 if (this->filter_list_ == nullptr) {
60 this->filter_list_ = filter;
61 } else {
62 Filter *last_filter = this->filter_list_;
63 while (last_filter->next_ != nullptr)
64 last_filter = last_filter->next_;
65 last_filter->initialize(this, filter);
66 }
67 filter->initialize(this, nullptr);
68}
69void TextSensor::add_filters(std::initializer_list<Filter *> filters) {
70 for (Filter *filter : filters) {
71 this->add_filter(filter);
72 }
73}
74void TextSensor::set_filters(std::initializer_list<Filter *> filters) {
75 this->clear_filters();
76 this->add_filters(filters);
77}
79 if (this->filter_list_ != nullptr) {
80 ESP_LOGVV(TAG, "TextSensor(%p)::clear_filters()", this);
81 }
82 this->filter_list_ = nullptr;
83}
84#endif // USE_TEXT_SENSOR_FILTER
85
86void TextSensor::add_on_state_callback(std::function<void(const std::string &)> callback) {
87 this->callback_.add(std::move(callback));
88}
89void TextSensor::add_on_raw_state_callback(std::function<void(const std::string &)> callback) {
90 this->raw_callback_.add(std::move(callback));
91}
92
93const std::string &TextSensor::get_state() const { return this->state; }
94const std::string &TextSensor::get_raw_state() const {
95#ifdef USE_TEXT_SENSOR_FILTER
96 if (this->filter_list_ != nullptr) {
97 // Suppress deprecation warning - get_raw_state() is the replacement API
98#pragma GCC diagnostic push
99#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
100 return this->raw_state;
101#pragma GCC diagnostic pop
102 }
103#endif
104 return this->state; // No filters, raw == filtered
105}
107 this->internal_send_state_to_frontend(state.data(), state.size());
108}
109
111 // Only assign if changed to avoid heap allocation
112 if (len != this->state.size() || memcmp(state, this->state.data(), len) != 0) {
113 this->state.assign(state, len);
114 }
115 this->notify_frontend_();
116}
117
119 this->set_has_state(true);
120 ESP_LOGD(TAG, "'%s' >> '%s'", this->name_.c_str(), this->state.c_str());
121 this->callback_.call(this->state);
122#if defined(USE_TEXT_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
124#endif
125}
126
127} // namespace esphome::text_sensor
static void notify_text_sensor_update(text_sensor::TextSensor *obj)
const StringRef & get_name() const
void set_has_state(bool state)
constexpr const char * c_str() const
Definition string_ref.h:73
Apply a filter to text sensor values such as to_upper.
Definition filter.h:18
void input(std::string value)
Definition filter.cpp:14
virtual void initialize(TextSensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition filter.cpp:28
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
bool state
Definition fan.h:2
const char *const TAG
Definition spi.cpp:7
void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj)
std::string size_t len
Definition helpers.h:817