ESPHome 2026.3.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:
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
125class SubstituteFilter : public Filter {
126 public:
127 explicit SubstituteFilter(const std::initializer_list<Substitution> &substitutions);
128 bool new_value(std::string &value) override;
129
130 protected:
132};
133
158class MapFilter : public Filter {
159 public:
160 explicit MapFilter(const std::initializer_list<Substitution> &mappings);
161 bool new_value(std::string &value) override;
162
163 protected:
165};
166
167} // namespace esphome::text_sensor
168
169#endif // USE_TEXT_SENSOR_FILTER
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:299
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:158
bool new_value(std::string &value) override
Definition filter.cpp:99
MapFilter(const std::initializer_list< Substitution > &mappings)
Definition filter.cpp:97
FixedVector< Substitution > mappings_
Definition filter.h:164
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:125
FixedVector< Substitution > substitutions_
Definition filter.h:131
bool new_value(std::string &value) override
Definition filter.cpp:80
SubstituteFilter(const std::initializer_list< Substitution > &substitutions)
Definition filter.cpp:77
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
std::function< optional< std::string >(std::string)> lambda_filter_t
Definition filter.h:44