ESPHome  2024.11.0
text_sensor.cpp
Go to the documentation of this file.
1 #include "text_sensor.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace text_sensor {
6 
7 static const char *const TAG = "text_sensor";
8 
9 void 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 }
36 void TextSensor::add_filters(const std::vector<Filter *> &filters) {
37  for (Filter *filter : filters) {
38  this->add_filter(filter);
39  }
40 }
41 void 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 
52 void TextSensor::add_on_state_callback(std::function<void(std::string)> callback) {
53  this->callback_.add(std::move(callback));
54 }
55 void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
56  this->raw_callback_.add(std::move(callback));
57 }
58 
59 std::string TextSensor::get_state() const { return this->state; }
60 std::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 
68 std::string TextSensor::unique_id() { return ""; }
69 bool TextSensor::has_state() { return this->has_state_; }
70 
71 } // namespace text_sensor
72 } // namespace esphome
void add_on_state_callback(std::function< void(std::string)> callback)
Definition: text_sensor.cpp:52
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition: text_sensor.cpp:68
void input(const std::string &value)
Definition: filter.cpp:12
void add_filters(const std::vector< Filter *> &filters)
Add a list of vectors to the back of the filter chain.
Definition: text_sensor.cpp:36
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
const char *const TAG
Definition: spi.cpp:8
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.
Definition: text_sensor.cpp:55
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition: text_sensor.cpp:22
CallbackManager< void(std::string)> callback_
Storage for filtered state callbacks.
Definition: text_sensor.h:76
void internal_send_state_to_frontend(const std::string &state)
Definition: text_sensor.cpp:61
virtual void initialize(TextSensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition: filter.cpp:27
std::string get_state() const
Getter-syntax for .state.
Definition: text_sensor.cpp:59
void set_filters(const std::vector< Filter *> &filters)
Clear the filters and replace them by filters.
Definition: text_sensor.cpp:41
constexpr const char * c_str() const
Definition: string_ref.h:68
Filter * filter_list_
Store all active filters.
Definition: text_sensor.h:78
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void clear_filters()
Clear the entire filter chain.
Definition: text_sensor.cpp:45
Apply a filter to text sensor values such as to_upper.
Definition: filter.h:20
bool state
Definition: fan.h:34
std::string get_raw_state() const
Getter-syntax for .raw_state.
Definition: text_sensor.cpp:60
CallbackManager< void(std::string)> raw_callback_
Storage for raw state callbacks.
Definition: text_sensor.h:75