ESPHome  2022.8.0
filter.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
5 
6 namespace esphome {
7 
8 namespace binary_sensor {
9 
10 class BinarySensor;
11 
12 class Filter {
13  public:
14  virtual optional<bool> new_value(bool value, bool is_initial) = 0;
15 
16  void input(bool value, bool is_initial);
17 
18  void output(bool value, bool is_initial);
19 
20  protected:
21  friend BinarySensor;
22 
23  Filter *next_{nullptr};
24  BinarySensor *parent_{nullptr};
26 };
27 
28 class DelayedOnOffFilter : public Filter, public Component {
29  public:
30  explicit DelayedOnOffFilter(uint32_t delay);
31 
32  optional<bool> new_value(bool value, bool is_initial) override;
33 
34  float get_setup_priority() const override;
35 
36  protected:
37  uint32_t delay_;
38 };
39 
40 class DelayedOnFilter : public Filter, public Component {
41  public:
42  explicit DelayedOnFilter(uint32_t delay);
43 
44  optional<bool> new_value(bool value, bool is_initial) override;
45 
46  float get_setup_priority() const override;
47 
48  protected:
49  uint32_t delay_;
50 };
51 
52 class DelayedOffFilter : public Filter, public Component {
53  public:
54  explicit DelayedOffFilter(uint32_t delay);
55 
56  optional<bool> new_value(bool value, bool is_initial) override;
57 
58  float get_setup_priority() const override;
59 
60  protected:
61  uint32_t delay_;
62 };
63 
64 class InvertFilter : public Filter {
65  public:
66  optional<bool> new_value(bool value, bool is_initial) override;
67 };
68 
70  AutorepeatFilterTiming(uint32_t delay, uint32_t off, uint32_t on) {
71  this->delay = delay;
72  this->time_off = off;
73  this->time_on = on;
74  }
75  uint32_t delay;
76  uint32_t time_off;
77  uint32_t time_on;
78 };
79 
80 class AutorepeatFilter : public Filter, public Component {
81  public:
82  explicit AutorepeatFilter(std::vector<AutorepeatFilterTiming> timings);
83 
84  optional<bool> new_value(bool value, bool is_initial) override;
85 
86  float get_setup_priority() const override;
87 
88  protected:
89  void next_timing_();
90  void next_value_(bool val);
91 
92  std::vector<AutorepeatFilterTiming> timings_;
93  uint8_t active_timing_{0};
94 };
95 
96 class LambdaFilter : public Filter {
97  public:
98  explicit LambdaFilter(std::function<optional<bool>(bool)> f);
99 
100  optional<bool> new_value(bool value, bool is_initial) override;
101 
102  protected:
103  std::function<optional<bool>(bool)> f_;
104 };
105 
106 class UniqueFilter : public Filter {
107  public:
108  optional<bool> new_value(bool value, bool is_initial) override;
109 
110  protected:
111  optional<bool> last_value_{};
112 };
113 
114 } // namespace binary_sensor
115 
116 } // namespace esphome
std::vector< AutorepeatFilterTiming > timings_
Definition: filter.h:92
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:24
Deduplicator< bool > dedup_
Definition: filter.h:25
AutorepeatFilterTiming(uint32_t delay, uint32_t off, uint32_t on)
Definition: filter.h:70
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
uint32_t val
Definition: datatypes.h:85
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27