ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
filter.cpp
Go to the documentation of this file.
2#ifdef USE_TEXT_SENSOR_FILTER
3
4#include "filter.h"
5#include "text_sensor.h"
7#include "esphome/core/log.h"
8#include "esphome/core/hal.h"
9
11
12ESPHOME_LOG_TAG(TAG, "text_sensor.filter");
13
14// Filter
15void Filter::input(std::string value) {
16 ESP_LOGVV(TAG, "Filter(%p)::input(%s)", this, value.c_str());
17 if (this->new_value(value))
18 this->output(value);
19}
20void Filter::output(std::string &value) {
21 if (this->next_ == nullptr) {
22 ESP_LOGVV(TAG, "Filter(%p)::output(%s) -> SENSOR", this, value.c_str());
24 } else {
25 ESP_LOGVV(TAG, "Filter(%p)::output(%s) -> %p", this, value.c_str(), this->next_);
26 this->next_->input(std::move(value));
27 }
28}
30 ESP_LOGVV(TAG, "Filter(%p)::initialize(parent=%p next=%p)", this, parent, next);
31 this->parent_ = parent;
32 this->next_ = next;
33}
34
35// LambdaFilter
36LambdaFilter::LambdaFilter(lambda_filter_t lambda_filter) : lambda_filter_(std::move(lambda_filter)) {}
38void LambdaFilter::set_lambda_filter(const lambda_filter_t &lambda_filter) { this->lambda_filter_ = lambda_filter; }
39
40bool LambdaFilter::new_value(std::string &value) {
41 auto result = this->lambda_filter_(value);
42 if (result.has_value()) {
43 ESP_LOGVV(TAG, "LambdaFilter(%p)::new_value(%s) -> %s (continue)", this, value.c_str(), result->c_str());
44 value = std::move(*result);
45 return true;
46 }
47 ESP_LOGVV(TAG, "LambdaFilter(%p)::new_value(%s) -> (stop)", this, value.c_str());
48 return false;
49}
50
51// ToUpperFilter
52bool ToUpperFilter::new_value(std::string &value) {
53 for (char &c : value)
54 c = ::toupper(c);
55 return true;
56}
57
58// ToLowerFilter
59bool ToLowerFilter::new_value(std::string &value) {
60 for (char &c : value)
61 c = ::tolower(c);
62 return true;
63}
64
65// Append
66bool AppendFilter::new_value(std::string &value) {
67 value.append(this->suffix_);
68 return true;
69}
70
71// Prepend
72bool PrependFilter::new_value(std::string &value) {
73 value.insert(0, this->prefix_);
74 return true;
75}
76
77// Substitute
79 for (const Substitution *s = this->substitutions_; s->from != nullptr; s++) {
80 const size_t from_len = strlen(s->from);
81 const size_t to_len = strlen(s->to);
82 std::size_t pos = 0;
83 while ((pos = value.find(s->from, pos, from_len)) != std::string::npos) {
84 value.replace(pos, from_len, s->to, to_len);
85 pos += to_len;
86 }
87 }
88 return true;
89}
90
91// Map
92bool MapFilter::new_value(std::string &value) {
93 for (const Substitution *m = this->mappings_; m->from != nullptr; m++) {
94 if (value == m->from) {
95 value.assign(m->to);
96 return true;
97 }
98 }
99 return true;
100}
101
102// Throttle
103// True when a value may pass: the period is over, or the value is in the nullptr-ended `prioritized` list
104static bool throttle_passes(uint32_t &last_input, uint32_t period, const std::string &value,
105 const char *const *prioritized) {
107 bool pass = last_input == 0 || now - last_input >= period;
108 for (const char *const *p = prioritized; !pass && p != nullptr && *p != nullptr; p++)
109 pass = value == *p;
110 if (pass)
111 last_input = now;
112 return pass;
113}
114
116 return throttle_passes(this->last_input_, this->min_time_between_inputs_, value, nullptr);
117}
118
120 return throttle_passes(this->last_input_, this->min_time_between_inputs_, value, this->prioritized_);
121}
122
123// Timeout
126 this->enable_loop();
127 return true;
128}
129
131 if (App.get_loop_component_start_time() - this->timeout_start_time_ < this->time_period_)
132 return;
133 // Before sending, so a callback that publishes to this sensor again re-arms the timeout
134 this->disable_loop();
135 this->send_timeout_value();
136}
137
138void TimeoutFilterBase::send_(const char *text, size_t len) {
139 if (this->next_ == nullptr) {
141 } else {
142 this->next_->input(std::string(text, len));
143 }
144}
145
147 this->last_ = value; // reuses last_'s buffer
148 return TimeoutFilterBase::new_value(value);
149}
150
153 return throttle_passes(this->last_input_, this->time_period_, value, this->prioritized_);
154}
155
157 if (throttle_passes(this->last_input_, this->time_period_, this->last_, this->prioritized_))
158 this->send_(this->last_.data(), this->last_.size());
159}
160
161} // namespace esphome::text_sensor
162
163#endif // USE_TEXT_SENSOR_FILTER
uint8_t m
Definition bl0906.h:1
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void enable_loop()
Enable this component's loop.
Definition component.h:261
void disable_loop()
Disable this component's loop.
bool new_value(std::string &value) override
Definition filter.cpp:66
Apply a filter to text sensor values such as to_upper.
Definition filter.h:18
virtual bool new_value(std::string &value)=0
This will be called every time the filter receives a new value.
void output(std::string &value)
Definition filter.cpp:20
void input(std::string value)
Definition filter.cpp:15
virtual void initialize(TextSensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition filter.cpp:29
lambda_filter_t lambda_filter_
Definition filter.h:62
LambdaFilter(lambda_filter_t lambda_filter)
Definition filter.cpp:36
void set_lambda_filter(const lambda_filter_t &lambda_filter)
Definition filter.cpp:38
bool new_value(std::string &value) override
Definition filter.cpp:40
const lambda_filter_t & get_lambda_filter() const
Definition filter.cpp:37
bool new_value(std::string &value) override
Definition filter.cpp:92
const Substitution * mappings_
Definition filter.h:169
bool new_value(std::string &value) override
Definition filter.cpp:72
bool new_value(std::string &value) override
Definition filter.cpp:78
const Substitution * substitutions_
Definition filter.h:133
void internal_send_state_to_frontend(const std::string &state)
Definition text_sensor.h:80
bool new_value(std::string &value) override
Definition filter.cpp:115
bool new_value(std::string &value) override
Definition filter.cpp:119
bool new_value(std::string &value) override
Definition filter.cpp:124
void send_(const char *text, size_t len)
Sends text; never moves a stored string on, so it keeps its buffer.
Definition filter.cpp:138
bool new_value(std::string &value) override
Definition filter.cpp:146
bool new_value(std::string &value) override
Definition filter.cpp:151
bool new_value(std::string &value) override
Definition filter.cpp:59
bool new_value(std::string &value) override
Definition filter.cpp:52
NameCopy * next
std::function< optional< std::string >(std::string)> lambda_filter_t
Definition filter.h:44
ESPHOME_LOG_TAG(TAG, "text_sensor.filter")
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
size_t size_t pos
Definition helpers.h:1333
Application App
Global storage of Application pointer - only one Application can exist.
STL namespace.
static void uint32_t