ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
6
7namespace esphome {
8namespace switch_ {
9
10template<typename... Ts> class TurnOnAction : public Action<Ts...> {
11 public:
12 explicit TurnOnAction(Switch *a_switch) : switch_(a_switch) {}
13
14 void play(Ts... x) override { this->switch_->turn_on(); }
15
16 protected:
18};
19
20template<typename... Ts> class TurnOffAction : public Action<Ts...> {
21 public:
22 explicit TurnOffAction(Switch *a_switch) : switch_(a_switch) {}
23
24 void play(Ts... x) override { this->switch_->turn_off(); }
25
26 protected:
28};
29
30template<typename... Ts> class ToggleAction : public Action<Ts...> {
31 public:
32 explicit ToggleAction(Switch *a_switch) : switch_(a_switch) {}
33
34 void play(Ts... x) override { this->switch_->toggle(); }
35
36 protected:
38};
39
40template<typename... Ts> class ControlAction : public Action<Ts...> {
41 public:
42 explicit ControlAction(Switch *a_switch) : switch_(a_switch) {}
43
45
46 void play(Ts... x) override {
47 auto state = this->state_.optional_value(x...);
48 if (state.has_value()) {
49 this->switch_->control(*state);
50 }
51 }
52
53 protected:
55};
56
57template<typename... Ts> class SwitchCondition : public Condition<Ts...> {
58 public:
59 SwitchCondition(Switch *parent, bool state) : parent_(parent), state_(state) {}
60 bool check(Ts... x) override { return this->parent_->state == this->state_; }
61
62 protected:
64 bool state_;
65};
66
67class SwitchStateTrigger : public Trigger<bool> {
68 public:
70 a_switch->add_on_state_callback([this](bool state) { this->trigger(state); });
71 }
72};
73
74class SwitchTurnOnTrigger : public Trigger<> {
75 public:
77 a_switch->add_on_state_callback([this](bool state) {
78 if (state) {
79 this->trigger();
80 }
81 });
82 }
83};
84
85class SwitchTurnOffTrigger : public Trigger<> {
86 public:
88 a_switch->add_on_state_callback([this](bool state) {
89 if (!state) {
90 this->trigger();
91 }
92 });
93 }
94};
95
96template<typename... Ts> class SwitchPublishAction : public Action<Ts...> {
97 public:
98 SwitchPublishAction(Switch *a_switch) : switch_(a_switch) {}
100
101 void play(Ts... x) override { this->switch_->publish_state(this->state_.value(x...)); }
102
103 protected:
105};
106
107} // namespace switch_
108} // namespace esphome
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:124
ControlAction(Switch *a_switch)
Definition automation.h:42
TEMPLATABLE_VALUE(bool, state) void play(Ts... x) override
Definition automation.h:44
SwitchCondition(Switch *parent, bool state)
Definition automation.h:59
bool check(Ts... x) override
Definition automation.h:60
Base class for all switches.
Definition switch.h:39
void toggle()
Toggle this switch.
Definition switch.cpp:27
void turn_on()
Turn this switch on.
Definition switch.cpp:19
void turn_off()
Turn this switch off.
Definition switch.cpp:23
bool state
The current reported state of the binary sensor.
Definition switch.h:56
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition switch.cpp:68
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:55
void control(bool target_state)
Control this switch using a boolean state value.
Definition switch.cpp:11
TEMPLATABLE_VALUE(bool, state) void play(Ts... x) override
Definition automation.h:99
ToggleAction(Switch *a_switch)
Definition automation.h:32
void play(Ts... x) override
Definition automation.h:34
void play(Ts... x) override
Definition automation.h:24
TurnOffAction(Switch *a_switch)
Definition automation.h:22
TurnOnAction(Switch *a_switch)
Definition automation.h:12
void play(Ts... x) override
Definition automation.h:14
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5