ESPHome 2026.5.0b1
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 optional<bool> new_value(bool value) override;
57
58 template<typename T> void set_delay(T delay) { this->delay_ = delay; }
59
60 protected:
62};
63
64class DelayedOffFilter : public Filter {
65 public:
66 optional<bool> new_value(bool value) override;
67
68 template<typename T> void set_delay(T delay) { this->delay_ = delay; }
69
70 protected:
72};
73
74class InvertFilter : public Filter {
75 public:
76 optional<bool> new_value(bool value) override;
77};
78
84
90 public:
91 optional<bool> new_value(bool value) override;
94
95 protected:
97 void next_timing_();
98 void next_value_(bool val);
99
101 uint8_t timings_count_{0};
102 uint8_t active_timing_{0};
103};
104
107template<size_t N> class AutorepeatFilter : public AutorepeatFilterBase {
108 public:
109 explicit AutorepeatFilter(std::initializer_list<AutorepeatFilterTiming> timings) {
110 init_array_from(this->timings_storage_, timings);
111 this->timings_ = this->timings_storage_.data();
112 this->timings_count_ = N;
113 }
114
115 protected:
116 std::array<AutorepeatFilterTiming, N> timings_storage_{};
117};
118
119class LambdaFilter : public Filter {
120 public:
121 explicit LambdaFilter(std::function<optional<bool>(bool)> f);
122
123 optional<bool> new_value(bool value) override;
124
125 protected:
126 std::function<optional<bool>(bool)> f_;
127};
128
135 public:
136 explicit StatelessLambdaFilter(optional<bool> (*f)(bool)) : f_(f) {}
137
138 optional<bool> new_value(bool value) override { return this->f_(value); }
139
140 protected:
141 optional<bool> (*f_)(bool);
142};
143
144class SettleFilter : public Filter {
145 public:
146 optional<bool> new_value(bool value) override;
147
148 template<typename T> void set_delay(T delay) { this->delay_ = delay; }
149
150 protected:
152 bool steady_{true};
153};
154
155} // namespace esphome::binary_sensor
156
157#endif // USE_BINARY_SENSOR_FILTER
Helper class to deduplicate items in a series of values.
Definition helpers.h:1833
Function-pointer-only templatable storage (4 bytes on 32-bit).
Definition automation.h:40
Non-template base for AutorepeatFilter — all methods in filter.cpp.
Definition filter.h:89
AutorepeatFilterBase(const AutorepeatFilterBase &)=delete
optional< bool > new_value(bool value) override
Definition filter.cpp:71
const AutorepeatFilterTiming * timings_
Definition filter.h:100
AutorepeatFilterBase & operator=(const AutorepeatFilterBase &)=delete
Template wrapper that provides inline std::array storage for timings.
Definition filter.h:107
std::array< AutorepeatFilterTiming, N > timings_storage_
Definition filter.h:116
AutorepeatFilter(std::initializer_list< AutorepeatFilterTiming > timings)
Definition filter.h:109
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:71
optional< bool > new_value(bool value) override
Definition filter.cpp:44
TemplatableFn< uint32_t > delay_
Definition filter.h:61
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:126
optional< bool > new_value(bool value) override
Definition filter.cpp:105
TemplatableFn< uint32_t > delay_
Definition filter.h:151
optional< bool > new_value(bool value) override
Definition filter.cpp:107
Optimized lambda filter for stateless lambdas (no capture).
Definition filter.h:134
StatelessLambdaFilter(optional< bool >(*f)(bool))
Definition filter.h:136
optional< bool > new_value(bool value) override
Definition filter.h:138
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:512
void HOT delay(uint32_t ms)
Definition hal.cpp:82
static void uint32_t