ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
filter.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_TEXT_SENSOR_FILTER
5
8
9namespace esphome::text_sensor {
10
11class TextSensor;
12
18class Filter {
19 public:
28 virtual bool new_value(std::string &value) = 0;
29
31 virtual void initialize(TextSensor *parent, Filter *next);
32
33 void input(std::string value);
34
35 void output(std::string &value);
36
37 protected:
38 friend TextSensor;
39
40 Filter *next_{nullptr};
42};
43
44using lambda_filter_t = std::function<optional<std::string>(std::string)>;
45
52class LambdaFilter : public Filter {
53 public:
54 explicit LambdaFilter(lambda_filter_t lambda_filter);
55
56 bool new_value(std::string &value) override;
57
59 void set_lambda_filter(const lambda_filter_t &lambda_filter);
60
61 protected:
63};
64
71 public:
72 explicit StatelessLambdaFilter(optional<std::string> (*lambda_filter)(std::string)) : lambda_filter_(lambda_filter) {}
73
74 bool new_value(std::string &value) override {
75 auto result = this->lambda_filter_(value);
76 if (result.has_value()) {
77 value = std::move(*result);
78 return true;
79 }
80 return false;
81 }
82
83 protected:
84 optional<std::string> (*lambda_filter_)(std::string);
85};
86
88class ToUpperFilter : public Filter {
89 public:
90 bool new_value(std::string &value) override;
91};
92
94class ToLowerFilter : public Filter {
95 public:
96 bool new_value(std::string &value) override;
97};
98
100class AppendFilter : public Filter {
101 public:
102 explicit AppendFilter(const char *suffix) : suffix_(suffix) {}
103 bool new_value(std::string &value) override;
104
105 protected:
106 const char *suffix_;
107};
108
110class PrependFilter : public Filter {
111 public:
112 explicit PrependFilter(const char *prefix) : prefix_(prefix) {}
113 bool new_value(std::string &value) override;
114
115 protected:
116 const char *prefix_;
117};
118
120 const char *from;
121 const char *to;
122};
123
127class SubstituteFilter : public Filter {
128 public:
129 explicit SubstituteFilter(const Substitution *substitutions) : substitutions_(substitutions) {}
130 bool new_value(std::string &value) override;
131
132 protected:
134};
135
163class MapFilter : public Filter {
164 public:
165 explicit MapFilter(const Substitution *mappings) : mappings_(mappings) {}
166 bool new_value(std::string &value) override;
167
168 protected:
170};
171
172} // namespace esphome::text_sensor
173
174#endif // USE_TEXT_SENSOR_FILTER
A simple filter that adds a string to the end of another string.
Definition filter.h:100
bool new_value(std::string &value) override
Definition filter.cpp:65
AppendFilter(const char *suffix)
Definition filter.h:102
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
This class allows for creation of simple template filters.
Definition filter.h:52
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
A filter that maps values from one set to another.
Definition filter.h:163
bool new_value(std::string &value) override
Definition filter.cpp:91
const Substitution * mappings_
Definition filter.h:169
MapFilter(const Substitution *mappings)
Definition filter.h:165
A simple filter that adds a string to the start of another string.
Definition filter.h:110
bool new_value(std::string &value) override
Definition filter.cpp:71
PrependFilter(const char *prefix)
Definition filter.h:112
Optimized lambda filter for stateless lambdas (no capture).
Definition filter.h:70
optional< std::string >(* lambda_filter_)(std::string)
Definition filter.h:84
StatelessLambdaFilter(optional< std::string >(*lambda_filter)(std::string))
Definition filter.h:72
bool new_value(std::string &value) override
Definition filter.h:74
A simple filter that replaces a substring with another substring.
Definition filter.h:127
bool new_value(std::string &value) override
Definition filter.cpp:77
SubstituteFilter(const Substitution *substitutions)
Definition filter.h:129
const Substitution * substitutions_
Definition filter.h:133
A simple filter that converts all text to lowercase.
Definition filter.h:94
bool new_value(std::string &value) override
Definition filter.cpp:58
A simple filter that converts all text to uppercase.
Definition filter.h:88
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