ESPHome  2024.11.0
filter.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
5 #include <queue>
6 #include <utility>
7 #include <map>
8 #include <vector>
9 
10 namespace esphome {
11 namespace text_sensor {
12 
13 class TextSensor;
14 
20 class Filter {
21  public:
31  virtual optional<std::string> new_value(std::string value) = 0;
32 
34  virtual void initialize(TextSensor *parent, Filter *next);
35 
36  void input(const std::string &value);
37 
38  void output(const std::string &value);
39 
40  protected:
41  friend TextSensor;
42 
43  Filter *next_{nullptr};
44  TextSensor *parent_{nullptr};
45 };
46 
47 using lambda_filter_t = std::function<optional<std::string>(std::string)>;
48 
56 class LambdaFilter : public Filter {
57  public:
58  explicit LambdaFilter(lambda_filter_t lambda_filter);
59 
60  optional<std::string> new_value(std::string value) override;
61 
62  const lambda_filter_t &get_lambda_filter() const;
63  void set_lambda_filter(const lambda_filter_t &lambda_filter);
64 
65  protected:
67 };
68 
70 class ToUpperFilter : public Filter {
71  public:
72  optional<std::string> new_value(std::string value) override;
73 };
74 
76 class ToLowerFilter : public Filter {
77  public:
78  optional<std::string> new_value(std::string value) override;
79 };
80 
82 class AppendFilter : public Filter {
83  public:
84  AppendFilter(std::string suffix) : suffix_(std::move(suffix)) {}
85  optional<std::string> new_value(std::string value) override;
86 
87  protected:
88  std::string suffix_;
89 };
90 
92 class PrependFilter : public Filter {
93  public:
94  PrependFilter(std::string prefix) : prefix_(std::move(prefix)) {}
95  optional<std::string> new_value(std::string value) override;
96 
97  protected:
98  std::string prefix_;
99 };
100 
102 class SubstituteFilter : public Filter {
103  public:
104  SubstituteFilter(std::vector<std::string> from_strings, std::vector<std::string> to_strings)
105  : from_strings_(std::move(from_strings)), to_strings_(std::move(to_strings)) {}
106  optional<std::string> new_value(std::string value) override;
107 
108  protected:
109  std::vector<std::string> from_strings_;
110  std::vector<std::string> to_strings_;
111 };
112 
114 class MapFilter : public Filter {
115  public:
116  MapFilter(std::map<std::string, std::string> mappings) : mappings_(std::move(mappings)) {}
117  optional<std::string> new_value(std::string value) override;
118 
119  protected:
120  std::map<std::string, std::string> mappings_;
121 };
122 
123 } // namespace text_sensor
124 } // namespace esphome
A simple filter that adds a string to the start of another string.
Definition: filter.h:92
This class allows for creation of simple template filters.
Definition: filter.h:56
void input(const std::string &value)
Definition: filter.cpp:12
MapFilter(std::map< std::string, std::string > mappings)
Definition: filter.h:116
TextSensor * parent_
Definition: filter.h:44
std::vector< std::string > from_strings_
Definition: filter.h:109
STL namespace.
lambda_filter_t lambda_filter_
Definition: filter.h:66
virtual optional< std::string > new_value(std::string value)=0
This will be called every time the filter receives a new value.
std::function< optional< std::string >(std::string)> lambda_filter_t
Definition: filter.h:47
A filter that maps values from one set to another.
Definition: filter.h:114
std::vector< std::string > to_strings_
Definition: filter.h:110
std::map< std::string, std::string > mappings_
Definition: filter.h:120
A simple filter that adds a string to the end of another string.
Definition: filter.h:82
virtual void initialize(TextSensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition: filter.cpp:27
AppendFilter(std::string suffix)
Definition: filter.h:84
PrependFilter(std::string prefix)
Definition: filter.h:94
void output(const std::string &value)
Definition: filter.cpp:18
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
A simple filter that converts all text to lowercase.
Definition: filter.h:76
A simple filter that replaces a substring with another substring.
Definition: filter.h:102
Apply a filter to text sensor values such as to_upper.
Definition: filter.h:20
A simple filter that converts all text to uppercase.
Definition: filter.h:70
SubstituteFilter(std::vector< std::string > from_strings, std::vector< std::string > to_strings)
Definition: filter.h:104