ESPHome 2025.8.0b1
Loading...
Searching...
No Matches
cover.cpp
Go to the documentation of this file.
1#include "cover.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace cover {
6
7static const char *const TAG = "cover";
8
9const float COVER_OPEN = 1.0f;
10const float COVER_CLOSED = 0.0f;
11
12const char *cover_command_to_str(float pos) {
13 if (pos == COVER_OPEN) {
14 return "OPEN";
15 } else if (pos == COVER_CLOSED) {
16 return "CLOSE";
17 } else {
18 return "UNKNOWN";
19 }
20}
22 switch (op) {
24 return "IDLE";
26 return "OPENING";
28 return "CLOSING";
29 default:
30 return "UNKNOWN";
31 }
32}
33
35
36CoverCall::CoverCall(Cover *parent) : parent_(parent) {}
37CoverCall &CoverCall::set_command(const char *command) {
38 if (strcasecmp(command, "OPEN") == 0) {
39 this->set_command_open();
40 } else if (strcasecmp(command, "CLOSE") == 0) {
41 this->set_command_close();
42 } else if (strcasecmp(command, "STOP") == 0) {
43 this->set_command_stop();
44 } else if (strcasecmp(command, "TOGGLE") == 0) {
45 this->set_command_toggle();
46 } else {
47 ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command);
48 }
49 return *this;
50}
52 this->position_ = COVER_OPEN;
53 return *this;
54}
56 this->position_ = COVER_CLOSED;
57 return *this;
58}
60 this->stop_ = true;
61 return *this;
62}
64 this->toggle_ = true;
65 return *this;
66}
68 this->position_ = position;
69 return *this;
70}
72 this->tilt_ = tilt;
73 return *this;
74}
76 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
77 auto traits = this->parent_->get_traits();
78 this->validate_();
79 if (this->stop_) {
80 ESP_LOGD(TAG, " Command: STOP");
81 }
82 if (this->position_.has_value()) {
83 if (traits.get_supports_position()) {
84 ESP_LOGD(TAG, " Position: %.0f%%", *this->position_ * 100.0f);
85 } else {
86 ESP_LOGD(TAG, " Command: %s", cover_command_to_str(*this->position_));
87 }
88 }
89 if (this->tilt_.has_value()) {
90 ESP_LOGD(TAG, " Tilt: %.0f%%", *this->tilt_ * 100.0f);
91 }
92 if (this->toggle_.has_value()) {
93 ESP_LOGD(TAG, " Command: TOGGLE");
94 }
95 this->parent_->control(*this);
96}
97const optional<float> &CoverCall::get_position() const { return this->position_; }
98const optional<float> &CoverCall::get_tilt() const { return this->tilt_; }
99const optional<bool> &CoverCall::get_toggle() const { return this->toggle_; }
101 auto traits = this->parent_->get_traits();
102 const char *name = this->parent_->get_name().c_str();
103
104 if (this->position_.has_value()) {
105 auto pos = *this->position_;
106 if (!traits.get_supports_position() && pos != COVER_OPEN && pos != COVER_CLOSED) {
107 ESP_LOGW(TAG, "'%s': position unsupported", name);
108 this->position_.reset();
109 } else if (pos < 0.0f || pos > 1.0f) {
110 ESP_LOGW(TAG, "'%s': position %.2f out of range", name, pos);
111 this->position_ = clamp(pos, 0.0f, 1.0f);
112 }
113 }
114 if (this->tilt_.has_value()) {
115 auto tilt = *this->tilt_;
116 if (!traits.get_supports_tilt()) {
117 ESP_LOGW(TAG, "'%s': tilt unsupported", name);
118 this->tilt_.reset();
119 } else if (tilt < 0.0f || tilt > 1.0f) {
120 ESP_LOGW(TAG, "'%s': tilt %.2f out of range", name, tilt);
121 this->tilt_ = clamp(tilt, 0.0f, 1.0f);
122 }
123 }
124 if (this->toggle_.has_value()) {
125 if (!traits.get_supports_toggle()) {
126 ESP_LOGW(TAG, "'%s': toggle unsupported", name);
127 this->toggle_.reset();
128 }
129 }
130 if (this->stop_) {
131 if (this->position_.has_value() || this->tilt_.has_value() || this->toggle_.has_value()) {
132 ESP_LOGW(TAG, "'%s': cannot position/tilt/toggle when stopping", name);
133 this->position_.reset();
134 this->tilt_.reset();
135 this->toggle_.reset();
136 }
137 }
138}
140 this->stop_ = stop;
141 return *this;
142}
143bool CoverCall::get_stop() const { return this->stop_; }
144
145CoverCall Cover::make_call() { return {this}; }
146void Cover::open() {
147 auto call = this->make_call();
148 call.set_command_open();
149 call.perform();
150}
151void Cover::close() {
152 auto call = this->make_call();
153 call.set_command_close();
154 call.perform();
155}
156void Cover::stop() {
157 auto call = this->make_call();
158 call.set_command_stop();
159 call.perform();
160}
161void Cover::add_on_state_callback(std::function<void()> &&f) { this->state_callback_.add(std::move(f)); }
162void Cover::publish_state(bool save) {
163 this->position = clamp(this->position, 0.0f, 1.0f);
164 this->tilt = clamp(this->tilt, 0.0f, 1.0f);
165
166 ESP_LOGD(TAG, "'%s' - Publishing:", this->name_.c_str());
167 auto traits = this->get_traits();
168 if (traits.get_supports_position()) {
169 ESP_LOGD(TAG, " Position: %.0f%%", this->position * 100.0f);
170 } else {
171 if (this->position == COVER_OPEN) {
172 ESP_LOGD(TAG, " State: OPEN");
173 } else if (this->position == COVER_CLOSED) {
174 ESP_LOGD(TAG, " State: CLOSED");
175 } else {
176 ESP_LOGD(TAG, " State: UNKNOWN");
177 }
178 }
179 if (traits.get_supports_tilt()) {
180 ESP_LOGD(TAG, " Tilt: %.0f%%", this->tilt * 100.0f);
181 }
182 ESP_LOGD(TAG, " Current Operation: %s", cover_operation_to_str(this->current_operation));
183
184 this->state_callback_.call();
185
186 if (save) {
187 CoverRestoreState restore{};
188 memset(&restore, 0, sizeof(restore));
189 restore.position = this->position;
190 if (traits.get_supports_tilt()) {
191 restore.tilt = this->tilt;
192 }
193 this->rtc_.save(&restore);
194 }
195}
198 CoverRestoreState recovered{};
199 if (!this->rtc_.load(&recovered))
200 return {};
201 return recovered;
202}
203
204bool Cover::is_fully_open() const { return this->position == COVER_OPEN; }
205bool Cover::is_fully_closed() const { return this->position == COVER_CLOSED; }
206
208 auto call = cover->make_call();
209 auto traits = cover->get_traits();
210 call.set_position(this->position);
211 if (traits.get_supports_tilt())
212 call.set_tilt(this->tilt);
213 return call;
214}
216 cover->position = this->position;
217 cover->tilt = this->tilt;
218 cover->publish_state();
219}
220
221} // namespace cover
222} // namespace esphome
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t get_object_id_hash()
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:69
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:143
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:139
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
optional< CoverRestoreState > restore_state_()
Definition cover.cpp:196
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:162
void add_on_state_callback(std::function< void()> &&f)
Definition cover.cpp:161
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition cover.cpp:145
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:205
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:204
virtual void control(const CoverCall &call)=0
bool has_value() const
Definition optional.h:92
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
const char * cover_command_to_str(float pos)
Definition cover.cpp:12
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
ESPPreferences * global_preferences
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:215
CoverCall to_call(Cover *cover)
Convert this struct to a cover call that can be performed.
Definition cover.cpp:207