ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
12namespace esphome {
13namespace binary_sensor {
14
16 bool state;
17 uint32_t min_length;
18 uint32_t max_length;
19};
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:
59 uint32_t start_time_{0};
60 uint32_t min_length_;
61 uint32_t max_length_;
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:
87 uint32_t start_time_{0};
88 uint32_t end_time_{0};
89 uint32_t min_length_;
90 uint32_t max_length_;
91};
92
93class 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);
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};
121 bool last_state_{false};
122 bool is_in_cooldown_{false};
123 bool is_valid_{false};
124};
125
126class 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
133template<typename... Ts> class BinarySensorCondition : public Condition<Ts...> {
134 public:
136 bool check(Ts... x) override { return this->parent_->state == this->state_; }
137
138 protected:
140 bool state_;
141};
142
143template<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
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:75
void trigger(Ts... x)
Definition automation.h:96
BinarySensorCondition(BinarySensor *parent, bool state)
Definition automation.h:135
Base class for all binary_sensor-type classes.
bool state
The current reported state of the binary sensor.
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
void publish_state(bool state)
Publish a new state to the front-end.
TEMPLATABLE_VALUE(bool, state) void play(Ts... x) override
Definition automation.h:146
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
void set_invalid_cooldown(uint32_t invalid_cooldown)
Definition automation.h:106
float get_setup_priority() const override
Definition automation.h:104
void schedule_is_valid_(uint32_t min_length)
MultiClickTrigger(BinarySensor *parent, std::vector< MultiClickTriggerEvent > timing)
Definition automation.h:95
void schedule_is_not_valid_(uint32_t max_length)
std::vector< MultiClickTriggerEvent > timing_
Definition automation.h:118
PressTrigger(BinarySensor *parent)
Definition automation.h:23
ReleaseTrigger(BinarySensor *parent)
Definition automation.h:33
StateTrigger(BinarySensor *parent)
Definition automation.h:128
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:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5