ESPHome 2026.2.3
Loading...
Searching...
No Matches
filter.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace esphome {
7namespace text_sensor {
8
9class TextSensor;
10
16class Filter {
17 public:
26 virtual bool new_value(std::string &value) = 0;
27
29 virtual void initialize(TextSensor *parent, Filter *next);
30
31 void input(std::string value);
32
33 void output(std::string &value);
34
35 protected:
36 friend TextSensor;
37
38 Filter *next_{nullptr};
40};
41
42using lambda_filter_t = std::function<optional<std::string>(std::string)>;
43
50class LambdaFilter : public Filter {
51 public:
52 explicit LambdaFilter(lambda_filter_t lambda_filter);
53
54 bool new_value(std::string &value) override;
55
57 void set_lambda_filter(const lambda_filter_t &lambda_filter);
58
59 protected:
61};
62
69 public:
70 explicit StatelessLambdaFilter(optional<std::string> (*lambda_filter)(std::string)) : lambda_filter_(lambda_filter) {}
71
72 bool new_value(std::string &value) override {
73 auto result = this->lambda_filter_(value);
74 if (result.has_value()) {
75 value = std::move(*result);
76 return true;
77 }
78 return false;
79 }
80
81 protected:
83};
84
86class ToUpperFilter : public Filter {
87 public:
88 bool new_value(std::string &value) override;
89};
90
92class ToLowerFilter : public Filter {
93 public:
94 bool new_value(std::string &value) override;
95};
96
98class AppendFilter : public Filter {
99 public:
100 explicit AppendFilter(const char *suffix) : suffix_(suffix) {}
101 bool new_value(std::string &value) override;
102
103 protected:
104 const char *suffix_;
105};
106
108class PrependFilter : public Filter {
109 public:
110 explicit PrependFilter(const char *prefix) : prefix_(prefix) {}
111 bool new_value(std::string &value) override;
112
113 protected:
114 const char *prefix_;
115};
116
118 const char *from;
119 const char *to;
120};
121
123class SubstituteFilter : public Filter {
124 public:
125 explicit SubstituteFilter(const std::initializer_list<Substitution> &substitutions);
126 bool new_value(std::string &value) override;
127
128 protected:
130};
131
156class MapFilter : public Filter {
157 public:
158 explicit MapFilter(const std::initializer_list<Substitution> &mappings);
159 bool new_value(std::string &value) override;
160
161 protected:
163};
164
165} // namespace text_sensor
166} // namespace esphome
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:227
A simple filter that adds a string to the end of another string.
Definition filter.h:98
bool new_value(std::string &value) override
Definition filter.cpp:63
AppendFilter(const char *suffix)
Definition filter.h:100
Apply a filter to text sensor values such as to_upper.
Definition filter.h:16
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:17
void input(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:26
This class allows for creation of simple template filters.
Definition filter.h:50
lambda_filter_t lambda_filter_
Definition filter.h:60
LambdaFilter(lambda_filter_t lambda_filter)
Definition filter.cpp:33
void set_lambda_filter(const lambda_filter_t &lambda_filter)
Definition filter.cpp:35
bool new_value(std::string &value) override
Definition filter.cpp:37
const lambda_filter_t & get_lambda_filter() const
Definition filter.cpp:34
A filter that maps values from one set to another.
Definition filter.h:156
bool new_value(std::string &value) override
Definition filter.cpp:97
MapFilter(const std::initializer_list< Substitution > &mappings)
Definition filter.cpp:95
FixedVector< Substitution > mappings_
Definition filter.h:162
A simple filter that adds a string to the start of another string.
Definition filter.h:108
bool new_value(std::string &value) override
Definition filter.cpp:69
PrependFilter(const char *prefix)
Definition filter.h:110
Optimized lambda filter for stateless lambdas (no capture).
Definition filter.h:68
optional< std::string >(* lambda_filter_)(std::string)
Definition filter.h:82
StatelessLambdaFilter(optional< std::string >(*lambda_filter)(std::string))
Definition filter.h:70
bool new_value(std::string &value) override
Definition filter.h:72
A simple filter that replaces a substring with another substring.
Definition filter.h:123
FixedVector< Substitution > substitutions_
Definition filter.h:129
bool new_value(std::string &value) override
Definition filter.cpp:78
SubstituteFilter(const std::initializer_list< Substitution > &substitutions)
Definition filter.cpp:75
A simple filter that converts all text to lowercase.
Definition filter.h:92
bool new_value(std::string &value) override
Definition filter.cpp:56
A simple filter that converts all text to uppercase.
Definition filter.h:86
bool new_value(std::string &value) override
Definition filter.cpp:49
std::function< optional< std::string >(std::string)> lambda_filter_t
Definition filter.h:42
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7