ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
filter.cpp
Go to the documentation of this file.
1#include "filter.h"
2
3#include "binary_sensor.h"
4
5namespace esphome {
6
7namespace binary_sensor {
8
9static const char *const TAG = "sensor.filter";
10
11void Filter::output(bool value) {
12 if (this->next_ == nullptr) {
13 this->parent_->send_state_internal(value);
14 } else {
15 this->next_->input(value);
16 }
17}
18void Filter::input(bool value) {
19 if (!this->dedup_.next(value))
20 return;
21 auto b = this->new_value(value);
22 if (b.has_value()) {
23 this->output(*b);
24 }
25}
26
27void TimeoutFilter::input(bool value) {
28 this->set_timeout("timeout", this->timeout_delay_.value(), [this]() { this->parent_->invalidate_state(); });
29 // we do not de-dup here otherwise changes from invalid to valid state will not be output
30 this->output(value);
31}
32
34 if (value) {
35 this->set_timeout("ON_OFF", this->on_delay_.value(), [this]() { this->output(true); });
36 } else {
37 this->set_timeout("ON_OFF", this->off_delay_.value(), [this]() { this->output(false); });
38 }
39 return {};
40}
41
43
45 if (value) {
46 this->set_timeout("ON", this->delay_.value(), [this]() { this->output(true); });
47 return {};
48 } else {
49 this->cancel_timeout("ON");
50 return false;
51 }
52}
53
55
57 if (!value) {
58 this->set_timeout("OFF", this->delay_.value(), [this]() { this->output(false); });
59 return {};
60 } else {
61 this->cancel_timeout("OFF");
62 return true;
63 }
64}
65
67
68optional<bool> InvertFilter::new_value(bool value) { return !value; }
69
70AutorepeatFilter::AutorepeatFilter(std::initializer_list<AutorepeatFilterTiming> timings) : timings_(timings) {}
71
73 if (value) {
74 // Ignore if already running
75 if (this->active_timing_ != 0)
76 return {};
77
78 this->next_timing_();
79 return true;
80 } else {
81 this->cancel_timeout("TIMING");
82 this->cancel_timeout("ON_OFF");
83 this->active_timing_ = 0;
84 return false;
85 }
86}
87
89 // Entering this method
90 // 1st time: starts waiting the first delay
91 // 2nd time: starts waiting the second delay and starts toggling with the first time_off / _on
92 // last time: no delay to start but have to bump the index to reflect the last
93 if (this->active_timing_ < this->timings_.size())
94 this->set_timeout("TIMING", this->timings_[this->active_timing_].delay, [this]() { this->next_timing_(); });
95
96 if (this->active_timing_ <= this->timings_.size()) {
97 this->active_timing_++;
98 }
99
100 if (this->active_timing_ == 2)
101 this->next_value_(false);
102
103 // Leaving this method: if the toggling is started, it has to use [active_timing_ - 2] for the intervals
104}
105
107 const AutorepeatFilterTiming &timing = this->timings_[this->active_timing_ - 2];
108 this->output(val); // This is at least the second one so not initial
109 this->set_timeout("ON_OFF", val ? timing.time_on : timing.time_off, [this, val]() { this->next_value_(!val); });
110}
111
113
114LambdaFilter::LambdaFilter(std::function<optional<bool>(bool)> f) : f_(std::move(f)) {}
115
116optional<bool> LambdaFilter::new_value(bool value) { return this->f_(value); }
117
119 if (!this->steady_) {
120 this->set_timeout("SETTLE", this->delay_.value(), [this, value]() {
121 this->steady_ = true;
122 this->output(value);
123 });
124 return {};
125 } else {
126 this->steady_ = false;
127 this->output(value);
128 this->set_timeout("SETTLE", this->delay_.value(), [this]() { this->steady_ = true; });
129 return value;
130 }
131}
132
134
135} // namespace binary_sensor
136
137} // namespace esphome
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool next(T value)
Feeds the next item in the series to the deduplicator and returns false if this is a duplicate.
Definition helpers.h:893
float get_setup_priority() const override
Definition filter.cpp:112
AutorepeatFilter(std::initializer_list< AutorepeatFilterTiming > timings)
Definition filter.cpp:70
FixedVector< AutorepeatFilterTiming > timings_
Definition filter.h:100
optional< bool > new_value(bool value) override
Definition filter.cpp:72
float get_setup_priority() const override
Definition filter.cpp:66
optional< bool > new_value(bool value) override
Definition filter.cpp:56
TemplatableValue< uint32_t > delay_
Definition filter.h:74
optional< bool > new_value(bool value) override
Definition filter.cpp:44
TemplatableValue< uint32_t > delay_
Definition filter.h:62
float get_setup_priority() const override
Definition filter.cpp:54
TemplatableValue< uint32_t > on_delay_
Definition filter.h:49
float get_setup_priority() const override
Definition filter.cpp:42
optional< bool > new_value(bool value) override
Definition filter.cpp:33
TemplatableValue< uint32_t > off_delay_
Definition filter.h:50
virtual void input(bool value)
Definition filter.cpp:18
virtual optional< bool > new_value(bool value)=0
Deduplicator< bool > dedup_
Definition filter.h:26
void output(bool value)
Definition filter.cpp:11
optional< bool > new_value(bool value) override
Definition filter.cpp:68
LambdaFilter(std::function< optional< bool >(bool)> f)
Definition filter.cpp:114
std::function< optional< bool >(bool)> f_
Definition filter.h:111
optional< bool > new_value(bool value) override
Definition filter.cpp:116
float get_setup_priority() const override
Definition filter.cpp:133
TemplatableValue< uint32_t > delay_
Definition filter.h:138
optional< bool > new_value(bool value) override
Definition filter.cpp:118
void input(bool value) override
Definition filter.cpp:27
TemplatableValue< uint32_t > timeout_delay_
Definition filter.h:36
mopeka_std_values val[4]
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:58
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:31