ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
text_sensor.cpp
Go to the documentation of this file.
1#include "text_sensor.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace text_sensor {
8
9static const char *const TAG = "text_sensor";
10
11void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj) {
12 if (obj == nullptr) {
13 return;
14 }
15
16 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
17
18 if (!obj->get_device_class_ref().empty()) {
19 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
20 }
21
22 if (!obj->get_icon_ref().empty()) {
23 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
24 }
25}
26
27void TextSensor::publish_state(const std::string &state) {
28 this->raw_state = state;
29 if (this->raw_callback_) {
30 this->raw_callback_->call(state);
31 }
32
33 ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), state.c_str());
34
35 if (this->filter_list_ == nullptr) {
37 } else {
38 this->filter_list_->input(state);
39 }
40}
41
43 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
44 // filters
45 ESP_LOGVV(TAG, "TextSensor(%p)::add_filter(%p)", this, filter);
46 if (this->filter_list_ == nullptr) {
47 this->filter_list_ = filter;
48 } else {
49 Filter *last_filter = this->filter_list_;
50 while (last_filter->next_ != nullptr)
51 last_filter = last_filter->next_;
52 last_filter->initialize(this, filter);
53 }
54 filter->initialize(this, nullptr);
55}
56void TextSensor::add_filters(std::initializer_list<Filter *> filters) {
57 for (Filter *filter : filters) {
58 this->add_filter(filter);
59 }
60}
61void TextSensor::set_filters(std::initializer_list<Filter *> filters) {
62 this->clear_filters();
63 this->add_filters(filters);
64}
66 if (this->filter_list_ != nullptr) {
67 ESP_LOGVV(TAG, "TextSensor(%p)::clear_filters()", this);
68 }
69 this->filter_list_ = nullptr;
70}
71
72void TextSensor::add_on_state_callback(std::function<void(std::string)> callback) {
73 this->callback_.add(std::move(callback));
74}
75void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
76 if (!this->raw_callback_) {
77 this->raw_callback_ = make_unique<CallbackManager<void(std::string)>>();
78 }
79 this->raw_callback_->add(std::move(callback));
80}
81
82std::string TextSensor::get_state() const { return this->state; }
83std::string TextSensor::get_raw_state() const { return this->raw_state; }
85 this->state = state;
86 this->set_has_state(true);
87 ESP_LOGD(TAG, "'%s': Sending state '%s'", this->name_.c_str(), state.c_str());
88 this->callback_.call(state);
89#if defined(USE_TEXT_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
91#endif
92}
93
94} // namespace text_sensor
95} // namespace esphome
static void notify_text_sensor_update(text_sensor::TextSensor *obj)
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:69
void set_has_state(bool state)
Definition entity_base.h:93
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:16
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 set_filters(std::initializer_list< Filter * > filters)
Clear the filters and replace them by filters.
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 add_on_state_callback(std::function< void(std::string)> callback)
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
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