ESPHome  2024.4.0
filter.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
6 
7 #include <vector>
8 
9 namespace esphome {
10 
11 namespace binary_sensor {
12 
13 class BinarySensor;
14 
15 class Filter {
16  public:
17  virtual optional<bool> new_value(bool value, bool is_initial) = 0;
18 
19  void input(bool value, bool is_initial);
20 
21  void output(bool value, bool is_initial);
22 
23  protected:
24  friend BinarySensor;
25 
26  Filter *next_{nullptr};
27  BinarySensor *parent_{nullptr};
29 };
30 
31 class DelayedOnOffFilter : public Filter, public Component {
32  public:
33  optional<bool> new_value(bool value, bool is_initial) override;
34 
35  float get_setup_priority() const override;
36 
37  template<typename T> void set_on_delay(T delay) { this->on_delay_ = delay; }
38  template<typename T> void set_off_delay(T delay) { this->off_delay_ = delay; }
39 
40  protected:
43 };
44 
45 class DelayedOnFilter : public Filter, public Component {
46  public:
47  optional<bool> new_value(bool value, bool is_initial) override;
48 
49  float get_setup_priority() const override;
50 
51  template<typename T> void set_delay(T delay) { this->delay_ = delay; }
52 
53  protected:
55 };
56 
57 class DelayedOffFilter : public Filter, public Component {
58  public:
59  optional<bool> new_value(bool value, bool is_initial) override;
60 
61  float get_setup_priority() const override;
62 
63  template<typename T> void set_delay(T delay) { this->delay_ = delay; }
64 
65  protected:
67 };
68 
69 class InvertFilter : public Filter {
70  public:
71  optional<bool> new_value(bool value, bool is_initial) override;
72 };
73 
75  AutorepeatFilterTiming(uint32_t delay, uint32_t off, uint32_t on) {
76  this->delay = delay;
77  this->time_off = off;
78  this->time_on = on;
79  }
80  uint32_t delay;
81  uint32_t time_off;
82  uint32_t time_on;
83 };
84 
85 class AutorepeatFilter : public Filter, public Component {
86  public:
87  explicit AutorepeatFilter(std::vector<AutorepeatFilterTiming> timings);
88 
89  optional<bool> new_value(bool value, bool is_initial) override;
90 
91  float get_setup_priority() const override;
92 
93  protected:
94  void next_timing_();
95  void next_value_(bool val);
96 
97  std::vector<AutorepeatFilterTiming> timings_;
98  uint8_t active_timing_{0};
99 };
100 
101 class LambdaFilter : public Filter {
102  public:
103  explicit LambdaFilter(std::function<optional<bool>(bool)> f);
104 
105  optional<bool> new_value(bool value, bool is_initial) override;
106 
107  protected:
108  std::function<optional<bool>(bool)> f_;
109 };
110 
111 class SettleFilter : public Filter, public Component {
112  public:
113  optional<bool> new_value(bool value, bool is_initial) override;
114 
115  float get_setup_priority() const override;
116 
117  template<typename T> void set_delay(T delay) { this->delay_ = delay; }
118 
119  protected:
121  bool steady_{true};
122 };
123 
124 } // namespace binary_sensor
125 
126 } // namespace esphome
std::vector< AutorepeatFilterTiming > timings_
Definition: filter.h:97
mopeka_std_values val[4]
virtual optional< bool > new_value(bool value, bool is_initial)=0
void input(bool value, bool is_initial)
Definition: filter.cpp:22
void output(bool value, bool is_initial)
Definition: filter.cpp:12
BinarySensor * parent_
Definition: filter.h:27
Deduplicator< bool > dedup_
Definition: filter.h:28
AutorepeatFilterTiming(uint32_t delay, uint32_t off, uint32_t on)
Definition: filter.h:75
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26