ESPHome  2022.6.3
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 
9 namespace esphome {
10 namespace text_sensor {
11 
12 class TextSensor;
13 
19 class Filter {
20  public:
30  virtual optional<std::string> new_value(std::string value);
31 
33  virtual void initialize(TextSensor *parent, Filter *next);
34 
35  void input(const std::string &value);
36 
37  void output(const std::string &value);
38 
39  protected:
40  friend TextSensor;
41 
42  Filter *next_{nullptr};
43  TextSensor *parent_{nullptr};
44 };
45 
46 using lambda_filter_t = std::function<optional<std::string>(std::string)>;
47 
55 class LambdaFilter : public Filter {
56  public:
57  explicit LambdaFilter(lambda_filter_t lambda_filter);
58 
59  optional<std::string> new_value(std::string value) override;
60 
61  const lambda_filter_t &get_lambda_filter() const;
62  void set_lambda_filter(const lambda_filter_t &lambda_filter);
63 
64  protected:
66 };
67 
69 class ToUpperFilter : public Filter {
70  public:
71  optional<std::string> new_value(std::string value) override;
72 };
73 
75 class ToLowerFilter : public Filter {
76  public:
77  optional<std::string> new_value(std::string value) override;
78 };
79 
81 class AppendFilter : public Filter {
82  public:
83  AppendFilter(std::string suffix) : suffix_(std::move(suffix)) {}
84  optional<std::string> new_value(std::string value) override;
85 
86  protected:
87  std::string suffix_;
88 };
89 
91 class PrependFilter : public Filter {
92  public:
93  PrependFilter(std::string prefix) : prefix_(std::move(prefix)) {}
94  optional<std::string> new_value(std::string value) override;
95 
96  protected:
97  std::string prefix_;
98 };
99 
101 class SubstituteFilter : public Filter {
102  public:
103  SubstituteFilter(std::vector<std::string> from_strings, std::vector<std::string> to_strings)
104  : from_strings_(std::move(from_strings)), to_strings_(std::move(to_strings)) {}
105  optional<std::string> new_value(std::string value) override;
106 
107  protected:
108  std::vector<std::string> from_strings_;
109  std::vector<std::string> to_strings_;
110 };
111 
113 class MapFilter : public Filter {
114  public:
115  MapFilter(std::map<std::string, std::string> mappings) : mappings_(std::move(mappings)) {}
116  optional<std::string> new_value(std::string value) override;
117 
118  protected:
119  std::map<std::string, std::string> mappings_;
120 };
121 
122 } // namespace text_sensor
123 } // namespace esphome
A simple filter that adds a string to the start of another string.
Definition: filter.h:91
This class allows for creation of simple template filters.
Definition: filter.h:55
void input(const std::string &value)
Definition: filter.cpp:12
MapFilter(std::map< std::string, std::string > mappings)
Definition: filter.h:115
TextSensor * parent_
Definition: filter.h:43
std::vector< std::string > from_strings_
Definition: filter.h:108
STL namespace.
lambda_filter_t lambda_filter_
Definition: filter.h:65
std::function< optional< std::string >(std::string)> lambda_filter_t
Definition: filter.h:46
A filter that maps values from one set to another.
Definition: filter.h:113
std::vector< std::string > to_strings_
Definition: filter.h:109
std::map< std::string, std::string > mappings_
Definition: filter.h:119
A simple filter that adds a string to the end of another string.
Definition: filter.h:81
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:83
PrependFilter(std::string prefix)
Definition: filter.h:93
void output(const std::string &value)
Definition: filter.cpp:18
virtual optional< std::string > new_value(std::string value)
This will be called every time the filter receives a new value.
Definition: a4988.cpp:4
A simple filter that converts all text to lowercase.
Definition: filter.h:75
A simple filter that replaces a substring with another substring.
Definition: filter.h:101
Apply a filter to text sensor values such as to_upper.
Definition: filter.h:19
A simple filter that converts all text to uppercase.
Definition: filter.h:69
SubstituteFilter(std::vector< std::string > from_strings, std::vector< std::string > to_strings)
Definition: filter.h:103