ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cinttypes>
5#include <utility>
6
9#include "esphome/core/hal.h"
12
13namespace esphome::binary_sensor {
14
20
21class 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
31class 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
41bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length);
42
43class 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:
62};
63
64class 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:
91};
92
94class MultiClickTriggerBase : public Trigger<>, public Component {
95 public:
96 explicit MultiClickTriggerBase(BinarySensor *parent) : parent_(parent) {}
97
98 void setup() override {
99 this->last_state_ = this->parent_->get_state_default(false);
100 this->parent_->add_on_state_callback([this](bool state) { this->on_state_(state); });
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 void cancel();
110
111 protected:
112 void on_state_(bool state);
113 void schedule_cooldown_();
114 void schedule_is_valid_(uint32_t min_length);
115 void schedule_is_not_valid_(uint32_t max_length);
116 void trigger_();
117
121 optional<size_t> at_index_{};
122 uint8_t timing_count_{0};
123 bool last_state_{false};
124 bool is_in_cooldown_{false};
125 bool is_valid_{false};
126};
127
130template<size_t N> class MultiClickTrigger : public MultiClickTriggerBase {
131 public:
132 MultiClickTrigger(BinarySensor *parent, std::initializer_list<MultiClickTriggerEvent> timing)
133 : MultiClickTriggerBase(parent) {
134 init_array_from(this->timing_storage_, timing);
135 this->timing_ = this->timing_storage_.data();
136 this->timing_count_ = N;
137 }
138
139 protected:
140 std::array<MultiClickTriggerEvent, N> timing_storage_{};
141};
142
143class StateTrigger : public Trigger<bool> {
144 public:
145 explicit StateTrigger(BinarySensor *parent) {
146 parent->add_on_state_callback([this](bool state) { this->trigger(state); });
147 }
148};
149
150class StateChangeTrigger : public Trigger<optional<bool>, optional<bool> > {
151 public:
154 [this](optional<bool> old_state, optional<bool> state) { this->trigger(old_state, state); });
155 }
156};
157
158template<typename... Ts> class BinarySensorCondition : public Condition<Ts...> {
159 public:
161 bool check(const Ts &...x) override { return this->parent_->state == this->state_; }
162
163 protected:
165 bool state_;
166};
167
168template<typename... Ts> class BinarySensorPublishAction : public Action<Ts...> {
169 public:
170 explicit BinarySensorPublishAction(BinarySensor *sensor) : sensor_(sensor) {}
172
173 void play(const Ts &...x) override {
174 auto val = this->state_.value(x...);
175 this->sensor_->publish_state(val);
176 }
177
178 protected:
180};
181
182template<typename... Ts> class BinarySensorInvalidateAction : public Action<Ts...> {
183 public:
184 explicit BinarySensorInvalidateAction(BinarySensor *sensor) : sensor_(sensor) {}
185
186 void play(const Ts &...x) override { this->sensor_->invalidate_state(); }
187
188 protected:
190};
191
192} // namespace esphome::binary_sensor
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:459
void add_full_state_callback(F &&callback)
void invalidate_state()
Clear the state — sets has_state() to false and fires callbacks with nullopt.
void add_on_state_callback(F &&callback)
T get_state_default(T default_value) const
Return the current state if available, otherwise return the provided default.
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:482
BinarySensorCondition(BinarySensor *parent, bool state)
Definition automation.h:160
bool check(const Ts &...x) override
Definition automation.h:161
Base class for all binary_sensor-type classes.
void publish_state(bool new_state)
Publish a new state to the front-end.
bool state
The current state of this binary sensor. Also used as the backing storage for StatefulEntityBase.
TEMPLATABLE_VALUE(bool, state) void play(const Ts &...x) override
Definition automation.h:171
uint32_t min_length_
The millis() time when the click started.
Definition automation.h:60
uint32_t max_length_
Minimum length of click. 0 means no minimum.
Definition automation.h:61
ClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
Definition automation.h:45
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:90
Non-template base for MultiClickTrigger (keeps large method bodies out of the header).
Definition automation.h:94
MultiClickTriggerBase & operator=(const MultiClickTriggerBase &)=delete
void set_invalid_cooldown(uint32_t invalid_cooldown)
Definition automation.h:105
void schedule_is_not_valid_(uint32_t max_length)
const MultiClickTriggerEvent * timing_
Definition automation.h:119
void schedule_is_valid_(uint32_t min_length)
MultiClickTriggerBase(const MultiClickTriggerBase &)=delete
Template wrapper that provides inline std::array storage for timing events.
Definition automation.h:130
std::array< MultiClickTriggerEvent, N > timing_storage_
Definition automation.h:140
MultiClickTrigger(BinarySensor *parent, std::initializer_list< MultiClickTriggerEvent > timing)
Definition automation.h:132
PressTrigger(BinarySensor *parent)
Definition automation.h:23
ReleaseTrigger(BinarySensor *parent)
Definition automation.h:33
StateTrigger(BinarySensor *parent)
Definition automation.h:145
bool state
Definition fan.h:2
mopeka_std_values val[3]
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length)
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:40
void init_array_from(std::array< T, N > &dest, std::initializer_list< T > src)
Initialize a std::array from an initializer_list.
Definition helpers.h:505
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5