ESPHome 2026.11.0b6
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
28 virtual void reset();
29
30 Filter *next_{nullptr};
33};
34
35class TimeoutFilter : public Filter {
36 public:
37 optional<bool> new_value(bool value) override { return value; }
38 void input(bool value) override;
39 template<typename T> void set_timeout_value(T timeout) { this->timeout_delay_ = timeout; }
40
41 protected:
43};
44
45class DelayedOnOffFilter final : public Filter {
46 public:
47 optional<bool> new_value(bool value) override;
48
49 template<typename T> void set_on_delay(T delay) { this->on_delay_ = delay; }
50 template<typename T> void set_off_delay(T delay) { this->off_delay_ = delay; }
51
52 protected:
55};
56
57class DelayedOnFilter : public Filter {
58 public:
59 // User provided, not "= default": `new(p) DelayedOnFilter()` would zero-fill .bss that is already zero.
61
62 optional<bool> new_value(bool value) override;
63
64 template<typename T> void set_delay(T delay) { this->delay_ = delay; }
65
66 protected:
68};
69
70class DelayedOffFilter : public Filter {
71 public:
72 // User provided, not "= default": `new(p) DelayedOffFilter()` would zero-fill .bss that is already zero.
74
75 optional<bool> new_value(bool value) override;
76
77 template<typename T> void set_delay(T delay) { this->delay_ = delay; }
78
79 protected:
81};
82
83class InvertFilter : public Filter {
84 public:
85 optional<bool> new_value(bool value) override;
86};
87
93
99 public:
100 optional<bool> new_value(bool value) override;
103
104 protected:
106 void reset() override;
107 void next_timing_();
108 void next_value_(bool val);
109
111 uint8_t timings_count_{0};
112 uint8_t active_timing_{0};
113};
114
117template<size_t N> class AutorepeatFilter : public AutorepeatFilterBase {
118 public:
119 explicit AutorepeatFilter(std::initializer_list<AutorepeatFilterTiming> timings) {
120 init_array_from(this->timings_storage_, timings);
121 this->timings_ = this->timings_storage_.data();
122 this->timings_count_ = N;
123 }
124
125 protected:
126 std::array<AutorepeatFilterTiming, N> timings_storage_{};
127};
128
129class LambdaFilter : public Filter {
130 public:
131 explicit LambdaFilter(std::function<optional<bool>(bool)> f);
132
133 optional<bool> new_value(bool value) override;
134
135 protected:
136 std::function<optional<bool>(bool)> f_;
137};
138
145 public:
146 explicit StatelessLambdaFilter(optional<bool> (*f)(bool)) : f_(f) {}
147
148 optional<bool> new_value(bool value) override { return this->f_(value); }
149
150 protected:
151 optional<bool> (*f_)(bool);
152};
153
154class SettleFilter : public Filter {
155 public:
156 // User provided, not "= default": `new(p) SettleFilter()` would zero-fill .bss that is already zero.
158 optional<bool> new_value(bool value) override;
159
160 template<typename T> void set_delay(T delay) { this->delay_ = delay; }
161
162 protected:
163 void reset() override;
164
165 bool steady_{true}; // Before delay_ so it fills the Filter base's tail padding
167};
168
169} // namespace esphome::binary_sensor
170
171#endif // USE_BINARY_SENSOR_FILTER
Helper class to deduplicate items in a series of values.
Definition helpers.h:2132
Function-pointer-only templatable storage (4 bytes on 32-bit).
Definition automation.h:21
Non-template base for AutorepeatFilter — all methods in filter.cpp.
Definition filter.h:98
AutorepeatFilterBase(const AutorepeatFilterBase &)=delete
optional< bool > new_value(bool value) override
Definition filter.cpp:77
const AutorepeatFilterTiming * timings_
Definition filter.h:110
AutorepeatFilterBase & operator=(const AutorepeatFilterBase &)=delete
Template wrapper that provides inline std::array storage for timings.
Definition filter.h:117
std::array< AutorepeatFilterTiming, N > timings_storage_
Definition filter.h:126
AutorepeatFilter(std::initializer_list< AutorepeatFilterTiming > timings)
Definition filter.h:119
Base class for all binary_sensor-type classes.
optional< bool > new_value(bool value) override
Definition filter.cpp:60
TemplatableFn< uint32_t > delay_
Definition filter.h:80
optional< bool > new_value(bool value) override
Definition filter.cpp:50
TemplatableFn< uint32_t > delay_
Definition filter.h:67
TemplatableFn< uint32_t > on_delay_
Definition filter.h:53
TemplatableFn< uint32_t > off_delay_
Definition filter.h:54
optional< bool > new_value(bool value) override
Definition filter.cpp:41
virtual void input(bool value)
Definition filter.cpp:26
virtual optional< bool > new_value(bool value)=0
Deduplicator< bool > dedup_
Definition filter.h:32
void output(bool value)
Definition filter.cpp:13
virtual void reset()
Forget the last value and drop any pending scheduled output; called when the sensor's state is invali...
Definition filter.cpp:21
optional< bool > new_value(bool value) override
Definition filter.cpp:70
LambdaFilter(std::function< optional< bool >(bool)> f)
Definition filter.cpp:118
std::function< optional< bool >(bool)> f_
Definition filter.h:136
optional< bool > new_value(bool value) override
Definition filter.cpp:120
TemplatableFn< uint32_t > delay_
Definition filter.h:166
optional< bool > new_value(bool value) override
Definition filter.cpp:127
Optimized lambda filter for stateless lambdas (no capture).
Definition filter.h:144
StatelessLambdaFilter(optional< bool >(*f)(bool))
Definition filter.h:146
optional< bool > new_value(bool value) override
Definition filter.h:148
TemplatableFn< uint32_t > timeout_delay_
Definition filter.h:42
void input(bool value) override
Definition filter.cpp:35
optional< bool > new_value(bool value) override
Definition filter.h:37
mopeka_std_values val[3]
uint32_t value
Definition hal.cpp:41
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:644
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t