ESPHome 2026.1.3
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,
25 "START min=%" PRIu32 " max=%" PRIu32 "\n"
26 "Multi Click: Starting multi click action!",
27 evt.min_length, evt.max_length);
28 this->at_index_ = 1;
29 if (this->timing_.size() == 1 && evt.max_length == 4294967294UL) {
30 this->set_timeout("trigger", evt.min_length, [this]() { this->trigger_(); });
31 } else {
34 }
35 } else {
36 ESP_LOGV(TAG, "Multi Click: action not started because first level does not match!");
37 }
38
39 return;
40 }
41
42 if (!this->is_valid_) {
43 this->schedule_cooldown_();
44 return;
45 }
46
47 if (*this->at_index_ == this->timing_.size()) {
48 this->trigger_();
49 return;
50 }
51
52 MultiClickTriggerEvent evt = this->timing_[*this->at_index_];
53
54 if (evt.max_length != 4294967294UL) {
55 ESP_LOGV(TAG, "A i=%zu min=%" PRIu32 " max=%" PRIu32, *this->at_index_, evt.min_length, evt.max_length); // NOLINT
58 } else if (*this->at_index_ + 1 != this->timing_.size()) {
59 ESP_LOGV(TAG, "B i=%zu min=%" PRIu32, *this->at_index_, evt.min_length); // NOLINT
60 this->cancel_timeout("is_not_valid");
62 } else {
63 ESP_LOGV(TAG, "C i=%zu min=%" PRIu32, *this->at_index_, evt.min_length); // NOLINT
64 this->is_valid_ = false;
65 this->cancel_timeout("is_not_valid");
66 this->set_timeout("trigger", evt.min_length, [this]() { this->trigger_(); });
67 }
68
69 *this->at_index_ = *this->at_index_ + 1;
70}
72 ESP_LOGV(TAG, "Multi Click: Invalid length of press, starting cooldown of %" PRIu32 " ms", this->invalid_cooldown_);
73 this->is_in_cooldown_ = true;
74 this->set_timeout("cooldown", this->invalid_cooldown_, [this]() {
75 ESP_LOGV(TAG, "Multi Click: Cooldown ended, matching is now enabled again.");
76 this->is_in_cooldown_ = false;
77 });
78 this->at_index_.reset();
79 this->cancel_timeout("trigger");
80 this->cancel_timeout("is_valid");
81 this->cancel_timeout("is_not_valid");
82}
83void MultiClickTrigger::schedule_is_valid_(uint32_t min_length) {
84 if (min_length == 0) {
85 this->is_valid_ = true;
86 return;
87 }
88 this->is_valid_ = false;
89 this->set_timeout("is_valid", min_length, [this]() {
90 ESP_LOGV(TAG, "Multi Click: You can now %s the button.", this->parent_->state ? "RELEASE" : "PRESS");
91 this->is_valid_ = true;
92 });
93}
95 this->set_timeout("is_not_valid", max_length, [this]() {
96 ESP_LOGV(TAG, "Multi Click: You waited too long to %s.", this->parent_->state ? "RELEASE" : "PRESS");
97 this->is_valid_ = false;
98 this->schedule_cooldown_();
99 });
100}
102 ESP_LOGV(TAG, "Multi Click: Sequence explicitly cancelled.");
103 this->is_valid_ = false;
104 this->schedule_cooldown_();
105}
107 ESP_LOGV(TAG, "Multi Click: Hooray, multi click is valid. Triggering!");
108 this->at_index_.reset();
109 this->cancel_timeout("trigger");
110 this->cancel_timeout("is_valid");
111 this->cancel_timeout("is_not_valid");
112 this->trigger();
113}
114
115bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length) {
116 if (max_length == 0) {
117 return length >= min_length;
118 } else {
119 return length >= min_length && length <= max_length;
120 }
121}
122} // namespace esphome::binary_sensor
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:445
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_timeout(const std boo cancel_timeout)(const char *name)
Cancel a timeout function.
Definition component.h:465
void trigger(const Ts &...x)
Definition automation.h:238
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