1 #include "automation.h" 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()) {
54 ESP_LOGV(TAG,
"A i=%zu min=%" PRIu32
" max=%" PRIu32, *this->at_index_, evt.
min_length, 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);
59 this->cancel_timeout(
"is_not_valid");
62 ESP_LOGV(TAG,
"C i=%zu min=%" PRIu32, *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 %" PRIu32
" ms...",
72 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;
78 this->at_index_.reset();
79 this->cancel_timeout(
"trigger");
80 this->cancel_timeout(
"is_valid");
81 this->cancel_timeout(
"is_not_valid");
84 if (min_length == 0) {
85 this->is_valid_ =
true;
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;
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_();
102 ESP_LOGV(TAG,
"Multi Click: Sequence explicitly cancelled.");
103 this->is_valid_ =
false;
104 this->schedule_cooldown_();
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");
116 if (max_length == 0) {
117 return length >= min_length;
119 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)
esphome::binary_sensor::BinarySensor * binary_sensor
void on_state_(bool state)
void schedule_is_valid_(uint32_t min_length)
Implementation of SPI Controller mode.