ESPHome 2026.2.1
Loading...
Searching...
No Matches
cover.h
Go to the documentation of this file.
1#pragma once
2
8
9#include "cover_traits.h"
10
11namespace esphome::cover {
12
13static constexpr float COVER_OPEN = 1.0f;
14static constexpr float COVER_CLOSED = 0.0f;
15
16#define LOG_COVER(prefix, type, obj) \
17 if ((obj) != nullptr) { \
18 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
19 auto traits_ = (obj)->get_traits(); \
20 if (traits_.get_is_assumed_state()) { \
21 ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
22 } \
23 LOG_ENTITY_DEVICE_CLASS(TAG, prefix, *(obj)); \
24 }
25
26class Cover;
27
28class CoverCall {
29 public:
30 CoverCall(Cover *parent);
31
33 CoverCall &set_command(const char *command);
45 CoverCall &set_tilt(float tilt);
47 CoverCall &set_stop(bool stop);
48
50 void perform();
51
52 const optional<float> &get_position() const;
53 bool get_stop() const;
54 const optional<float> &get_tilt() const;
55 const optional<bool> &get_toggle() const;
56
57 protected:
58 void validate_();
59
61 bool stop_{false};
65};
66
69 float position;
70 float tilt;
71
73 CoverCall to_call(Cover *cover);
75 void apply(Cover *cover);
76} __attribute__((packed));
77
87
88const LogString *cover_operation_to_str(CoverOperation op);
89
111 public:
112 explicit Cover();
113
121 float position;
123 float tilt{COVER_OPEN};
124
127
128 void add_on_state_callback(std::function<void()> &&f);
129
137 void publish_state(bool save = true);
138
139 virtual CoverTraits get_traits() = 0;
140
142 bool is_fully_open() const;
144 bool is_fully_closed() const;
145
146 protected:
147 friend CoverCall;
148
149 virtual void control(const CoverCall &call) = 0;
150
152
154
156};
157
158} // namespace esphome::cover
const optional< float > & get_tilt() const
Definition cover.cpp:93
CoverCall & set_command_toggle()
Set the command to toggle the cover.
Definition cover.cpp:58
const optional< bool > & get_toggle() const
Definition cover.cpp:94
optional< float > tilt_
Definition cover.h:63
CoverCall & set_command_open()
Set the command to open the cover.
Definition cover.cpp:46
CoverCall & set_command_close()
Set the command to close the cover.
Definition cover.cpp:50
CoverCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition cover.cpp:32
void perform()
Perform the cover call.
Definition cover.cpp:70
CoverCall & set_position(float position)
Set the call to a certain target position.
Definition cover.cpp:62
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition cover.cpp:54
bool get_stop() const
Definition cover.cpp:138
optional< float > position_
Definition cover.h:62
const optional< float > & get_position() const
Definition cover.cpp:92
CoverCall(Cover *parent)
Definition cover.cpp:31
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition cover.cpp:66
optional< bool > toggle_
Definition cover.h:64
CoverCall & set_stop(bool stop)
Set whether this cover call should stop the cover.
Definition cover.cpp:134
Base class for all cover devices.
Definition cover.h:110
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:115
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:180
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:143
void add_on_state_callback(std::function< void()> &&f)
Definition cover.cpp:142
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition cover.cpp:140
LazyCallbackManager< void()> state_callback_
Definition cover.h:153
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:123
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:121
bool is_fully_closed() const
Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0....
Definition cover.cpp:189
ESPPreferenceObject rtc_
Definition cover.h:155
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:188
virtual void control(const CoverCall &call)=0
float position
Definition cover.h:0
float tilt
Definition cover.h:1
const LogString * cover_operation_to_str(CoverOperation op)
Definition cover.cpp:25
enum esphome::cover::CoverOperation __attribute__
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:79
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:83
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:85
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:81
Struct used to store the restored state of a cover.
Definition cover.h:68
void apply(Cover *cover)
Apply these settings to the cover.
Definition cover.cpp:199
CoverCall to_call(Cover *cover)
Convert this struct to a cover call that can be performed.
Definition cover.cpp:191