ESPHome  2024.11.0
cover.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
7 #include "cover_traits.h"
8 
9 namespace esphome {
10 namespace cover {
11 
12 const extern float COVER_OPEN;
13 const extern float COVER_CLOSED;
14 
15 #define LOG_COVER(prefix, type, obj) \
16  if ((obj) != nullptr) { \
17  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
18  auto traits_ = (obj)->get_traits(); \
19  if (traits_.get_is_assumed_state()) { \
20  ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
21  } \
22  if (!(obj)->get_device_class().empty()) { \
23  ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
24  } \
25  }
26 
27 class Cover;
28 
29 class CoverCall {
30  public:
31  CoverCall(Cover *parent);
32 
34  CoverCall &set_command(const char *command);
46  CoverCall &set_tilt(float tilt);
48  CoverCall &set_stop(bool stop);
49 
51  void perform();
52 
53  const optional<float> &get_position() const;
54  bool get_stop() const;
55  const optional<float> &get_tilt() const;
56  const optional<bool> &get_toggle() const;
57 
58  protected:
59  void validate_();
60 
62  bool stop_{false};
66 };
67 
70  float position;
71  float tilt;
72 
74  CoverCall to_call(Cover *cover);
76  void apply(Cover *cover);
77 } __attribute__((packed));
78 
80 enum CoverOperation : uint8_t {
87 };
88 
90 
111 class Cover : public EntityBase, public EntityBase_DeviceClass {
112  public:
113  explicit Cover();
114 
122  float position;
124  float tilt{COVER_OPEN};
125 
127  CoverCall make_call();
132  ESPDEPRECATED("open() is deprecated, use make_call().set_command_open().perform() instead.", "2021.9")
133  void open();
138  ESPDEPRECATED("close() is deprecated, use make_call().set_command_close().perform() instead.", "2021.9")
139  void close();
145  ESPDEPRECATED("stop() is deprecated, use make_call().set_command_stop().perform() instead.", "2021.9")
146  void stop();
147 
148  void add_on_state_callback(std::function<void()> &&f);
149 
157  void publish_state(bool save = true);
158 
159  virtual CoverTraits get_traits() = 0;
160 
162  bool is_fully_open() const;
164  bool is_fully_closed() const;
165 
166  protected:
167  friend CoverCall;
168 
169  virtual void control(const CoverCall &call) = 0;
170 
171  optional<CoverRestoreState> restore_state_();
172 
173  CallbackManager<void()> state_callback_{};
174 
176 };
177 
178 } // namespace cover
179 } // namespace esphome
CoverCall & set_command_open()
Set the command to open the cover.
Definition: cover.cpp:51
Base class for all cover devices.
Definition: cover.h:111
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition: cover.cpp:59
CoverOperation
Enum encoding the current operation of a cover.
Definition: cover.h:80
optional< float > position_
Definition: cover.h:63
CoverCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition: cover.cpp:37
CoverCall & set_position(float position)
Set the call to a certain target position.
Definition: cover.cpp:67
The cover is currently closing.
Definition: cover.h:86
CoverCall to_call(Cover *cover)
Convert this struct to a cover call that can be performed.
const float COVER_CLOSED
Definition: cover.cpp:10
float tilt
Definition: cover.h:15
void perform()
Perform the cover call.
Definition: cover.cpp:75
optional< float > tilt_
Definition: cover.h:64
CoverCall & set_stop(bool stop)
Set whether this cover call should stop the cover.
Definition: cover.cpp:143
optional< bool > toggle_
Definition: cover.h:65
Struct used to store the restored state of a cover.
Definition: cover.h:69
ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") extern const Color COLOR_BLACK
void apply(Cover *cover)
Apply these settings to the cover.
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
const char * cover_operation_to_str(CoverOperation op)
Definition: cover.cpp:21
const optional< bool > & get_toggle() const
Definition: cover.cpp:99
const float COVER_OPEN
Definition: cover.cpp:9
The cover is currently idle (not moving)
Definition: cover.h:82
enum esphome::cover::CoverOperation __attribute__
CoverCall(Cover *parent)
Definition: cover.cpp:36
ESPPreferenceObject rtc_
Definition: cover.h:175
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition: cover.h:122
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
float position
Definition: cover.h:14
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition: cover.cpp:71
The cover is currently opening.
Definition: cover.h:84
const optional< float > & get_tilt() const
Definition: cover.cpp:98
const optional< float > & get_position() const
Definition: cover.cpp:97
bool get_stop() const
Definition: cover.cpp:147