ESPHome  2024.11.0
automation.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "valve.h"
6 
7 namespace esphome {
8 namespace valve {
9 
10 template<typename... Ts> class OpenAction : public Action<Ts...> {
11  public:
12  explicit OpenAction(Valve *valve) : valve_(valve) {}
13 
14  void play(Ts... x) override { this->valve_->make_call().set_command_open().perform(); }
15 
16  protected:
18 };
19 
20 template<typename... Ts> class CloseAction : public Action<Ts...> {
21  public:
22  explicit CloseAction(Valve *valve) : valve_(valve) {}
23 
24  void play(Ts... x) override { this->valve_->make_call().set_command_close().perform(); }
25 
26  protected:
28 };
29 
30 template<typename... Ts> class StopAction : public Action<Ts...> {
31  public:
32  explicit StopAction(Valve *valve) : valve_(valve) {}
33 
34  void play(Ts... x) override { this->valve_->make_call().set_command_stop().perform(); }
35 
36  protected:
38 };
39 
40 template<typename... Ts> class ToggleAction : public Action<Ts...> {
41  public:
42  explicit ToggleAction(Valve *valve) : valve_(valve) {}
43 
44  void play(Ts... x) override { this->valve_->make_call().set_command_toggle().perform(); }
45 
46  protected:
48 };
49 
50 template<typename... Ts> class ControlAction : public Action<Ts...> {
51  public:
52  explicit ControlAction(Valve *valve) : valve_(valve) {}
53 
54  TEMPLATABLE_VALUE(bool, stop)
55  TEMPLATABLE_VALUE(float, position)
56 
57  void play(Ts... x) override {
58  auto call = this->valve_->make_call();
59  if (this->stop_.has_value())
60  call.set_stop(this->stop_.value(x...));
61  if (this->position_.has_value())
62  call.set_position(this->position_.value(x...));
63  call.perform();
64  }
65 
66  protected:
68 };
69 
70 template<typename... Ts> class ValvePublishAction : public Action<Ts...> {
71  public:
72  ValvePublishAction(Valve *valve) : valve_(valve) {}
73  TEMPLATABLE_VALUE(float, position)
74  TEMPLATABLE_VALUE(ValveOperation, current_operation)
75 
76  void play(Ts... x) override {
77  if (this->position_.has_value())
78  this->valve_->position = this->position_.value(x...);
79  if (this->current_operation_.has_value())
80  this->valve_->current_operation = this->current_operation_.value(x...);
81  this->valve_->publish_state();
82  }
83 
84  protected:
86 };
87 
88 template<typename... Ts> class ValveIsOpenCondition : public Condition<Ts...> {
89  public:
90  ValveIsOpenCondition(Valve *valve) : valve_(valve) {}
91  bool check(Ts... x) override { return this->valve_->is_fully_open(); }
92 
93  protected:
95 };
96 
97 template<typename... Ts> class ValveIsClosedCondition : public Condition<Ts...> {
98  public:
99  ValveIsClosedCondition(Valve *valve) : valve_(valve) {}
100  bool check(Ts... x) override { return this->valve_->is_fully_closed(); }
101 
102  protected:
104 };
105 
106 class ValveOpenTrigger : public Trigger<> {
107  public:
109  a_valve->add_on_state_callback([this, a_valve]() {
110  if (a_valve->is_fully_open()) {
111  this->trigger();
112  }
113  });
114  }
115 };
116 
117 class ValveClosedTrigger : public Trigger<> {
118  public:
120  a_valve->add_on_state_callback([this, a_valve]() {
121  if (a_valve->is_fully_closed()) {
122  this->trigger();
123  }
124  });
125  }
126 };
127 
128 } // namespace valve
129 } // namespace esphome
ValveCall & set_command_open()
Set the command to open the valve.
Definition: valve.cpp:51
ValveOperation
Enum encoding the current operation of a valve.
Definition: valve.h:75
virtual void stop()
Definition: automation.h:166
void play(Ts... x) override
Definition: automation.h:44
bool is_fully_closed() const
Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0...
Definition: valve.cpp:166
uint16_t x
Definition: tt21100.cpp:17
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition: valve.h:116
void add_on_state_callback(std::function< void()> &&f)
Definition: valve.cpp:129
OpenAction(Valve *valve)
Definition: automation.h:12
CloseAction(Valve *valve)
Definition: automation.h:22
bool check(Ts... x) override
Definition: automation.h:91
ValveCall & set_command_stop()
Set the command to stop the valve.
Definition: valve.cpp:59
ValveCall & set_command_toggle()
Set the command to toggle the valve.
Definition: valve.cpp:63
ValveCall make_call()
Construct a new valve call used to control the valve.
Definition: valve.cpp:127
ValveCall & set_command_close()
Set the command to close the valve.
Definition: valve.cpp:55
void perform()
Perform the valve call.
Definition: valve.cpp:71
void play(Ts... x) override
Definition: automation.h:14
Base class for all automation conditions.
Definition: automation.h:74
bool is_fully_open() const
Helper method to check if the valve is fully open. Equivalent to comparing .position against 1...
Definition: valve.cpp:165
ValveCall & set_stop(bool stop)
Set whether this valve call should stop the valve.
Definition: valve.cpp:121
StopAction(Valve *valve)
Definition: automation.h:32
ToggleAction(Valve *valve)
Definition: automation.h:42
void play(Ts... x) override
Definition: automation.h:24
ValveOpenTrigger(Valve *a_valve)
Definition: automation.h:108
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all valve devices.
Definition: valve.h:105
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
Definition: valve.h:110
void publish_state(bool save=true)
Publish the current state of the valve.
Definition: valve.cpp:130
float position
Definition: cover.h:14
ValveCall & set_position(float position)
Set the call to a certain target position.
Definition: valve.cpp:67
void play(Ts... x) override
Definition: automation.h:34