ESPHome 2026.11.0-dev
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 (log_entity_header(TAG, prefix, LOG_STR_LITERAL(type), (obj))) { \
18 auto traits_ = (obj)->get_traits(); \
19 if (traits_.get_is_assumed_state()) { \
20 ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
21 } \
22 LOG_ENTITY_DEVICE_CLASS(TAG, prefix, *(obj)); \
23 }
24
25class Cover;
26
27class CoverCall {
28 public:
29 CoverCall(Cover *parent);
30
32 CoverCall &set_command(const char *command);
46 CoverCall &set_tilt(float tilt);
48 CoverCall &set_stop(bool stop);
50 CoverCall &set_stop_tilt(bool stop_tilt);
51
53 void perform();
54
55 const optional<float> &get_position() const;
56 bool get_stop() const { return this->stop_; }
57 bool get_stop_tilt() const { return this->stop_tilt_; }
58 const optional<float> &get_tilt() const;
59 const optional<bool> &get_toggle() const;
60
61 protected:
62 void validate_();
63
65 bool stop_{false};
66 bool stop_tilt_{false};
67 optional<float> position_{};
68 optional<float> tilt_{};
69 optional<bool> toggle_{};
70};
71
74 float position;
75 float tilt;
76
78 CoverCall to_call(Cover *cover);
80 void apply(Cover *cover);
81} __attribute__((packed));
82
92
93const LogString *cover_operation_to_str(CoverOperation op);
94
115class Cover : public EntityBase {
116 public:
117 explicit Cover();
118
126 float position;
128 float tilt{COVER_OPEN};
129
131 CoverCall make_call() { return {this}; }
132
133 template<typename F> void add_on_state_callback(F &&f) { this->state_callback_.add(std::forward<F>(f)); }
134
142 void publish_state(bool save = true);
143
144 virtual CoverTraits get_traits() = 0;
145
147 bool is_fully_open() const { return this->position == COVER_OPEN; }
149 bool is_fully_closed() const { return this->position == COVER_CLOSED; }
150
151 protected:
152 friend CoverCall;
153
154 virtual void control(const CoverCall &call) = 0;
155
156 optional<CoverRestoreState> restore_state_();
157
159
161};
162
163} // namespace esphome::cover
const optional< float > & get_tilt() const
Definition cover.cpp:100
CoverCall & set_command_toggle()
Set the command to toggle the cover.
Definition cover.cpp:62
const optional< bool > & get_toggle() const
Definition cover.cpp:101
optional< float > tilt_
Definition cover.h:68
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:74
CoverCall & set_position(float position)
Set the call to a certain target position.
Definition cover.cpp:66
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition cover.cpp:54
bool get_stop() const
Definition cover.h:56
CoverCall & set_stop_tilt(bool stop_tilt)
Set whether this cover call should stop the cover tilt.
Definition cover.cpp:154
optional< float > position_
Definition cover.h:67
CoverCall & set_command_stop_tilt()
Set the command to stop the cover tilt.
Definition cover.cpp:58
bool get_stop_tilt() const
Definition cover.h:57
const optional< float > & get_position() const
Definition cover.cpp:99
CoverCall(Cover *parent)
Definition cover.cpp:31
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition cover.cpp:70
optional< bool > toggle_
Definition cover.h:69
CoverCall & set_stop(bool stop)
Set whether this cover call should stop the cover.
Definition cover.cpp:150
Base class for all cover devices.
Definition cover.h:115
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:120
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:195
void add_on_state_callback(F &&f)
Definition cover.h:133
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:158
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition cover.h:131
LazyCallbackManager< void()> state_callback_
Definition cover.h:158
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:128
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:126
bool is_fully_closed() const
Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0....
Definition cover.h:149
ESPPreferenceObject rtc_
Definition cover.h:160
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.h:147
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:84
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:88
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:90
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:86
Struct used to store the restored state of a cover.
Definition cover.h:73
void apply(Cover *cover)
Apply these settings to the cover.
Definition cover.cpp:211
CoverCall to_call(Cover *cover)
Convert this struct to a cover call that can be performed.
Definition cover.cpp:203