7 static const char *
const TAG =
"cover";
13 if (pos == COVER_OPEN) {
15 }
else if (pos == COVER_CLOSED) {
38 if (strcasecmp(command,
"OPEN") == 0) {
40 }
else if (strcasecmp(command,
"CLOSE") == 0) {
42 }
else if (strcasecmp(command,
"STOP") == 0) {
44 }
else if (strcasecmp(command,
"TOGGLE") == 0) {
80 ESP_LOGD(TAG,
" Command: STOP");
83 if (traits.get_supports_position()) {
84 ESP_LOGD(TAG,
" Position: %.0f%%", *this->
position_ * 100.0f);
90 ESP_LOGD(TAG,
" Tilt: %.0f%%", *this->
tilt_ * 100.0f);
93 ESP_LOGD(TAG,
" Command: TOGGLE");
104 if (!traits.get_supports_position() && pos != COVER_OPEN && pos !=
COVER_CLOSED) {
105 ESP_LOGW(TAG,
"'%s' - This cover device does not support setting position!", this->
parent_->
get_name().
c_str());
107 }
else if (pos < 0.0f || pos > 1.0f) {
108 ESP_LOGW(TAG,
"'%s' - Position %.2f is out of range [0.0 - 1.0]", this->
parent_->
get_name().
c_str(), pos);
114 if (!traits.get_supports_tilt()) {
115 ESP_LOGW(TAG,
"'%s' - This cover device does not support tilt!", this->
parent_->
get_name().
c_str());
117 }
else if (tilt < 0.0f || tilt > 1.0f) {
123 if (!traits.get_supports_toggle()) {
124 ESP_LOGW(TAG,
"'%s' - This cover device does not support toggle!", this->
parent_->
get_name().
c_str());
130 ESP_LOGW(TAG,
"Cannot set position when stopping a cover!");
134 ESP_LOGW(TAG,
"Cannot set tilt when stopping a cover!");
138 ESP_LOGW(TAG,
"Cannot set toggle when stopping a cover!");
151 auto call = this->make_call();
152 call.set_command_open();
155 void Cover::close() {
156 auto call = this->make_call();
157 call.set_command_close();
161 auto call = this->make_call();
162 call.set_command_stop();
170 ESP_LOGD(TAG,
"'%s' - Publishing:", this->name_.c_str());
171 auto traits = this->get_traits();
172 if (traits.get_supports_position()) {
173 ESP_LOGD(TAG,
" Position: %.0f%%", this->
position * 100.0f);
176 ESP_LOGD(TAG,
" State: OPEN");
177 }
else if (this->
position == COVER_CLOSED) {
178 ESP_LOGD(TAG,
" State: CLOSED");
180 ESP_LOGD(TAG,
" State: UNKNOWN");
183 if (traits.get_supports_tilt()) {
184 ESP_LOGD(TAG,
" Tilt: %.0f%%", this->
tilt * 100.0f);
188 this->state_callback_.call();
192 memset(&restore, 0,
sizeof(restore));
194 if (traits.get_supports_tilt()) {
195 restore.tilt = this->
tilt;
197 this->rtc_.save(&restore);
203 if (!this->rtc_.load(&recovered))
215 if (traits.get_supports_tilt())
216 call.set_tilt(this->tilt);
CoverCall & set_command_open()
Set the command to open the cover.
void publish_state(bool save=true)
Publish the current state of the cover.
Base class for all cover devices.
CoverCall & set_command_stop()
Set the command to stop the cover.
CoverCall make_call()
Construct a new cover call used to control the cover.
CoverOperation
Enum encoding the current operation of a cover.
optional< float > position_
bool is_fully_open() const
Helper method to check if the cover is fully open. Equivalent to comparing .position against 1...
CoverCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
CoverCall & set_position(float position)
Set the call to a certain target position.
The cover is currently closing.
void perform()
Perform the cover call.
optional< CoverRestoreState > restore_state_()
CoverCall & set_stop(bool stop)
Set whether this cover call should stop the cover.
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Struct used to store the restored state of a cover.
virtual CoverTraits get_traits()=0
void add_on_state_callback(std::function< void()> &&f)
virtual void control(const CoverCall &call)=0
CoverCall & set_command_toggle()
Set the command to toggle the cover.
CoverCall & set_command_close()
Set the command to close the cover.
ESPPreferences * global_preferences
const char * cover_operation_to_str(CoverOperation op)
const optional< bool > & get_toggle() const
bool is_fully_closed() const
Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0...
The cover is currently idle (not moving)
CoverCall to_call(Cover *cover)
Convert this struct to a cover call that can be performed.
constexpr const char * c_str() const
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
Implementation of SPI Controller mode.
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
const char * cover_command_to_str(float pos)
The cover is currently opening.
const optional< float > & get_tilt() const
const StringRef & get_name() const
void apply(Cover *cover)
Apply these settings to the cover.
const optional< float > & get_position() const