ESPHome 2026.12.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
9ESPHOME_LOG_TAG(TAG, "text_sensor");
10
11void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj) {
12 if (!log_entity_header(tag, prefix, type, obj)) {
13 return;
14 }
15 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
16 LOG_ENTITY_ICON(tag, prefix, *obj);
17}
18
19void TextSensor::publish_state(const char *state, size_t len) {
20#ifdef USE_TEXT_SENSOR_FILTER
21 if (this->filter_list_ == nullptr) {
22#endif
23 // No filters: raw_state == state, store once and use for both callbacks
24 // Only assign if changed to avoid heap allocation
25 if (len != this->state.size() || memcmp(state, this->state.data(), len) != 0) {
26 this->state.assign(state, len);
27 }
28#ifdef USE_TEXT_SENSOR_FILTER
29 this->raw_callback_.call(this->state);
30#endif
31 ESP_LOGV(TAG, "'%s': Received new state %s", LOG_STR_ARG(this->get_log_name()), this->state.c_str());
32 this->notify_frontend_();
33#ifdef USE_TEXT_SENSOR_FILTER
34 } else {
35 // Has filters: need separate raw storage
36 // Only assign if changed to avoid heap allocation
37 if (len != this->raw_state_.size() || memcmp(state, this->raw_state_.data(), len) != 0) {
38 this->raw_state_.assign(state, len);
39 }
40 this->raw_callback_.call(this->raw_state_);
41 ESP_LOGV(TAG, "'%s': Received new state %s", LOG_STR_ARG(this->get_log_name()), this->raw_state_.c_str());
42 this->filter_list_->input(this->raw_state_);
43 }
44#endif
45}
46
47#ifdef USE_ESP8266
49 char buf[256];
50 strncpy_P(buf, reinterpret_cast<PGM_P>(state), sizeof(buf));
51 buf[sizeof(buf) - 1] = '\0';
52 this->publish_state(buf);
53}
54#endif
55
56#ifdef USE_TEXT_SENSOR_FILTER
58 ESP_LOGVV(TAG, "TextSensor(%p)::add_filter(%p)", this, filter);
59 // Link the new filter before the chain can reach it
60 filter->initialize(this, nullptr);
61 if (this->filter_list_ == nullptr) {
62 this->filter_list_ = filter;
63 } else {
64 this->filter_list_->chain_last_()->initialize(this, filter);
65 }
66}
67void TextSensor::add_filters(std::initializer_list<Filter *> filters) {
68 for (Filter *filter : filters) {
69 this->add_filter(filter);
70 }
71}
72void TextSensor::set_filters(std::initializer_list<Filter *> filters) {
73 this->clear_filters();
74 this->add_filters(filters);
75}
77 if (this->filter_list_ != nullptr) {
78 ESP_LOGVV(TAG, "TextSensor(%p)::clear_filters()", this);
79 }
80 this->filter_list_ = nullptr;
81}
82#ifdef USE_TEXT_SENSOR_FILTER_RESET
84 Filter *filters = this->filter_list_;
85 if (filters == nullptr)
86 return nullptr;
87 // Reset first, so a timeout armed by an earlier value does not send it while detached
88 filters->reset_chain();
89 this->filter_list_ = nullptr;
90 if (this->raw_state_ != this->state)
92 return filters;
93}
95 if (filters == nullptr)
96 return;
97 this->filter_list_ = filters;
98 // Values published while detached skipped the filters, which may change the current one (a map, for example)
99 if (this->has_state()) {
100 this->raw_state_ = this->state;
101 filters->input(this->raw_state_);
102 }
103}
104#endif
105#endif // USE_TEXT_SENSOR_FILTER
106
107const std::string &TextSensor::get_state() const { return this->state; }
108const std::string &TextSensor::get_raw_state() const {
109#ifdef USE_TEXT_SENSOR_FILTER
110 if (this->filter_list_ != nullptr) {
111 return this->raw_state_;
112 }
113#endif
114 return this->state; // No filters, raw == filtered
115}
117 // Only assign if changed to avoid heap allocation
118 if (len != this->state.size() || memcmp(state, this->state.data(), len) != 0) {
119 this->state.assign(state, len);
120 }
121 this->notify_frontend_();
122}
123
125 this->set_has_state(true);
126 ESP_LOGV(TAG, "'%s' >> '%s'", LOG_STR_ARG(this->get_log_name()), this->state.c_str());
127 this->callback_.call(this->state);
128#if defined(USE_TEXT_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
129 ControllerRegistry::notify_text_sensor_update(this);
130#endif
131}
132
133} // namespace esphome::text_sensor
Node * chain_last_()
Only for a chain that ends in its owner, never in nullptr.
Definition chain.h:61
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
void set_has_state(bool state)
bool has_state() const
Apply a filter to text sensor values such as to_upper.
Definition filter.h:19
void input(std::string value)
Definition filter.cpp:15
virtual void initialize(TextSensor *parent, Filter *next)
Put this filter in its state before the first value.
Definition filter.cpp:42
void reset_chain()
Re-initialize this filter and every filter after it, so they behave as at boot.
Definition filter.cpp:33
void internal_send_state_to_frontend(const std::string &state)
Definition text_sensor.h:91
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
Returns the raw (pre-filter) state.
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.
LazyCallbackManager< void(const std::string &)> raw_callback_
Storage for raw state callbacks.
LazyCallbackManager< void(const std::string &)> callback_
Storage for filtered state callbacks.
const std::string & get_state() const
Getter-syntax for .state.
std::string raw_state_
Backing storage for the raw (pre-filter) value. Only used when a filter is attached.
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
Filter * detach_filters()
Reset the filter chain and take it out of use, so values publish unfiltered.
void notify_frontend_()
Notify frontend that state has changed (assumes this->state is already set)
void attach_filters(Filter *filters)
Put back a chain from detach_filters() and run the current state through it, without calling the raw ...
void publish_state(const std::string &state)
Definition text_sensor.h:42
uint8_t type
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "text_sensor.filter")
void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj)
bool log_entity_header(const char *tag, const char *prefix, const char *type, const EntityBase *obj)
Log "<prefix><type> '<name>'" for a dump_config header; returns false when obj is null.
const void size_t len
Definition hal.h:68
void const char * tag
Definition log.h:110
const __FlashStringHelper * ProgmemStr
Definition progmem.h:33