ESPHome 2025.6.3
Loading...
Searching...
No Matches
text_sensor.cpp
Go to the documentation of this file.
1#include "text_sensor.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace text_sensor {
6
7static const char *const TAG = "text_sensor";
8
9void TextSensor::publish_state(const std::string &state) {
10 this->raw_state = state;
11 if (this->raw_callback_) {
12 this->raw_callback_->call(state);
13 }
14
15 ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), state.c_str());
16
17 if (this->filter_list_ == nullptr) {
19 } else {
20 this->filter_list_->input(state);
21 }
22}
23
25 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
26 // filters
27 ESP_LOGVV(TAG, "TextSensor(%p)::add_filter(%p)", this, filter);
28 if (this->filter_list_ == nullptr) {
29 this->filter_list_ = filter;
30 } else {
31 Filter *last_filter = this->filter_list_;
32 while (last_filter->next_ != nullptr)
33 last_filter = last_filter->next_;
34 last_filter->initialize(this, filter);
35 }
36 filter->initialize(this, nullptr);
37}
38void TextSensor::add_filters(const std::vector<Filter *> &filters) {
39 for (Filter *filter : filters) {
40 this->add_filter(filter);
41 }
42}
43void TextSensor::set_filters(const std::vector<Filter *> &filters) {
44 this->clear_filters();
45 this->add_filters(filters);
46}
48 if (this->filter_list_ != nullptr) {
49 ESP_LOGVV(TAG, "TextSensor(%p)::clear_filters()", this);
50 }
51 this->filter_list_ = nullptr;
52}
53
54void TextSensor::add_on_state_callback(std::function<void(std::string)> callback) {
55 this->callback_.add(std::move(callback));
56}
57void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
58 if (!this->raw_callback_) {
59 this->raw_callback_ = make_unique<CallbackManager<void(std::string)>>();
60 }
61 this->raw_callback_->add(std::move(callback));
62}
63
64std::string TextSensor::get_state() const { return this->state; }
65std::string TextSensor::get_raw_state() const { return this->raw_state; }
67 this->state = state;
68 this->set_has_state(true);
69 ESP_LOGD(TAG, "'%s': Sending state '%s'", this->name_.c_str(), state.c_str());
70 this->callback_.call(state);
71}
72
73std::string TextSensor::unique_id() { return ""; }
74
75} // namespace text_sensor
76} // namespace esphome
void set_has_state(bool state)
Definition entity_base.h:56
constexpr const char * c_str() const
Definition string_ref.h:69
Apply a filter to text sensor values such as to_upper.
Definition filter.h:20
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: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)
bool state
Definition fan.h:0
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7