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