ESPHome 2025.5.0
Loading...
Searching...
No Matches
cover.h
Go to the documentation of this file.
1#pragma once
2
7#include "cover_traits.h"
8
9namespace esphome {
10namespace cover {
11
12const extern float COVER_OPEN;
13const 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
27class Cover;
28
29class 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
88
90
112 public:
113 explicit Cover();
114
122 float position;
125
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
172
174
176};
177
178} // namespace cover
179} // namespace esphome
const optional< float > & get_tilt() const
Definition cover.cpp:98
CoverCall & set_command_toggle()
Set the command to toggle the cover.
Definition cover.cpp:63
const optional< bool > & get_toggle() const
Definition cover.cpp:99
optional< float > tilt_
Definition cover.h:64
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
CoverCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition cover.cpp:37
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
bool get_stop() const
Definition cover.cpp:147
optional< float > position_
Definition cover.h:63
const optional< float > & get_position() const
Definition cover.cpp:97
CoverCall(Cover *parent)
Definition cover.cpp:36
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition cover.cpp:71
optional< bool > toggle_
Definition cover.h:65
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
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:116
ESPDEPRECATED("open() is deprecated, use make_call().set_command_open().perform() instead.", "2021.9") void open()
Open the cover.
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:200
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:166
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
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:124
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:122
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
CallbackManager< void()> state_callback_
Definition cover.h:173
ESPPreferenceObject rtc_
Definition cover.h:175
virtual CoverTraits get_traits()=0
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
virtual void control(const CoverCall &call)=0
float position
Definition cover.h:0
float tilt
Definition cover.h:1
const float COVER_CLOSED
Definition cover.cpp:10
const float COVER_OPEN
Definition cover.cpp:9
enum esphome::cover::CoverOperation __attribute__
const char * cover_operation_to_str(CoverOperation op)
Definition cover.cpp:21
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:80
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:84
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:86
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:82
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Struct used to store the restored state of a cover.
Definition cover.h:69
void apply(Cover *cover)
Apply these settings to the cover.
Definition cover.cpp:219
CoverCall to_call(Cover *cover)
Convert this struct to a cover call that can be performed.
Definition cover.cpp:211