ESPHome  2024.10.2
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  void cancel();
109 
110  protected:
111  void on_state_(bool state);
112  void schedule_cooldown_();
113  void schedule_is_valid_(uint32_t min_length);
114  void schedule_is_not_valid_(uint32_t max_length);
115  void trigger_();
116 
118  std::vector<MultiClickTriggerEvent> timing_;
119  uint32_t invalid_cooldown_{1000};
120  optional<size_t> at_index_{};
121  bool last_state_{false};
122  bool is_in_cooldown_{false};
123  bool is_valid_{false};
124 };
125 
126 class StateTrigger : public Trigger<bool> {
127  public:
128  explicit StateTrigger(BinarySensor *parent) {
129  parent->add_on_state_callback([this](bool state) { this->trigger(state); });
130  }
131 };
132 
133 template<typename... Ts> class BinarySensorCondition : public Condition<Ts...> {
134  public:
135  BinarySensorCondition(BinarySensor *parent, bool state) : parent_(parent), state_(state) {}
136  bool check(Ts... x) override { return this->parent_->state == this->state_; }
137 
138  protected:
140  bool state_;
141 };
142 
143 template<typename... Ts> class BinarySensorPublishAction : public Action<Ts...> {
144  public:
145  explicit BinarySensorPublishAction(BinarySensor *sensor) : sensor_(sensor) {}
147 
148  void play(Ts... x) override {
149  auto val = this->state_.value(x...);
150  this->sensor_->publish_state(val);
151  }
152 
153  protected:
155 };
156 
157 } // namespace binary_sensor
158 } // 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:115
std::vector< MultiClickTriggerEvent > timing_
Definition: automation.h:118
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:128
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
esphome::binary_sensor::BinarySensor * binary_sensor
Definition: statsd.h:40
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
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:135
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:146
esphome::sensor::Sensor * sensor
Definition: statsd.h:37