ESPHome  2024.10.2
automation.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "cover.h"
6 
7 namespace esphome {
8 namespace cover {
9 
10 template<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 
20 template<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 
30 template<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 
40 template<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 
50 template<typename... Ts> class ControlAction : public Action<Ts...> {
51  public:
52  explicit ControlAction(Cover *cover) : cover_(cover) {}
53 
54  TEMPLATABLE_VALUE(bool, stop)
55  TEMPLATABLE_VALUE(float, position)
56  TEMPLATABLE_VALUE(float, tilt)
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 
73 template<typename... Ts> class CoverPublishAction : public Action<Ts...> {
74  public:
75  CoverPublishAction(Cover *cover) : cover_(cover) {}
76  TEMPLATABLE_VALUE(float, position)
77  TEMPLATABLE_VALUE(float, tilt)
78  TEMPLATABLE_VALUE(CoverOperation, current_operation)
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 
94 template<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 
103 template<typename... Ts> class CoverIsClosedCondition : public Condition<Ts...> {
104  public:
105  CoverIsClosedCondition(Cover *cover) : cover_(cover) {}
106  bool check(Ts... x) override { return this->cover_->is_fully_closed(); }
107 
108  protected:
110 };
111 
112 class 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 
123 class 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
CoverCall & set_command_open()
Set the command to open the cover.
Definition: cover.cpp:51
void publish_state(bool save=true)
Publish the current state of the cover.
Definition: cover.cpp:166
Base class for all cover devices.
Definition: cover.h:111
virtual void stop()
Definition: automation.h:166
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition: cover.cpp:59
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition: cover.cpp:149
CoverOperation
Enum encoding the current operation of a cover.
Definition: cover.h:80
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition: cover.h:116
uint16_t x
Definition: tt21100.cpp:17
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
CoverCall & set_position(float position)
Set the call to a certain target position.
Definition: cover.cpp:67
CloseAction(Cover *cover)
Definition: automation.h:22
float tilt
Definition: cover.h:15
void perform()
Perform the cover call.
Definition: cover.cpp:75
CoverCall & set_stop(bool stop)
Set whether this cover call should stop the cover.
Definition: cover.cpp:143
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition: cover.h:124
CoverOpenTrigger(Cover *a_cover)
Definition: automation.h:114
void add_on_state_callback(std::function< void()> &&f)
Definition: cover.cpp:165
StopAction(Cover *cover)
Definition: automation.h:32
Base class for all automation conditions.
Definition: automation.h:74
CoverCall & set_command_toggle()
Set the command to toggle the cover.
Definition: cover.cpp:63
CoverCall & set_command_close()
Set the command to close the cover.
Definition: cover.cpp:55
ToggleAction(Cover *cover)
Definition: automation.h:42
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
void play(Ts... x) override
Definition: automation.h:24
OpenAction(Cover *cover)
Definition: automation.h:12
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition: cover.h:122
void play(Ts... x) override
Definition: automation.h:14
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void play(Ts... x) override
Definition: automation.h:44
float position
Definition: cover.h:14
bool check(Ts... x) override
Definition: automation.h:97
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition: cover.cpp:71
void play(Ts... x) override
Definition: automation.h:34