ESPHome  2024.11.0
filter.cpp
Go to the documentation of this file.
1 #include "filter.h"
2 #include "text_sensor.h"
3 #include "esphome/core/log.h"
4 #include "esphome/core/hal.h"
5 
6 namespace esphome {
7 namespace text_sensor {
8 
9 static const char *const TAG = "text_sensor.filter";
10 
11 // Filter
12 void Filter::input(const std::string &value) {
13  ESP_LOGVV(TAG, "Filter(%p)::input(%s)", this, value.c_str());
14  optional<std::string> out = this->new_value(value);
15  if (out.has_value())
16  this->output(*out);
17 }
18 void Filter::output(const std::string &value) {
19  if (this->next_ == nullptr) {
20  ESP_LOGVV(TAG, "Filter(%p)::output(%s) -> SENSOR", this, value.c_str());
22  } else {
23  ESP_LOGVV(TAG, "Filter(%p)::output(%s) -> %p", this, value.c_str(), this->next_);
24  this->next_->input(value);
25  }
26 }
27 void Filter::initialize(TextSensor *parent, Filter *next) {
28  ESP_LOGVV(TAG, "Filter(%p)::initialize(parent=%p next=%p)", this, parent, next);
29  this->parent_ = parent;
30  this->next_ = next;
31 }
32 
33 // LambdaFilter
34 LambdaFilter::LambdaFilter(lambda_filter_t lambda_filter) : lambda_filter_(std::move(lambda_filter)) {}
36 void LambdaFilter::set_lambda_filter(const lambda_filter_t &lambda_filter) { this->lambda_filter_ = lambda_filter; }
37 
39  auto it = this->lambda_filter_(value);
40  ESP_LOGVV(TAG, "LambdaFilter(%p)::new_value(%s) -> %s", this, value.c_str(), it.value_or("").c_str());
41  return it;
42 }
43 
44 // ToUpperFilter
46  for (char &c : value)
47  c = ::toupper(c);
48  return value;
49 }
50 
51 // ToLowerFilter
53  for (char &c : value)
54  c = ::tolower(c);
55  return value;
56 }
57 
58 // Append
59 optional<std::string> AppendFilter::new_value(std::string value) { return value + this->suffix_; }
60 
61 // Prepend
62 optional<std::string> PrependFilter::new_value(std::string value) { return this->prefix_ + value; }
63 
64 // Substitute
66  std::size_t pos;
67  for (size_t i = 0; i < this->from_strings_.size(); i++) {
68  while ((pos = value.find(this->from_strings_[i])) != std::string::npos)
69  value.replace(pos, this->from_strings_[i].size(), this->to_strings_[i]);
70  }
71  return value;
72 }
73 
74 // Map
76  auto item = mappings_.find(value);
77  return item == mappings_.end() ? value : item->second;
78 }
79 
80 } // namespace text_sensor
81 } // namespace esphome
void set_lambda_filter(const lambda_filter_t &lambda_filter)
Definition: filter.cpp:36
const lambda_filter_t & get_lambda_filter() const
Definition: filter.cpp:35
void input(const std::string &value)
Definition: filter.cpp:12
TextSensor * parent_
Definition: filter.h:44
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
const char *const TAG
Definition: spi.cpp:8
optional< std::string > new_value(std::string value) override
Definition: filter.cpp:52
void internal_send_state_to_frontend(const std::string &state)
Definition: text_sensor.cpp:61
optional< std::string > new_value(std::string value) override
Definition: filter.cpp:38
virtual void initialize(TextSensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition: filter.cpp:27
void output(const std::string &value)
Definition: filter.cpp:18
optional< std::string > new_value(std::string value) override
Definition: filter.cpp:59
optional< std::string > new_value(std::string value) override
Definition: filter.cpp:62
optional< std::string > new_value(std::string value) override
Definition: filter.cpp:45
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
optional< std::string > new_value(std::string value) override
Definition: filter.cpp:75
LambdaFilter(lambda_filter_t lambda_filter)
Definition: filter.cpp:34
Apply a filter to text sensor values such as to_upper.
Definition: filter.h:20
optional< std::string > new_value(std::string value) override
Definition: filter.cpp:65