ESPHome 2025.5.0
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
5#include "cover.h"
6
7namespace esphome {
8namespace cover {
9
10template<typename... Ts> class OpenAction : public Action<Ts...> {
11 public:
12 explicit OpenAction(Cover *cover) : cover_(cover) {}
13
14 void play(Ts... x) override { this->cover_->make_call().set_command_open().perform(); }
15
16 protected:
18};
19
20template<typename... Ts> class CloseAction : public Action<Ts...> {
21 public:
22 explicit CloseAction(Cover *cover) : cover_(cover) {}
23
24 void play(Ts... x) override { this->cover_->make_call().set_command_close().perform(); }
25
26 protected:
28};
29
30template<typename... Ts> class StopAction : public Action<Ts...> {
31 public:
32 explicit StopAction(Cover *cover) : cover_(cover) {}
33
34 void play(Ts... x) override { this->cover_->make_call().set_command_stop().perform(); }
35
36 protected:
38};
39
40template<typename... Ts> class ToggleAction : public Action<Ts...> {
41 public:
42 explicit ToggleAction(Cover *cover) : cover_(cover) {}
43
44 void play(Ts... x) override { this->cover_->make_call().set_command_toggle().perform(); }
45
46 protected:
48};
49
50template<typename... Ts> class ControlAction : public Action<Ts...> {
51 public:
52 explicit ControlAction(Cover *cover) : cover_(cover) {}
53
57
58 void play(Ts... x) override {
59 auto call = this->cover_->make_call();
60 if (this->stop_.has_value())
61 call.set_stop(this->stop_.value(x...));
62 if (this->position_.has_value())
63 call.set_position(this->position_.value(x...));
64 if (this->tilt_.has_value())
65 call.set_tilt(this->tilt_.value(x...));
66 call.perform();
67 }
68
69 protected:
71};
72
73template<typename... Ts> class CoverPublishAction : public Action<Ts...> {
74 public:
75 CoverPublishAction(Cover *cover) : cover_(cover) {}
79
80 void play(Ts... x) override {
81 if (this->position_.has_value())
82 this->cover_->position = this->position_.value(x...);
83 if (this->tilt_.has_value())
84 this->cover_->tilt = this->tilt_.value(x...);
85 if (this->current_operation_.has_value())
86 this->cover_->current_operation = this->current_operation_.value(x...);
87 this->cover_->publish_state();
88 }
89
90 protected:
92};
93
94template<typename... Ts> class CoverIsOpenCondition : public Condition<Ts...> {
95 public:
96 CoverIsOpenCondition(Cover *cover) : cover_(cover) {}
97 bool check(Ts... x) override { return this->cover_->is_fully_open(); }
98
99 protected:
101};
102
103template<typename... Ts> class CoverIsClosedCondition : public Condition<Ts...> {
104 public:
106 bool check(Ts... x) override { return this->cover_->is_fully_closed(); }
107
108 protected:
110};
111
112class CoverOpenTrigger : public Trigger<> {
113 public:
115 a_cover->add_on_state_callback([this, a_cover]() {
116 if (a_cover->is_fully_open()) {
117 this->trigger();
118 }
119 });
120 }
121};
122
123class CoverClosedTrigger : public Trigger<> {
124 public:
126 a_cover->add_on_state_callback([this, a_cover]() {
127 if (a_cover->is_fully_closed()) {
128 this->trigger();
129 }
130 });
131 }
132};
133
134} // namespace cover
135} // namespace esphome
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:75
void trigger(Ts... x)
Definition automation.h:96
void play(Ts... x) override
Definition automation.h:24
TEMPLATABLE_VALUE(bool, stop) TEMPLATABLE_VALUE(float
CoverCall & set_command_toggle()
Set the command to toggle the cover.
Definition cover.cpp:63
CoverCall & set_command_open()
Set the command to open the cover.
Definition cover.cpp:51
CoverCall & set_command_close()
Set the command to close the cover.
Definition cover.cpp:55
void perform()
Perform the cover call.
Definition cover.cpp:75
CoverCall & set_position(float position)
Set the call to a certain target position.
Definition cover.cpp:67
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition cover.cpp:59
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition cover.cpp:71
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:111
void add_on_state_callback(std::function< void()> &&f)
Definition cover.cpp:165
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:209
bool is_fully_open() const
Helper method to check if the cover is fully open. Equivalent to comparing .position against 1....
Definition cover.cpp:208
bool check(Ts... x) override
Definition automation.h:97
TEMPLATABLE_VALUE(float, position) TEMPLATABLE_VALUE(float
void play(Ts... x) override
Definition automation.h:14
void play(Ts... x) override
Definition automation.h:34
void play(Ts... x) override
Definition automation.h:44
float position
Definition cover.h:0
float tilt
Definition cover.h:1
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:80
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5