ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
automation.cpp
Go to the documentation of this file.
1#include "automation.h"
2#include "esphome/core/log.h"
3
5
6static const char *const TAG = "binary_sensor.automation";
7
9 // Handle duplicate events
10 if (state == this->last_state_) {
11 return;
12 }
13 this->last_state_ = state;
14
15 // Cooldown: Do not immediately try matching after having invalid timing
16 if (this->is_in_cooldown_) {
17 return;
18 }
19
20 if (!this->at_index_.has_value()) {
21 // Start matching
22 MultiClickTriggerEvent evt = this->timing_[0];
23 if (evt.state == state) {
24 ESP_LOGV(TAG, "START min=%" PRIu32 " max=%" PRIu32, evt.min_length, evt.max_length);
25 ESP_LOGV(TAG, "Multi Click: Starting multi click action!");
26 this->at_index_ = 1;
27 if (this->timing_.size() == 1 && evt.max_length == 4294967294UL) {
28 this->set_timeout("trigger", evt.min_length, [this]() { this->trigger_(); });
29 } else {
32 }
33 } else {
34 ESP_LOGV(TAG, "Multi Click: action not started because first level does not match!");
35 }
36
37 return;
38 }
39
40 if (!this->is_valid_) {
41 this->schedule_cooldown_();
42 return;
43 }
44
45 if (*this->at_index_ == this->timing_.size()) {
46 this->trigger_();
47 return;
48 }
49
50 MultiClickTriggerEvent evt = this->timing_[*this->at_index_];
51
52 if (evt.max_length != 4294967294UL) {
53 ESP_LOGV(TAG, "A i=%zu min=%" PRIu32 " max=%" PRIu32, *this->at_index_, evt.min_length, evt.max_length); // NOLINT
56 } else if (*this->at_index_ + 1 != this->timing_.size()) {
57 ESP_LOGV(TAG, "B i=%zu min=%" PRIu32, *this->at_index_, evt.min_length); // NOLINT
58 this->cancel_timeout("is_not_valid");
60 } else {
61 ESP_LOGV(TAG, "C i=%zu min=%" PRIu32, *this->at_index_, evt.min_length); // NOLINT
62 this->is_valid_ = false;
63 this->cancel_timeout("is_not_valid");
64 this->set_timeout("trigger", evt.min_length, [this]() { this->trigger_(); });
65 }
66
67 *this->at_index_ = *this->at_index_ + 1;
68}
70 ESP_LOGV(TAG, "Multi Click: Invalid length of press, starting cooldown of %" PRIu32 " ms", this->invalid_cooldown_);
71 this->is_in_cooldown_ = true;
72 this->set_timeout("cooldown", this->invalid_cooldown_, [this]() {
73 ESP_LOGV(TAG, "Multi Click: Cooldown ended, matching is now enabled again.");
74 this->is_in_cooldown_ = false;
75 });
76 this->at_index_.reset();
77 this->cancel_timeout("trigger");
78 this->cancel_timeout("is_valid");
79 this->cancel_timeout("is_not_valid");
80}
81void MultiClickTrigger::schedule_is_valid_(uint32_t min_length) {
82 if (min_length == 0) {
83 this->is_valid_ = true;
84 return;
85 }
86 this->is_valid_ = false;
87 this->set_timeout("is_valid", min_length, [this]() {
88 ESP_LOGV(TAG, "Multi Click: You can now %s the button.", this->parent_->state ? "RELEASE" : "PRESS");
89 this->is_valid_ = true;
90 });
91}
93 this->set_timeout("is_not_valid", max_length, [this]() {
94 ESP_LOGV(TAG, "Multi Click: You waited too long to %s.", this->parent_->state ? "RELEASE" : "PRESS");
95 this->is_valid_ = false;
96 this->schedule_cooldown_();
97 });
98}
100 ESP_LOGV(TAG, "Multi Click: Sequence explicitly cancelled.");
101 this->is_valid_ = false;
102 this->schedule_cooldown_();
103}
105 ESP_LOGV(TAG, "Multi Click: Hooray, multi click is valid. Triggering!");
106 this->at_index_.reset();
107 this->cancel_timeout("trigger");
108 this->cancel_timeout("is_valid");
109 this->cancel_timeout("is_not_valid");
110 this->trigger();
111}
112
113bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length) {
114 if (max_length == 0) {
115 return length >= min_length;
116 } else {
117 return length >= min_length && length <= max_length;
118 }
119}
120} // namespace esphome::binary_sensor
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.
void trigger(const Ts &...x)
Definition automation.h:204
FixedVector< MultiClickTriggerEvent > timing_
Definition automation.h:117
void schedule_is_not_valid_(uint32_t max_length)
void schedule_is_valid_(uint32_t min_length)
bool has_value() const
Definition optional.h:92
bool state
Definition fan.h:0
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length)
uint16_t length
Definition tt21100.cpp:0