ESPHome 2026.5.1
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
5#include "valve.h"
6
7namespace esphome::valve {
8
9template<typename... Ts> class OpenAction : public Action<Ts...> {
10 public:
11 explicit OpenAction(Valve *valve) : valve_(valve) {}
12
13 void play(const Ts &...x) override { this->valve_->make_call().set_command_open().perform(); }
14
15 protected:
17};
18
19template<typename... Ts> class CloseAction : public Action<Ts...> {
20 public:
21 explicit CloseAction(Valve *valve) : valve_(valve) {}
22
23 void play(const Ts &...x) override { this->valve_->make_call().set_command_close().perform(); }
24
25 protected:
27};
28
29template<typename... Ts> class StopAction : public Action<Ts...> {
30 public:
31 explicit StopAction(Valve *valve) : valve_(valve) {}
32
33 void play(const Ts &...x) override { this->valve_->make_call().set_command_stop().perform(); }
34
35 protected:
37};
38
39template<typename... Ts> class ToggleAction : public Action<Ts...> {
40 public:
41 explicit ToggleAction(Valve *valve) : valve_(valve) {}
42
43 void play(const Ts &...x) override { this->valve_->make_call().set_command_toggle().perform(); }
44
45 protected:
47};
48
49// All configured fields are baked into a single stateless lambda whose
50// constants live in flash. The action only stores one function pointer
51// plus one parent pointer, regardless of how many fields the user set.
52// Trigger args are forwarded to the apply function so user lambdas
53// (e.g. `position: !lambda "return x;"`) keep working.
54//
55// Trigger args are normalized to `const std::remove_cvref_t<Ts> &...` so
56// the codegen can emit a matching parameter list for both the apply lambda
57// and any inner field lambdas without producing invalid C++ source text
58// (e.g. `const T & &` if Ts already carries a reference, or `const const
59// T &` if Ts already carries a const). This keeps trigger args no-copy
60// regardless of whether the trigger supplies `T`, `T &`, or `const T &`.
61template<typename... Ts> class ControlAction : public Action<Ts...> {
62 public:
63 using ApplyFn = void (*)(ValveCall &, const std::remove_cvref_t<Ts> &...);
65
66 void play(const Ts &...x) override {
67 auto call = this->valve_->make_call();
68 this->apply_(call, x...);
69 call.perform();
70 }
71
72 protected:
75};
76
77template<typename... Ts> class ValveIsOpenCondition : public Condition<Ts...> {
78 public:
79 ValveIsOpenCondition(Valve *valve) : valve_(valve) {}
80 bool check(const Ts &...x) override { return this->valve_->is_fully_open(); }
81
82 protected:
84};
85
86template<typename... Ts> class ValveIsClosedCondition : public Condition<Ts...> {
87 public:
89 bool check(const Ts &...x) override { return this->valve_->is_fully_closed(); }
90
91 protected:
93};
94
95class ValveOpenTrigger : public Trigger<> {
96 public:
97 ValveOpenTrigger(Valve *a_valve) : valve_(a_valve) {
98 a_valve->add_on_state_callback([this]() {
99 if (this->valve_->is_fully_open()) {
100 this->trigger();
101 }
102 });
103 }
104
105 protected:
107};
108
109class ValveClosedTrigger : public Trigger<> {
110 public:
111 ValveClosedTrigger(Valve *a_valve) : valve_(a_valve) {
112 a_valve->add_on_state_callback([this]() {
113 if (this->valve_->is_fully_closed()) {
114 this->trigger();
115 }
116 });
117 }
118
119 protected:
121};
122
123} // namespace esphome::valve
Base class for all automation conditions.
Definition automation.h:459
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:482
void play(const Ts &...x) override
Definition automation.h:23
ControlAction(Valve *valve, ApplyFn apply)
Definition automation.h:64
void play(const Ts &...x) override
Definition automation.h:66
void(*)(ValveCall &, const std::remove_cvref_t< Ts > &...) ApplyFn
Definition automation.h:63
void play(const Ts &...x) override
Definition automation.h:13
void play(const Ts &...x) override
Definition automation.h:33
void play(const Ts &...x) override
Definition automation.h:43
ValveCall & set_command_close()
Set the command to close the valve.
Definition valve.cpp:53
ValveCall & set_command_toggle()
Set the command to toggle the valve.
Definition valve.cpp:61
ValveCall & set_command_stop()
Set the command to stop the valve.
Definition valve.cpp:57
ValveCall & set_command_open()
Set the command to open the valve.
Definition valve.cpp:49
void perform()
Perform the valve call.
Definition valve.cpp:69
Base class for all valve devices.
Definition valve.h:103
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
void add_on_state_callback(F &&f)
Definition valve.h:119
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 make_call()
Construct a new valve call used to control the valve.
Definition valve.cpp:125
bool check(const Ts &...x) override
Definition automation.h:89
bool check(const Ts &...x) override
Definition automation.h:80
void apply(Climate *climate)
Apply these settings to the climate device.
uint16_t x
Definition tt21100.cpp:5