ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
cover.h
Go to the documentation of this file.
1#pragma once
2
7
8#include "cover_traits.h"
9
10namespace esphome {
11namespace cover {
12
13const extern float COVER_OPEN;
14const extern float COVER_CLOSED;
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 if (!(obj)->get_device_class_ref().empty()) { \
24 ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class_ref().c_str()); \
25 } \
26 }
27
28class Cover;
29
30class CoverCall {
31 public:
32 CoverCall(Cover *parent);
33
35 CoverCall &set_command(const char *command);
47 CoverCall &set_tilt(float tilt);
49 CoverCall &set_stop(bool stop);
50
52 void perform();
53
54 const optional<float> &get_position() const;
55 bool get_stop() const;
56 const optional<float> &get_tilt() const;
57 const optional<bool> &get_toggle() const;
58
59 protected:
60 void validate_();
61
63 bool stop_{false};
67};
68
71 float position;
72 float tilt;
73
75 CoverCall to_call(Cover *cover);
77 void apply(Cover *cover);
78} __attribute__((packed));
79
89
91
113 public:
114 explicit Cover();
115
123 float position;
126
129
130 void add_on_state_callback(std::function<void()> &&f);
131
139 void publish_state(bool save = true);
140
141 virtual CoverTraits get_traits() = 0;
142
144 bool is_fully_open() const;
146 bool is_fully_closed() const;
147
148 protected:
149 friend CoverCall;
150
151 virtual void control(const CoverCall &call) = 0;
152
154
156
158};
159
160} // namespace cover
161} // namespace esphome
const optional< float > & get_tilt() const
Definition cover.cpp:103
CoverCall & set_command_toggle()
Set the command to toggle the cover.
Definition cover.cpp:68
const optional< bool > & get_toggle() const
Definition cover.cpp:104
optional< float > tilt_
Definition cover.h:65
CoverCall & set_command_open()
Set the command to open the cover.
Definition cover.cpp:56
CoverCall & set_command_close()
Set the command to close the cover.
Definition cover.cpp:60
CoverCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition cover.cpp:42
void perform()
Perform the cover call.
Definition cover.cpp:80
CoverCall & set_position(float position)
Set the call to a certain target position.
Definition cover.cpp:72
CoverCall & set_command_stop()
Set the command to stop the cover.
Definition cover.cpp:64
bool get_stop() const
Definition cover.cpp:148
optional< float > position_
Definition cover.h:64
const optional< float > & get_position() const
Definition cover.cpp:102
CoverCall(Cover *parent)
Definition cover.cpp:41
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition cover.cpp:76
optional< bool > toggle_
Definition cover.h:66
CoverCall & set_stop(bool stop)
Set whether this cover call should stop the cover.
Definition cover.cpp:144
Base class for all cover devices.
Definition cover.h:112
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:117
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:190
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:153
void add_on_state_callback(std::function< void()> &&f)
Definition cover.cpp:152
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition cover.cpp:150
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:125
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:123
bool is_fully_closed() const
Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0....
Definition cover.cpp:199
CallbackManager< void()> state_callback_
Definition cover.h:155
ESPPreferenceObject rtc_
Definition cover.h:157
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:198
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:15
const float COVER_OPEN
Definition cover.cpp:14
enum esphome::cover::CoverOperation __attribute__
const char * cover_operation_to_str(CoverOperation op)
Definition cover.cpp:26
CoverOperation
Enum encoding the current operation of a cover.
Definition cover.h:81
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:85
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:87
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Definition cover.h:83
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:70
void apply(Cover *cover)
Apply these settings to the cover.
Definition cover.cpp:209
CoverCall to_call(Cover *cover)
Convert this struct to a cover call that can be performed.
Definition cover.cpp:201