1 #include "automation.h" 5 namespace binary_sensor {
7 static const char *
const TAG =
"binary_sensor.automation";
11 if (state == this->last_state_) {
14 this->last_state_ =
state;
17 if (this->is_in_cooldown_) {
21 if (!this->at_index_.has_value()) {
24 if (evt.
state == state) {
26 ESP_LOGV(TAG,
"Multi Click: Starting multi click action!");
28 if (this->timing_.size() == 1 && evt.
max_length == 4294967294UL) {
29 this->set_timeout(
"trigger", evt.
min_length, [
this]() { this->trigger_(); });
35 ESP_LOGV(TAG,
"Multi Click: action not started because first level does not match!");
41 if (!this->is_valid_) {
42 this->schedule_cooldown_();
46 if (*this->at_index_ == this->timing_.size()) {
57 }
else if (*this->at_index_ + 1 != this->timing_.size()) {
58 ESP_LOGV(TAG,
"B i=%u min=%u", *this->at_index_, evt.
min_length);
59 this->cancel_timeout(
"is_not_valid");
62 ESP_LOGV(TAG,
"C i=%u min=%u", *this->at_index_, evt.
min_length);
63 this->is_valid_ =
false;
64 this->cancel_timeout(
"is_not_valid");
65 this->set_timeout(
"trigger", evt.
min_length, [
this]() { this->trigger_(); });
68 *this->at_index_ = *this->at_index_ + 1;
71 ESP_LOGV(TAG,
"Multi Click: Invalid length of press, starting cooldown of %u 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;
77 this->at_index_.reset();
78 this->cancel_timeout(
"trigger");
79 this->cancel_timeout(
"is_valid");
80 this->cancel_timeout(
"is_not_valid");
83 if (min_length == 0) {
84 this->is_valid_ =
true;
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;
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_();
101 ESP_LOGV(TAG,
"Multi Click: Hooray, multi click is valid. Triggering!");
102 this->at_index_.reset();
103 this->cancel_timeout(
"trigger");
104 this->cancel_timeout(
"is_valid");
105 this->cancel_timeout(
"is_not_valid");
110 if (max_length == 0) {
111 return length >= min_length;
113 return length >= min_length && length <= max_length;
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length)
void schedule_cooldown_()
void schedule_is_not_valid_(uint32_t max_length)
void on_state_(bool state)
void schedule_is_valid_(uint32_t min_length)