ESPHome  2023.3.1
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 
10 TextSensor::TextSensor(const std::string &name) : EntityBase(name) {}
11 
12 void TextSensor::publish_state(const std::string &state) {
13  this->raw_state = state;
14  this->raw_callback_.call(state);
15 
16  ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), state.c_str());
17 
18  if (this->filter_list_ == nullptr) {
20  } else {
21  this->filter_list_->input(state);
22  }
23 }
24 
26  // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
27  // filters
28  ESP_LOGVV(TAG, "TextSensor(%p)::add_filter(%p)", this, filter);
29  if (this->filter_list_ == nullptr) {
30  this->filter_list_ = filter;
31  } else {
32  Filter *last_filter = this->filter_list_;
33  while (last_filter->next_ != nullptr)
34  last_filter = last_filter->next_;
35  last_filter->initialize(this, filter);
36  }
37  filter->initialize(this, nullptr);
38 }
39 void TextSensor::add_filters(const std::vector<Filter *> &filters) {
40  for (Filter *filter : filters) {
41  this->add_filter(filter);
42  }
43 }
44 void TextSensor::set_filters(const std::vector<Filter *> &filters) {
45  this->clear_filters();
46  this->add_filters(filters);
47 }
49  if (this->filter_list_ != nullptr) {
50  ESP_LOGVV(TAG, "TextSensor(%p)::clear_filters()", this);
51  }
52  this->filter_list_ = nullptr;
53 }
54 
55 void TextSensor::add_on_state_callback(std::function<void(std::string)> callback) {
56  this->callback_.add(std::move(callback));
57 }
58 void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
59  this->raw_callback_.add(std::move(callback));
60 }
61 
62 std::string TextSensor::get_state() const { return this->state; }
63 std::string TextSensor::get_raw_state() const { return this->raw_state; }
65  this->state = state;
66  this->has_state_ = true;
67  ESP_LOGD(TAG, "'%s': Sending state '%s'", this->name_.c_str(), state.c_str());
68  this->callback_.call(state);
69 }
70 
71 std::string TextSensor::unique_id() { return ""; }
72 bool TextSensor::has_state() { return this->has_state_; }
73 
74 } // namespace text_sensor
75 } // namespace esphome
const char * name
Definition: stm32flash.h:78
void add_on_state_callback(std::function< void(std::string)> callback)
Definition: text_sensor.cpp:55
virtual std::string unique_id()
Definition: text_sensor.cpp:71
void input(const std::string &value)
Definition: filter.cpp:12
std::string name_
Definition: entity_base.h:54
void add_filters(const std::vector< Filter *> &filters)
Add a list of vectors to the back of the filter chain.
Definition: text_sensor.cpp:39
void publish_state(const std::string &state)
Definition: text_sensor.cpp:12
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:58
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition: text_sensor.cpp:25
CallbackManager< void(std::string)> callback_
Storage for filtered state callbacks.
Definition: text_sensor.h:72
void internal_send_state_to_frontend(const std::string &state)
Definition: text_sensor.cpp:64
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:62
void set_filters(const std::vector< Filter *> &filters)
Clear the filters and replace them by filters.
Definition: text_sensor.cpp:44
Filter * filter_list_
Store all active filters.
Definition: text_sensor.h:74
Definition: a4988.cpp:4
void clear_filters()
Clear the entire filter chain.
Definition: text_sensor.cpp:48
Apply a filter to text sensor values such as to_upper.
Definition: filter.h:20
std::string get_raw_state() const
Getter-syntax for .raw_state.
Definition: text_sensor.cpp:63
CallbackManager< void(std::string)> raw_callback_
Storage for raw state callbacks.
Definition: text_sensor.h:71