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