ESPHome 2026.2.4
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
49template<typename... Ts> class ControlAction : public Action<Ts...> {
50 public:
51 explicit ControlAction(Cover *cover) : cover_(cover) {}
52
56
57 void play(const Ts &...x) override {
58 auto call = this->cover_->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 if (this->tilt_.has_value())
64 call.set_tilt(this->tilt_.value(x...));
65 call.perform();
66 }
67
68 protected:
70};
71
72template<typename... Ts> class CoverPublishAction : public Action<Ts...> {
73 public:
74 CoverPublishAction(Cover *cover) : cover_(cover) {}
78
79 void play(const Ts &...x) override {
80 if (this->position_.has_value())
81 this->cover_->position = this->position_.value(x...);
82 if (this->tilt_.has_value())
83 this->cover_->tilt = this->tilt_.value(x...);
84 if (this->current_operation_.has_value())
85 this->cover_->current_operation = this->current_operation_.value(x...);
86 this->cover_->publish_state();
87 }
88
89 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:
109 a_cover->add_on_state_callback([this, a_cover]() {
110 if (a_cover->position != this->last_position_) {
111 this->last_position_ = a_cover->position;
112 if (a_cover->position == (OPEN ? COVER_OPEN : COVER_CLOSED))
113 this->trigger();
114 }
115 });
116 }
117
118 protected:
119 float last_position_{NAN};
120};
121
124
125template<CoverOperation OP> class CoverTrigger : public Trigger<> {
126 public:
127 CoverTrigger(Cover *a_cover) {
128 a_cover->add_on_state_callback([this, a_cover]() {
129 auto current_op = a_cover->current_operation;
130 if (current_op == OP) {
131 if (!this->last_operation_.has_value() || this->last_operation_.value() != OP) {
132 this->trigger();
133 }
134 }
135 this->last_operation_ = current_op;
136 });
137 }
138
139 protected:
141};
142} // namespace esphome::cover
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:258
void trigger(const Ts &...x)
Definition automation.h:279
void play(const Ts &...x) override
Definition automation.h:23
TEMPLATABLE_VALUE(bool, stop) TEMPLATABLE_VALUE(float
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_position(float position)
Set the call to a certain target position.
Definition cover.cpp:62
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition cover.cpp:54
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition cover.cpp:66
CoverCall & set_stop(bool stop)
Set whether this cover call should stop the cover.
Definition cover.cpp:134
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(std::function< void()> &&f)
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
TEMPLATABLE_VALUE(float, position) TEMPLATABLE_VALUE(float
optional< CoverOperation > last_operation_
Definition automation.h:140
CoverTrigger(Cover *a_cover)
Definition automation.h:127
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
bool has_value() const
Definition optional.h:92
float position
Definition cover.h:0
float tilt
Definition cover.h:1
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:79
if(written< 0)
Definition helpers.h:738
uint16_t x
Definition tt21100.cpp:5