ESPHome  2022.11.3
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 {
112  public:
113  explicit Cover();
114  explicit Cover(const std::string &name);
115 
123  float position;
125  float tilt{COVER_OPEN};
126 
128  CoverCall make_call();
133  ESPDEPRECATED("open() is deprecated, use make_call().set_command_open() instead.", "2021.9")
134  void open();
139  ESPDEPRECATED("close() is deprecated, use make_call().set_command_close() instead.", "2021.9")
140  void close();
145  ESPDEPRECATED("stop() is deprecated, use make_call().set_command_stop() 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  void set_device_class(const std::string &device_class);
161  std::string get_device_class();
162 
164  bool is_fully_open() const;
166  bool is_fully_closed() const;
167 
168  protected:
169  friend CoverCall;
170 
171  virtual void control(const CoverCall &call) = 0;
172 
177  virtual std::string device_class();
178 
179  optional<CoverRestoreState> restore_state_();
180 
181  CallbackManager<void()> state_callback_{};
182  optional<std::string> device_class_override_{};
183 
185 };
186 
187 } // namespace cover
188 } // namespace esphome
CoverCall & set_command_open()
Set the command to open the cover.
Definition: cover.cpp:51
const char * name
Definition: stm32flash.h:78
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:184
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition: cover.h:123
Definition: a4988.cpp:4
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