ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
3#include <cinttypes>
4#include <type_traits>
5#include <utility>
6
9#include "esphome/core/hal.h"
12
13namespace esphome::binary_sensor {
14
16 uint32_t state; // Not bool: all-word fields allow aligned loads from PROGMEM on ESP8266
19};
20// Read straight from PROGMEM; a byte-sized field would fault on ESP8266.
21static_assert(std::is_same_v<decltype(MultiClickTriggerEvent::state), uint32_t> &&
22 sizeof(MultiClickTriggerEvent) == 3 * sizeof(uint32_t),
23 "MultiClickTriggerEvent fields must all be uint32_t");
24
25bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length);
26
27class ClickTrigger final : public Trigger<> {
28 public:
29 explicit ClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
30 : min_length_(min_length), max_length_(max_length) {
31 parent->add_on_state_callback([this](bool state) {
32 if (state) {
33 this->start_time_ = millis();
34 } else {
35 const uint32_t length = millis() - this->start_time_;
36 if (match_interval(this->min_length_, this->max_length_, length))
37 this->trigger();
38 }
39 });
40 }
41
42 protected:
43 uint32_t start_time_{0};
46};
47
48class DoubleClickTrigger final : public Trigger<> {
49 public:
50 explicit DoubleClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
51 : min_length_(min_length), max_length_(max_length) {
52 parent->add_on_state_callback([this](bool state) {
53 const uint32_t now = millis();
54
55 if (state && this->start_time_ != 0 && this->end_time_ != 0) {
56 if (match_interval(this->min_length_, this->max_length_, this->end_time_ - this->start_time_) &&
57 match_interval(this->min_length_, this->max_length_, now - this->end_time_)) {
58 this->trigger();
59 this->start_time_ = 0;
60 this->end_time_ = 0;
61 return;
62 }
63 }
64
65 this->start_time_ = this->end_time_;
66 this->end_time_ = now;
67 });
68 }
69
70 protected:
75};
76
77class MultiClickTrigger final : public Trigger<>, public Component {
78 public:
80 MultiClickTrigger(BinarySensor *parent, const MultiClickTriggerEvent *timing, uint8_t timing_count)
81 : parent_(parent), timing_(timing), timing_count_(timing_count) {}
82
83 void setup() override {
84 this->last_state_ = this->parent_->get_state_default(false);
85 this->parent_->add_on_state_callback([this](bool state) { this->on_state_(state); });
86 }
87
88 float get_setup_priority() const override { return setup_priority::HARDWARE; }
89
90 void set_invalid_cooldown(uint32_t invalid_cooldown) { this->invalid_cooldown_ = invalid_cooldown; }
91
92 void cancel();
95
96 protected:
97 void on_state_(bool state);
98 void schedule_cooldown_();
99 void schedule_is_valid_(uint32_t min_length);
100 void schedule_is_not_valid_(uint32_t max_length);
101 void trigger_();
102
105 uint32_t invalid_cooldown_{1000}; // Must match DEFAULT_INVALID_COOLDOWN_MS in __init__.py
107 uint8_t at_index_{0}; // 0: not matching, otherwise the index of the next timing event
108 bool last_state_{false};
109 bool is_in_cooldown_{false};
110 bool is_valid_{false};
111};
112
113} // namespace esphome::binary_sensor
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:556
Base class for all binary_sensor-type classes.
uint32_t min_length_
The millis() time when the click started.
Definition automation.h:44
uint32_t max_length_
Minimum length of click. 0 means no minimum.
Definition automation.h:45
ClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
Definition automation.h:29
DoubleClickTrigger(BinarySensor *parent, uint32_t min_length, uint32_t max_length)
Definition automation.h:50
uint32_t max_length_
Minimum length of click. 0 means no minimum.
Definition automation.h:74
const MultiClickTriggerEvent * timing_
Definition automation.h:104
void set_invalid_cooldown(uint32_t invalid_cooldown)
Definition automation.h:90
void schedule_is_not_valid_(uint32_t max_length)
MultiClickTrigger(BinarySensor *parent, const MultiClickTriggerEvent *timing, uint8_t timing_count)
timing is a codegen PROGMEM table shared by triggers with the same timing.
Definition automation.h:80
float get_setup_priority() const override
Definition automation.h:88
void schedule_is_valid_(uint32_t min_length)
MultiClickTrigger(const MultiClickTrigger &)=delete
MultiClickTrigger & operator=(const MultiClickTrigger &)=delete
bool state
Definition fan.h:2
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:43
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t
uint16_t length
Definition tt21100.cpp:0