ESPHome 2025.12.2
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<typename... Ts> class CoverIsOpenCondition : public Condition<Ts...> {
94 public:
95 CoverIsOpenCondition(Cover *cover) : cover_(cover) {}
96 bool check(const Ts &...x) override { return this->cover_->is_fully_open(); }
97
98 protected:
100};
101
102template<typename... Ts> class CoverIsClosedCondition : public Condition<Ts...> {
103 public:
105 bool check(const Ts &...x) override { return this->cover_->is_fully_closed(); }
106
107 protected:
109};
110
111class CoverOpenTrigger : public Trigger<> {
112 public:
114 a_cover->add_on_state_callback([this, a_cover]() {
115 if (a_cover->is_fully_open()) {
116 this->trigger();
117 }
118 });
119 }
120};
121
122class CoverClosedTrigger : public Trigger<> {
123 public:
125 a_cover->add_on_state_callback([this, a_cover]() {
126 if (a_cover->is_fully_closed()) {
127 this->trigger();
128 }
129 });
130 }
131};
132
133} // namespace esphome::cover
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:183
void trigger(const Ts &...x)
Definition automation.h:204
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:67
CoverCall & set_command_open()
Set the command to open the cover.
Definition cover.cpp:55
CoverCall & set_command_close()
Set the command to close the cover.
Definition cover.cpp:59
void perform()
Perform the cover call.
Definition cover.cpp:79
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition cover.cpp:63
CoverCall & set_stop(bool stop)
Set whether this cover call should stop the cover.
Definition cover.cpp:143
Base class for all cover devices.
Definition cover.h:112
void add_on_state_callback(std::function< void()> &&f)
Definition cover.cpp:151
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition cover.cpp:149
bool is_fully_closed() const
Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0....
Definition cover.cpp:198
bool is_fully_open() const
Helper method to check if the cover is fully open. Equivalent to comparing .position against 1....
Definition cover.cpp:197
bool check(const Ts &...x) override
Definition automation.h:105
bool check(const Ts &...x) override
Definition automation.h:96
TEMPLATABLE_VALUE(float, position) TEMPLATABLE_VALUE(float
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
float position
Definition cover.h:0
float tilt
Definition cover.h:1
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:81
uint16_t x
Definition tt21100.cpp:5