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