ESPHome  2023.8.3
automation.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include <vector>
5 
8 #include "esphome/core/hal.h"
10 
11 namespace esphome {
12 namespace binary_sensor {
13 
15  bool state;
16  uint32_t min_length;
17  uint32_t max_length;
18 };
19 
20 class PressTrigger : public Trigger<> {
21  public:
22  explicit PressTrigger(BinarySensor *parent) {
23  parent->add_on_state_callback([this](bool state) {
24  if (state)
25  this->trigger();
26  });
27  }
28 };
29 
30 class ReleaseTrigger : public Trigger<> {
31  public:
32  explicit ReleaseTrigger(BinarySensor *parent) {
33  parent->add_on_state_callback([this](bool state) {
34  if (!state)
35  this->trigger();
36  });
37  }
38 };
39 
40 bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length);
41 
42 class ClickTrigger : public Trigger<> {
43  public:
44  explicit ClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
45  : min_length_(min_length), max_length_(max_length) {
46  parent->add_on_state_callback([this](bool state) {
47  if (state) {
48  this->start_time_ = millis();
49  } else {
50  const uint32_t length = millis() - this->start_time_;
51  if (match_interval(this->min_length_, this->max_length_, length))
52  this->trigger();
53  }
54  });
55  }
56 
57  protected:
58  uint32_t start_time_{0};
59  uint32_t min_length_;
60  uint32_t max_length_;
61 };
62 
63 class DoubleClickTrigger : public Trigger<> {
64  public:
65  explicit DoubleClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
66  : min_length_(min_length), max_length_(max_length) {
67  parent->add_on_state_callback([this](bool state) {
68  const uint32_t now = millis();
69 
70  if (state && this->start_time_ != 0 && this->end_time_ != 0) {
71  if (match_interval(this->min_length_, this->max_length_, this->end_time_ - this->start_time_) &&
72  match_interval(this->min_length_, this->max_length_, now - this->end_time_)) {
73  this->trigger();
74  this->start_time_ = 0;
75  this->end_time_ = 0;
76  return;
77  }
78  }
79 
80  this->start_time_ = this->end_time_;
81  this->end_time_ = now;
82  });
83  }
84 
85  protected:
86  uint32_t start_time_{0};
87  uint32_t end_time_{0};
88  uint32_t min_length_;
89  uint32_t max_length_;
90 };
91 
92 class MultiClickTrigger : public Trigger<>, public Component {
93  public:
94  explicit MultiClickTrigger(BinarySensor *parent, std::vector<MultiClickTriggerEvent> timing)
95  : parent_(parent), timing_(std::move(timing)) {}
96 
97  void setup() override {
98  this->last_state_ = this->parent_->state;
99  auto f = std::bind(&MultiClickTrigger::on_state_, this, std::placeholders::_1);
100  this->parent_->add_on_state_callback(f);
101  }
102 
103  float get_setup_priority() const override { return setup_priority::HARDWARE; }
104 
105  void set_invalid_cooldown(uint32_t invalid_cooldown) { this->invalid_cooldown_ = invalid_cooldown; }
106 
107  protected:
108  void on_state_(bool state);
109  void schedule_cooldown_();
110  void schedule_is_valid_(uint32_t min_length);
111  void schedule_is_not_valid_(uint32_t max_length);
112  void trigger_();
113 
115  std::vector<MultiClickTriggerEvent> timing_;
116  uint32_t invalid_cooldown_{1000};
117  optional<size_t> at_index_{};
118  bool last_state_{false};
119  bool is_in_cooldown_{false};
120  bool is_valid_{false};
121 };
122 
123 class StateTrigger : public Trigger<bool> {
124  public:
125  explicit StateTrigger(BinarySensor *parent) {
126  parent->add_on_state_callback([this](bool state) { this->trigger(state); });
127  }
128 };
129 
130 template<typename... Ts> class BinarySensorCondition : public Condition<Ts...> {
131  public:
132  BinarySensorCondition(BinarySensor *parent, bool state) : parent_(parent), state_(state) {}
133  bool check(Ts... x) override { return this->parent_->state == this->state_; }
134 
135  protected:
137  bool state_;
138 };
139 
140 template<typename... Ts> class BinarySensorPublishAction : public Action<Ts...> {
141  public:
142  explicit BinarySensorPublishAction(BinarySensor *sensor) : sensor_(sensor) {}
144 
145  void play(Ts... x) override {
146  auto val = this->state_.value(x...);
147  this->sensor_->publish_state(val);
148  }
149 
150  protected:
152 };
153 
154 } // namespace binary_sensor
155 } // namespace esphome
ReleaseTrigger(BinarySensor *parent)
Definition: automation.h:32
DoubleClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
Definition: automation.h:65
uint32_t max_length_
Minimum length of click. 0 means no minimum.
Definition: automation.h:60
uint32_t max_length_
Minimum length of click. 0 means no minimum.
Definition: automation.h:89
float get_setup_priority() const override
Definition: automation.h:103
MultiClickTrigger(BinarySensor *parent, std::vector< MultiClickTriggerEvent > timing)
Definition: automation.h:94
uint16_t x
Definition: tt21100.cpp:17
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length)
Definition: automation.cpp:109
std::vector< MultiClickTriggerEvent > timing_
Definition: automation.h:115
STL namespace.
mopeka_std_values val[4]
void set_invalid_cooldown(uint32_t invalid_cooldown)
Definition: automation.h:105
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:27
StateTrigger(BinarySensor *parent)
Definition: automation.h:125
ClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
Definition: automation.h:44
Base class for all automation conditions.
Definition: automation.h:74
uint32_t min_length_
The millis() time when the click started.
Definition: automation.h:59
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
PressTrigger(BinarySensor *parent)
Definition: automation.h:22
uint16_t length
Definition: tt21100.cpp:12
BinarySensorCondition(BinarySensor *parent, bool state)
Definition: automation.h:132
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
TEMPLATABLE_VALUE(bool, state) void play(Ts... x) override
Definition: automation.h:143