ESPHome 2026.5.1
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
5#include "cover.h"
6
7namespace esphome::cover {
8
9template<typename... Ts> class OpenAction : public Action<Ts...> {
10 public:
11 explicit OpenAction(Cover *cover) : cover_(cover) {}
12
13 void play(const Ts &...x) override { this->cover_->make_call().set_command_open().perform(); }
14
15 protected:
17};
18
19template<typename... Ts> class CloseAction : public Action<Ts...> {
20 public:
21 explicit CloseAction(Cover *cover) : cover_(cover) {}
22
23 void play(const Ts &...x) override { this->cover_->make_call().set_command_close().perform(); }
24
25 protected:
27};
28
29template<typename... Ts> class StopAction : public Action<Ts...> {
30 public:
31 explicit StopAction(Cover *cover) : cover_(cover) {}
32
33 void play(const Ts &...x) override { this->cover_->make_call().set_command_stop().perform(); }
34
35 protected:
37};
38
39template<typename... Ts> class ToggleAction : public Action<Ts...> {
40 public:
41 explicit ToggleAction(Cover *cover) : cover_(cover) {}
42
43 void play(const Ts &...x) override { this->cover_->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. Each action stores only 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 &`.
61
62template<typename... Ts> class ControlAction : public Action<Ts...> {
63 public:
64 using ApplyFn = void (*)(CoverCall &, const std::remove_cvref_t<Ts> &...);
66
67 void play(const Ts &...x) override {
68 auto call = this->cover_->make_call();
69 this->apply_(call, x...);
70 call.perform();
71 }
72
73 protected:
76};
77
78template<typename... Ts> class CoverPublishAction : public Action<Ts...> {
79 public:
80 using ApplyFn = void (*)(Cover *, const std::remove_cvref_t<Ts> &...);
82
83 void play(const Ts &...x) override {
84 this->apply_(this->cover_, x...);
85 this->cover_->publish_state();
86 }
87
88 protected:
91};
92
93template<bool OPEN, typename... Ts> class CoverPositionCondition : public Condition<Ts...> {
94 public:
96
97 bool check(const Ts &...x) override { return this->cover_->position == (OPEN ? COVER_OPEN : COVER_CLOSED); }
98
99 protected:
101};
102
103template<typename... Ts> using CoverIsOpenCondition = CoverPositionCondition<true, Ts...>;
104template<typename... Ts> using CoverIsClosedCondition = CoverPositionCondition<false, Ts...>;
105
106template<bool OPEN> class CoverPositionTrigger : public Trigger<> {
107 public:
108 CoverPositionTrigger(Cover *a_cover) : cover_(a_cover) {
109 a_cover->add_on_state_callback([this]() {
110 if (this->cover_->position != this->last_position_) {
111 this->last_position_ = this->cover_->position;
112 if (this->cover_->position == (OPEN ? COVER_OPEN : COVER_CLOSED))
113 this->trigger();
114 }
115 });
116 }
117
118 protected:
120 float last_position_{NAN};
121};
122
125
126template<CoverOperation OP> class CoverTrigger : public Trigger<> {
127 public:
128 CoverTrigger(Cover *a_cover) : cover_(a_cover) {
129 a_cover->add_on_state_callback([this]() {
130 auto current_op = this->cover_->current_operation;
131 if (current_op == OP) {
132 if (!this->last_operation_.has_value() || this->last_operation_.value() != OP) {
133 this->trigger();
134 }
135 }
136 this->last_operation_ = current_op;
137 });
138 }
139
140 protected:
142 optional<CoverOperation> last_operation_{};
143};
144} // namespace esphome::cover
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
void(*)(CoverCall &, const std::remove_cvref_t< Ts > &...) ApplyFn
Definition automation.h:64
ControlAction(Cover *cover, ApplyFn apply)
Definition automation.h:65
void play(const Ts &...x) override
Definition automation.h:67
CoverCall & set_command_toggle()
Set the command to toggle the cover.
Definition cover.cpp:58
CoverCall & set_command_open()
Set the command to open the cover.
Definition cover.cpp:46
CoverCall & set_command_close()
Set the command to close the cover.
Definition cover.cpp:50
void perform()
Perform the cover call.
Definition cover.cpp:70
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition cover.cpp:54
Base class for all cover devices.
Definition cover.h:110
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:115
void add_on_state_callback(F &&f)
Definition cover.h:128
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:142
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition cover.cpp:140
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:121
bool check(const Ts &...x) override
Definition automation.h:97
CoverPublishAction(Cover *cover, ApplyFn apply)
Definition automation.h:81
void(*)(Cover *, const std::remove_cvref_t< Ts > &...) ApplyFn
Definition automation.h:80
void play(const Ts &...x) override
Definition automation.h:83
optional< CoverOperation > last_operation_
Definition automation.h:142
CoverTrigger(Cover *a_cover)
Definition automation.h:128
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
void apply(Climate *climate)
Apply these settings to the climate device.
uint16_t x
Definition tt21100.cpp:5