ESPHome 2026.11.0-dev
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"
6#include "esphome/core/log.h"
7#include "esphome/core/hal.h"
8
10
11ESPHOME_LOG_TAG(TAG, "text_sensor.filter");
12
13// Filter
14void Filter::input(std::string value) {
15 ESP_LOGVV(TAG, "Filter(%p)::input(%s)", this, value.c_str());
16 if (this->new_value(value))
17 this->output(value);
18}
19void Filter::output(std::string &value) {
20 if (this->next_ == nullptr) {
21 ESP_LOGVV(TAG, "Filter(%p)::output(%s) -> SENSOR", this, value.c_str());
23 } else {
24 ESP_LOGVV(TAG, "Filter(%p)::output(%s) -> %p", this, value.c_str(), this->next_);
25 this->next_->input(std::move(value));
26 }
27}
29 ESP_LOGVV(TAG, "Filter(%p)::initialize(parent=%p next=%p)", this, parent, next);
30 this->parent_ = parent;
31 this->next_ = next;
32}
33
34// LambdaFilter
35LambdaFilter::LambdaFilter(lambda_filter_t lambda_filter) : lambda_filter_(std::move(lambda_filter)) {}
37void LambdaFilter::set_lambda_filter(const lambda_filter_t &lambda_filter) { this->lambda_filter_ = lambda_filter; }
38
39bool LambdaFilter::new_value(std::string &value) {
40 auto result = this->lambda_filter_(value);
41 if (result.has_value()) {
42 ESP_LOGVV(TAG, "LambdaFilter(%p)::new_value(%s) -> %s (continue)", this, value.c_str(), result->c_str());
43 value = std::move(*result);
44 return true;
45 }
46 ESP_LOGVV(TAG, "LambdaFilter(%p)::new_value(%s) -> (stop)", this, value.c_str());
47 return false;
48}
49
50// ToUpperFilter
51bool ToUpperFilter::new_value(std::string &value) {
52 for (char &c : value)
53 c = ::toupper(c);
54 return true;
55}
56
57// ToLowerFilter
58bool ToLowerFilter::new_value(std::string &value) {
59 for (char &c : value)
60 c = ::tolower(c);
61 return true;
62}
63
64// Append
65bool AppendFilter::new_value(std::string &value) {
66 value.append(this->suffix_);
67 return true;
68}
69
70// Prepend
71bool PrependFilter::new_value(std::string &value) {
72 value.insert(0, this->prefix_);
73 return true;
74}
75
76// Substitute
77bool SubstituteFilter::new_value(std::string &value) {
78 for (const Substitution *s = this->substitutions_; s->from != nullptr; s++) {
79 const size_t from_len = strlen(s->from);
80 const size_t to_len = strlen(s->to);
81 std::size_t pos = 0;
82 while ((pos = value.find(s->from, pos, from_len)) != std::string::npos) {
83 value.replace(pos, from_len, s->to, to_len);
84 pos += to_len;
85 }
86 }
87 return true;
88}
89
90// Map
91bool MapFilter::new_value(std::string &value) {
92 for (const Substitution *m = this->mappings_; m->from != nullptr; m++) {
93 if (value == m->from) {
94 value.assign(m->to);
95 return true;
96 }
97 }
98 return true;
99}
100
101} // namespace esphome::text_sensor
102
103#endif // USE_TEXT_SENSOR_FILTER
uint8_t m
Definition bl0906.h:1
bool new_value(std::string &value) override
Definition filter.cpp:65
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:19
void input(std::string value)
Definition filter.cpp:14
virtual void initialize(TextSensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition filter.cpp:28
lambda_filter_t lambda_filter_
Definition filter.h:62
LambdaFilter(lambda_filter_t lambda_filter)
Definition filter.cpp:35
void set_lambda_filter(const lambda_filter_t &lambda_filter)
Definition filter.cpp:37
bool new_value(std::string &value) override
Definition filter.cpp:39
const lambda_filter_t & get_lambda_filter() const
Definition filter.cpp:36
bool new_value(std::string &value) override
Definition filter.cpp:91
const Substitution * mappings_
Definition filter.h:169
bool new_value(std::string &value) override
Definition filter.cpp:71
bool new_value(std::string &value) override
Definition filter.cpp:77
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:58
bool new_value(std::string &value) override
Definition filter.cpp:51
NameCopy * next
std::function< optional< std::string >(std::string)> lambda_filter_t
Definition filter.h:44
ESPHOME_LOG_TAG(TAG, "text_sensor.filter")
size_t size_t pos
Definition helpers.h:1255
STL namespace.