ESPHome  1.15.3
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 *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=%u max=%u", 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=%u min=%u max=%u", *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=%u min=%u", *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=%u min=%u", *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 %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;
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  this->is_valid_ = false;
84  this->set_timeout("is_valid", min_length, [this]() {
85  ESP_LOGV(TAG, "Multi Click: You can now %s the button.", this->parent_->state ? "RELEASE" : "PRESS");
86  this->is_valid_ = true;
87  });
88 }
90  this->set_timeout("is_not_valid", max_length, [this]() {
91  ESP_LOGV(TAG, "Multi Click: You waited too long to %s.", this->parent_->state ? "RELEASE" : "PRESS");
92  this->is_valid_ = false;
93  this->schedule_cooldown_();
94  });
95 }
97  ESP_LOGV(TAG, "Multi Click: Hooray, multi click is valid. Triggering!");
98  this->at_index_.reset();
99  this->cancel_timeout("trigger");
100  this->cancel_timeout("is_valid");
101  this->cancel_timeout("is_not_valid");
102  this->trigger();
103 }
104 
105 bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length) {
106  if (max_length == 0) {
107  return length >= min_length;
108  } else {
109  return length >= min_length && length <= max_length;
110  }
111 }
112 } // namespace binary_sensor
113 } // namespace esphome
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length)
Definition: automation.cpp:105
void schedule_is_not_valid_(uint32_t max_length)
Definition: automation.cpp:89
void schedule_is_valid_(uint32_t min_length)
Definition: automation.cpp:82
Definition: a4988.cpp:4