ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
filter.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_BINARY_SENSOR_FILTER
5
6#include <array>
7
11
12namespace esphome::binary_sensor {
13
14class BinarySensor;
15
16class Filter {
17 public:
18 virtual optional<bool> new_value(bool value) = 0;
19
20 virtual void input(bool value);
21
22 void output(bool value);
23
24 protected:
26
27 Filter *next_{nullptr};
30};
31
32class TimeoutFilter : public Filter {
33 public:
34 optional<bool> new_value(bool value) override { return value; }
35 void input(bool value) override;
36 template<typename T> void set_timeout_value(T timeout) { this->timeout_delay_ = timeout; }
37
38 protected:
40};
41
42class DelayedOnOffFilter final : public Filter {
43 public:
44 optional<bool> new_value(bool value) override;
45
46 template<typename T> void set_on_delay(T delay) { this->on_delay_ = delay; }
47 template<typename T> void set_off_delay(T delay) { this->off_delay_ = delay; }
48
49 protected:
52};
53
54class DelayedOnFilter : public Filter {
55 public:
56 // User provided, not "= default": `new(p) DelayedOnFilter()` would zero-fill .bss that is already zero.
58
59 optional<bool> new_value(bool value) override;
60
61 template<typename T> void set_delay(T delay) { this->delay_ = delay; }
62
63 protected:
65};
66
67class DelayedOffFilter : public Filter {
68 public:
69 // User provided, not "= default": `new(p) DelayedOffFilter()` would zero-fill .bss that is already zero.
71
72 optional<bool> new_value(bool value) override;
73
74 template<typename T> void set_delay(T delay) { this->delay_ = delay; }
75
76 protected:
78};
79
80class InvertFilter : public Filter {
81 public:
82 optional<bool> new_value(bool value) override;
83};
84
90
96 public:
97 optional<bool> new_value(bool value) override;
100
101 protected:
103 void next_timing_();
104 void next_value_(bool val);
105
107 uint8_t timings_count_{0};
108 uint8_t active_timing_{0};
109};
110
113template<size_t N> class AutorepeatFilter : public AutorepeatFilterBase {
114 public:
115 explicit AutorepeatFilter(std::initializer_list<AutorepeatFilterTiming> timings) {
116 init_array_from(this->timings_storage_, timings);
117 this->timings_ = this->timings_storage_.data();
118 this->timings_count_ = N;
119 }
120
121 protected:
122 std::array<AutorepeatFilterTiming, N> timings_storage_{};
123};
124
125class LambdaFilter : public Filter {
126 public:
127 explicit LambdaFilter(std::function<optional<bool>(bool)> f);
128
129 optional<bool> new_value(bool value) override;
130
131 protected:
132 std::function<optional<bool>(bool)> f_;
133};
134
141 public:
142 explicit StatelessLambdaFilter(optional<bool> (*f)(bool)) : f_(f) {}
143
144 optional<bool> new_value(bool value) override { return this->f_(value); }
145
146 protected:
147 optional<bool> (*f_)(bool);
148};
149
150class SettleFilter : public Filter {
151 public:
152 // User provided, not "= default": `new(p) SettleFilter()` would zero-fill .bss that is already zero.
154 optional<bool> new_value(bool value) override;
155
156 template<typename T> void set_delay(T delay) { this->delay_ = delay; }
157
158 protected:
160 bool steady_{true};
161};
162
163} // namespace esphome::binary_sensor
164
165#endif // USE_BINARY_SENSOR_FILTER
Helper class to deduplicate items in a series of values.
Definition helpers.h:2045
Function-pointer-only templatable storage (4 bytes on 32-bit).
Definition automation.h:20
Non-template base for AutorepeatFilter — all methods in filter.cpp.
Definition filter.h:95
AutorepeatFilterBase(const AutorepeatFilterBase &)=delete
optional< bool > new_value(bool value) override
Definition filter.cpp:71
const AutorepeatFilterTiming * timings_
Definition filter.h:106
AutorepeatFilterBase & operator=(const AutorepeatFilterBase &)=delete
Template wrapper that provides inline std::array storage for timings.
Definition filter.h:113
std::array< AutorepeatFilterTiming, N > timings_storage_
Definition filter.h:122
AutorepeatFilter(std::initializer_list< AutorepeatFilterTiming > timings)
Definition filter.h:115
Base class for all binary_sensor-type classes.
optional< bool > new_value(bool value) override
Definition filter.cpp:54
TemplatableFn< uint32_t > delay_
Definition filter.h:77
optional< bool > new_value(bool value) override
Definition filter.cpp:44
TemplatableFn< uint32_t > delay_
Definition filter.h:64
TemplatableFn< uint32_t > on_delay_
Definition filter.h:50
TemplatableFn< uint32_t > off_delay_
Definition filter.h:51
optional< bool > new_value(bool value) override
Definition filter.cpp:35
virtual void input(bool value)
Definition filter.cpp:20
virtual optional< bool > new_value(bool value)=0
Deduplicator< bool > dedup_
Definition filter.h:29
void output(bool value)
Definition filter.cpp:13
optional< bool > new_value(bool value) override
Definition filter.cpp:64
LambdaFilter(std::function< optional< bool >(bool)> f)
Definition filter.cpp:103
std::function< optional< bool >(bool)> f_
Definition filter.h:132
optional< bool > new_value(bool value) override
Definition filter.cpp:105
TemplatableFn< uint32_t > delay_
Definition filter.h:159
optional< bool > new_value(bool value) override
Definition filter.cpp:107
Optimized lambda filter for stateless lambdas (no capture).
Definition filter.h:140
StatelessLambdaFilter(optional< bool >(*f)(bool))
Definition filter.h:142
optional< bool > new_value(bool value) override
Definition filter.h:144
TemplatableFn< uint32_t > timeout_delay_
Definition filter.h:39
void input(bool value) override
Definition filter.cpp:29
optional< bool > new_value(bool value) override
Definition filter.h:34
mopeka_std_values val[3]
void init_array_from(std::array< T, N > &dest, std::initializer_list< T > src)
Initialize a std::array from an initializer_list.
Definition helpers.h:599
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t