ESPHome 2025.5.0
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 this->raw_callback_.call(state);
12
13 ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), state.c_str());
14
15 if (this->filter_list_ == nullptr) {
17 } else {
18 this->filter_list_->input(state);
19 }
20}
21
23 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
24 // filters
25 ESP_LOGVV(TAG, "TextSensor(%p)::add_filter(%p)", this, filter);
26 if (this->filter_list_ == nullptr) {
27 this->filter_list_ = filter;
28 } else {
29 Filter *last_filter = this->filter_list_;
30 while (last_filter->next_ != nullptr)
31 last_filter = last_filter->next_;
32 last_filter->initialize(this, filter);
33 }
34 filter->initialize(this, nullptr);
35}
36void TextSensor::add_filters(const std::vector<Filter *> &filters) {
37 for (Filter *filter : filters) {
38 this->add_filter(filter);
39 }
40}
41void TextSensor::set_filters(const std::vector<Filter *> &filters) {
42 this->clear_filters();
43 this->add_filters(filters);
44}
46 if (this->filter_list_ != nullptr) {
47 ESP_LOGVV(TAG, "TextSensor(%p)::clear_filters()", this);
48 }
49 this->filter_list_ = nullptr;
50}
51
52void TextSensor::add_on_state_callback(std::function<void(std::string)> callback) {
53 this->callback_.add(std::move(callback));
54}
55void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
56 this->raw_callback_.add(std::move(callback));
57}
58
59std::string TextSensor::get_state() const { return this->state; }
60std::string TextSensor::get_raw_state() const { return this->raw_state; }
62 this->state = state;
63 this->has_state_ = true;
64 ESP_LOGD(TAG, "'%s': Sending state '%s'", this->name_.c_str(), state.c_str());
65 this->callback_.call(state);
66}
67
68std::string TextSensor::unique_id() { return ""; }
69bool TextSensor::has_state() { return this->has_state_; }
70
71} // namespace text_sensor
72} // namespace esphome
constexpr const char * c_str() const
Definition string_ref.h:68
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:76
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:78
CallbackManager< void(std::string)> raw_callback_
Storage for raw state callbacks.
Definition text_sensor.h:75
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