ESPHome 2025.10.0-dev
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 log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj) {
10 if (obj == nullptr) {
11 return;
12 }
13
14 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
15
16 if (!obj->get_device_class_ref().empty()) {
17 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
18 }
19
20 if (!obj->get_icon_ref().empty()) {
21 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
22 }
23}
24
25void TextSensor::publish_state(const std::string &state) {
26 this->raw_state = state;
27 if (this->raw_callback_) {
28 this->raw_callback_->call(state);
29 }
30
31 ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), state.c_str());
32
33 if (this->filter_list_ == nullptr) {
35 } else {
36 this->filter_list_->input(state);
37 }
38}
39
41 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
42 // filters
43 ESP_LOGVV(TAG, "TextSensor(%p)::add_filter(%p)", this, filter);
44 if (this->filter_list_ == nullptr) {
45 this->filter_list_ = filter;
46 } else {
47 Filter *last_filter = this->filter_list_;
48 while (last_filter->next_ != nullptr)
49 last_filter = last_filter->next_;
50 last_filter->initialize(this, filter);
51 }
52 filter->initialize(this, nullptr);
53}
54void TextSensor::add_filters(const std::vector<Filter *> &filters) {
55 for (Filter *filter : filters) {
56 this->add_filter(filter);
57 }
58}
59void TextSensor::set_filters(const std::vector<Filter *> &filters) {
60 this->clear_filters();
61 this->add_filters(filters);
62}
64 if (this->filter_list_ != nullptr) {
65 ESP_LOGVV(TAG, "TextSensor(%p)::clear_filters()", this);
66 }
67 this->filter_list_ = nullptr;
68}
69
70void TextSensor::add_on_state_callback(std::function<void(std::string)> callback) {
71 this->callback_.add(std::move(callback));
72}
73void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
74 if (!this->raw_callback_) {
75 this->raw_callback_ = make_unique<CallbackManager<void(std::string)>>();
76 }
77 this->raw_callback_->add(std::move(callback));
78}
79
80std::string TextSensor::get_state() const { return this->state; }
81std::string TextSensor::get_raw_state() const { return this->raw_state; }
83 this->state = state;
84 this->set_has_state(true);
85 ESP_LOGD(TAG, "'%s': Sending state '%s'", this->name_.c_str(), state.c_str());
86 this->callback_.call(state);
87}
88
89} // namespace text_sensor
90} // namespace esphome
StringRef get_device_class_ref() const
Get the device class as StringRef.
const StringRef & get_name() const
StringRef get_icon_ref() const
Definition entity_base.h:62
void set_has_state(bool state)
Definition entity_base.h:86
constexpr const char * c_str() const
Definition string_ref.h:69
constexpr bool empty() const
Definition string_ref.h:71
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:63
std::unique_ptr< CallbackManager< void(std::string)> > raw_callback_
Storage for raw state callbacks (lazy allocated).
Definition text_sensor.h:62
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.
Filter * filter_list_
Store all active filters.
Definition text_sensor.h:65
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)
uint16_t type
bool state
Definition fan.h:0
const char *const TAG
Definition spi.cpp:8
void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7