ESPHome  2024.9.2
automation.cpp
Go to the documentation of this file.
1 #include "automation.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace binary_sensor {
6 
7 static 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...",
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;
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 }
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 
115 bool 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 binary_sensor
123 } // namespace esphome
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length)
Definition: automation.cpp:115
void schedule_is_not_valid_(uint32_t max_length)
Definition: automation.cpp:94
esphome::binary_sensor::BinarySensor * binary_sensor
Definition: statsd.h:40
void schedule_is_valid_(uint32_t min_length)
Definition: automation.cpp:83
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool state
Definition: fan.h:34