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