ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
3#include <cinttypes>
4#include <utility>
5
8#include "esphome/core/hal.h"
11
12namespace esphome::binary_sensor {
13
15 bool state;
16 uint32_t min_length;
17 uint32_t max_length;
18};
19
20class 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
30class 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
40bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length);
41
42class 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
63class 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
92class MultiClickTrigger : public Trigger<>, public Component {
93 public:
94 explicit MultiClickTrigger(BinarySensor *parent, std::initializer_list<MultiClickTriggerEvent> timing)
95 : parent_(parent), timing_(timing) {}
96
97 void setup() override {
98 this->last_state_ = this->parent_->get_state_default(false);
99 auto f = std::bind(&MultiClickTrigger::on_state_, this, std::placeholders::_1);
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();
108
109 protected:
110 void on_state_(bool state);
111 void schedule_cooldown_();
112 void schedule_is_valid_(uint32_t min_length);
113 void schedule_is_not_valid_(uint32_t max_length);
114 void trigger_();
115
118 uint32_t invalid_cooldown_{1000};
120 bool last_state_{false};
121 bool is_in_cooldown_{false};
122 bool is_valid_{false};
123};
124
125class StateTrigger : public Trigger<bool> {
126 public:
127 explicit StateTrigger(BinarySensor *parent) {
128 parent->add_on_state_callback([this](bool state) { this->trigger(state); });
129 }
130};
131
132class StateChangeTrigger : public Trigger<optional<bool>, optional<bool> > {
133 public:
136 [this](optional<bool> old_state, optional<bool> state) { this->trigger(old_state, state); });
137 }
138};
139
140template<typename... Ts> class BinarySensorCondition : public Condition<Ts...> {
141 public:
143 bool check(const Ts &...x) override { return this->parent_->state == this->state_; }
144
145 protected:
147 bool state_;
148};
149
150template<typename... Ts> class BinarySensorPublishAction : public Action<Ts...> {
151 public:
152 explicit BinarySensorPublishAction(BinarySensor *sensor) : sensor_(sensor) {}
154
155 void play(const Ts &...x) override {
156 auto val = this->state_.value(x...);
157 this->sensor_->publish_state(val);
158 }
159
160 protected:
162};
163
164template<typename... Ts> class BinarySensorInvalidateAction : public Action<Ts...> {
165 public:
166 explicit BinarySensorInvalidateAction(BinarySensor *sensor) : sensor_(sensor) {}
167
168 void play(const Ts &...x) override { this->sensor_->invalidate_state(); }
169
170 protected:
172};
173
174} // namespace esphome::binary_sensor
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:183
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:184
void add_full_state_callback(std::function< void(optional< T > previous, optional< T > current)> &&callback)
void add_on_state_callback(std::function< void(T)> &&callback)
virtual T get_state_default(T default_value) const
void trigger(const Ts &...x)
Definition automation.h:204
BinarySensorCondition(BinarySensor *parent, bool state)
Definition automation.h:142
bool check(const Ts &...x) override
Definition automation.h:143
Base class for all binary_sensor-type classes.
void publish_state(bool new_state)
Publish a new state to the front-end.
TEMPLATABLE_VALUE(bool, state) void play(const Ts &...x) override
Definition automation.h:153
uint32_t min_length_
The millis() time when the click started.
Definition automation.h:59
uint32_t max_length_
Minimum length of click. 0 means no minimum.
Definition automation.h:60
ClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
Definition automation.h:44
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:89
void set_invalid_cooldown(uint32_t invalid_cooldown)
Definition automation.h:105
FixedVector< MultiClickTriggerEvent > timing_
Definition automation.h:117
void schedule_is_not_valid_(uint32_t max_length)
float get_setup_priority() const override
Definition automation.h:103
void schedule_is_valid_(uint32_t min_length)
MultiClickTrigger(BinarySensor *parent, std::initializer_list< MultiClickTriggerEvent > timing)
Definition automation.h:94
PressTrigger(BinarySensor *parent)
Definition automation.h:22
ReleaseTrigger(BinarySensor *parent)
Definition automation.h:32
StateTrigger(BinarySensor *parent)
Definition automation.h:127
bool state
Definition fan.h:0
mopeka_std_values val[4]
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length)
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:80
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5