ESPHome 2026.2.3
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 {
8namespace text_sensor {
9
10static const char *const TAG = "text_sensor";
11
12void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj) {
13 if (obj == nullptr) {
14 return;
15 }
16
17 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
18 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
19 LOG_ENTITY_ICON(tag, prefix, *obj);
20}
21
22void TextSensor::publish_state(const std::string &state) { this->publish_state(state.data(), state.size()); }
23
24void TextSensor::publish_state(const char *state) { this->publish_state(state, strlen(state)); }
25
26void TextSensor::publish_state(const char *state, size_t len) {
27 if (this->filter_list_ == nullptr) {
28 // No filters: raw_state == state, store once and use for both callbacks
29 // Only assign if changed to avoid heap allocation
30 if (len != this->state.size() || memcmp(state, this->state.data(), len) != 0) {
31 this->state.assign(state, len);
32 }
33 this->raw_callback_.call(this->state);
34 ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), this->state.c_str());
35 this->notify_frontend_();
36 } else {
37 // Has filters: need separate raw storage
38#pragma GCC diagnostic push
39#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40 // Only assign if changed to avoid heap allocation
41 if (len != this->raw_state.size() || memcmp(state, this->raw_state.data(), len) != 0) {
42 this->raw_state.assign(state, len);
43 }
44 this->raw_callback_.call(this->raw_state);
45 ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), this->raw_state.c_str());
46 this->filter_list_->input(this->raw_state);
47#pragma GCC diagnostic pop
48 }
49}
50
52 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
53 // filters
54 ESP_LOGVV(TAG, "TextSensor(%p)::add_filter(%p)", this, filter);
55 if (this->filter_list_ == nullptr) {
56 this->filter_list_ = filter;
57 } else {
58 Filter *last_filter = this->filter_list_;
59 while (last_filter->next_ != nullptr)
60 last_filter = last_filter->next_;
61 last_filter->initialize(this, filter);
62 }
63 filter->initialize(this, nullptr);
64}
65void TextSensor::add_filters(std::initializer_list<Filter *> filters) {
66 for (Filter *filter : filters) {
67 this->add_filter(filter);
68 }
69}
70void TextSensor::set_filters(std::initializer_list<Filter *> filters) {
71 this->clear_filters();
72 this->add_filters(filters);
73}
75 if (this->filter_list_ != nullptr) {
76 ESP_LOGVV(TAG, "TextSensor(%p)::clear_filters()", this);
77 }
78 this->filter_list_ = nullptr;
79}
80
81void TextSensor::add_on_state_callback(std::function<void(const std::string &)> callback) {
82 this->callback_.add(std::move(callback));
83}
84void TextSensor::add_on_raw_state_callback(std::function<void(const std::string &)> callback) {
85 this->raw_callback_.add(std::move(callback));
86}
87
88const std::string &TextSensor::get_state() const { return this->state; }
89const std::string &TextSensor::get_raw_state() const {
90 if (this->filter_list_ == nullptr) {
91 return this->state; // No filters, raw == filtered
92 }
93// Suppress deprecation warning - get_raw_state() is the replacement API
94#pragma GCC diagnostic push
95#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
96 return this->raw_state;
97#pragma GCC diagnostic pop
98}
100 this->internal_send_state_to_frontend(state.data(), state.size());
101}
102
104 // Only assign if changed to avoid heap allocation
105 if (len != this->state.size() || memcmp(state, this->state.data(), len) != 0) {
106 this->state.assign(state, len);
107 }
108 this->notify_frontend_();
109}
110
112 this->set_has_state(true);
113 ESP_LOGD(TAG, "'%s' >> '%s'", this->name_.c_str(), this->state.c_str());
114 this->callback_.call(this->state);
115#if defined(USE_TEXT_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
117#endif
118}
119
120} // namespace text_sensor
121} // namespace esphome
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:16
void input(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:26
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:76
LazyCallbackManager< void(const std::string &)> raw_callback_
Storage for raw state callbacks.
Definition text_sensor.h:73
LazyCallbackManager< void(const std::string &)> callback_
Storage for filtered state callbacks.
Definition text_sensor.h:74
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:692