ESPHome 2025.5.0
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 SwitchCondition : public Condition<Ts...> {
41 public:
42 SwitchCondition(Switch *parent, bool state) : parent_(parent), state_(state) {}
43 bool check(Ts... x) override { return this->parent_->state == this->state_; }
44
45 protected:
47 bool state_;
48};
49
50class SwitchTurnOnTrigger : public Trigger<> {
51 public:
53 a_switch->add_on_state_callback([this](bool state) {
54 if (state) {
55 this->trigger();
56 }
57 });
58 }
59};
60
61class SwitchTurnOffTrigger : public Trigger<> {
62 public:
64 a_switch->add_on_state_callback([this](bool state) {
65 if (!state) {
66 this->trigger();
67 }
68 });
69 }
70};
71
72template<typename... Ts> class SwitchPublishAction : public Action<Ts...> {
73 public:
74 SwitchPublishAction(Switch *a_switch) : switch_(a_switch) {}
76
77 void play(Ts... x) override { this->switch_->publish_state(this->state_.value(x...)); }
78
79 protected:
81};
82
83} // namespace switch_
84} // 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
SwitchCondition(Switch *parent, bool state)
Definition automation.h:42
bool check(Ts... x) override
Definition automation.h:43
Base class for all switches.
Definition switch.h:39
void toggle()
Toggle this switch.
Definition switch.cpp:19
void turn_on()
Turn this switch on.
Definition switch.cpp:11
void turn_off()
Turn this switch off.
Definition switch.cpp:15
bool state
The current reported state of the binary sensor.
Definition switch.h:53
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition switch.cpp:60
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:47
TEMPLATABLE_VALUE(bool, state) void play(Ts... x) override
Definition automation.h:75
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