ESPHome  2023.5.5
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=%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  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: 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");
106  this->trigger();
107 }
108 
109 bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length) {
110  if (max_length == 0) {
111  return length >= min_length;
112  } else {
113  return length >= min_length && length <= max_length;
114  }
115 }
116 } // namespace binary_sensor
117 } // namespace esphome
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length)
Definition: automation.cpp:109
void schedule_is_not_valid_(uint32_t max_length)
Definition: automation.cpp:93
void schedule_is_valid_(uint32_t min_length)
Definition: automation.cpp:82
Definition: a4988.cpp:4
bool state
Definition: fan.h:34