ESPHome  2024.12.2
automation.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace switch_ {
9 
10 template<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 
20 template<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 
30 template<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 
40 template<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 
50 class 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 
61 class 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 
72 template<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
Base class for all switches.
Definition: switch.h:39
SwitchTurnOffTrigger(Switch *a_switch)
Definition: automation.h:63
uint16_t x
Definition: tt21100.cpp:17
void play(Ts... x) override
Definition: automation.h:14
void play(Ts... x) override
Definition: automation.h:34
SwitchPublishAction(Switch *a_switch)
Definition: automation.h:74
void play(Ts... x) override
Definition: automation.h:24
TurnOffAction(Switch *a_switch)
Definition: automation.h:22
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition: switch.cpp:60
Base class for all automation conditions.
Definition: automation.h:74
void turn_on()
Turn this switch on.
Definition: switch.cpp:11
TEMPLATABLE_VALUE(bool, state) void play(Ts... x) override
Definition: automation.h:75
void toggle()
Toggle this switch.
Definition: switch.cpp:19
SwitchTurnOnTrigger(Switch *a_switch)
Definition: automation.h:52
TurnOnAction(Switch *a_switch)
Definition: automation.h:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool check(Ts... x) override
Definition: automation.h:43
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:47
ToggleAction(Switch *a_switch)
Definition: automation.h:32
SwitchCondition(Switch *parent, bool state)
Definition: automation.h:42
bool state
Definition: fan.h:34
void turn_off()
Turn this switch off.
Definition: switch.cpp:15