time_based_cover: rename last_operation_ to last_moving_operation_

The previous implementation of the `last_operation` attribute was a misnomer: it represented the last moving operation of the cover, since it ignored all `COVER_OPERATION_IDLE` operations.
To keep compatibility with existing logic, an ad-hoc attribute `last_moving_operation` is introduced (and exposed via a getter method).
It has the same update logic as the previously named `last_operation` attribute.
This commit is contained in:
Carlo Mion 2024-06-01 13:16:51 +00:00
parent 2beb1f0336
commit c8ac879d56
2 changed files with 19 additions and 4 deletions

View file

@ -63,14 +63,18 @@ void TimeBasedCover::control(const CoverCall &call) {
this->publish_state(); this->publish_state();
} }
if (call.get_toggle().has_value()) { if (call.get_toggle().has_value()) {
// Cover is not idle -> stop it
if (this->current_operation != COVER_OPERATION_IDLE) { if (this->current_operation != COVER_OPERATION_IDLE) {
this->start_direction_(COVER_OPERATION_IDLE); this->start_direction_(COVER_OPERATION_IDLE);
this->publish_state(); this->publish_state();
} else { } else {
if (this->position == COVER_CLOSED || this->last_operation_ == COVER_OPERATION_CLOSING) { // Cover is idle, check its last direction of movement
if (this->position == COVER_CLOSED || this->last_moving_operation_ == COVER_OPERATION_CLOSING) {
// Cover was closing -> open it
this->target_position_ = COVER_OPEN; this->target_position_ = COVER_OPEN;
this->start_direction_(COVER_OPERATION_OPENING); this->start_direction_(COVER_OPERATION_OPENING);
} else { } else {
// Cover was opening -> close it
this->target_position_ = COVER_CLOSED; this->target_position_ = COVER_CLOSED;
this->start_direction_(COVER_OPERATION_CLOSING); this->start_direction_(COVER_OPERATION_CLOSING);
} }
@ -126,17 +130,20 @@ void TimeBasedCover::start_direction_(CoverOperation dir) {
return; return;
this->recompute_position_(); this->recompute_position_();
// Keep track of the previous operation the cover was in
this->last_operation_ = current_operation;
Trigger<> *trig; Trigger<> *trig;
switch (dir) { switch (dir) {
case COVER_OPERATION_IDLE: case COVER_OPERATION_IDLE:
trig = this->stop_trigger_; trig = this->stop_trigger_;
break; break;
case COVER_OPERATION_OPENING: case COVER_OPERATION_OPENING:
this->last_operation_ = dir; this->last_moving_operation_ = dir;
trig = this->open_trigger_; trig = this->open_trigger_;
break; break;
case COVER_OPERATION_CLOSING: case COVER_OPERATION_CLOSING:
this->last_operation_ = dir; this->last_moving_operation_ = dir;
trig = this->close_trigger_; trig = this->close_trigger_;
break; break;
default: default:

View file

@ -23,7 +23,10 @@ class TimeBasedCover : public cover::Cover, public Component {
void set_has_built_in_endstop(bool value) { this->has_built_in_endstop_ = value; } void set_has_built_in_endstop(bool value) { this->has_built_in_endstop_ = value; }
void set_manual_control(bool value) { this->manual_control_ = value; } void set_manual_control(bool value) { this->manual_control_ = value; }
void set_assumed_state(bool value) { this->assumed_state_ = value; } void set_assumed_state(bool value) { this->assumed_state_ = value; }
// Return the previous operation the cover was executing.
cover::CoverOperation get_last_operation() const { return this->last_operation_; } cover::CoverOperation get_last_operation() const { return this->last_operation_; }
// Return the previous direction of travel of the cover (either OPENING or CLOSING)
cover::CoverOperation get_last_moving_operation() const { return this->last_moving_operation_; }
protected: protected:
void control(const cover::CoverCall &call) override; void control(const cover::CoverCall &call) override;
@ -48,7 +51,12 @@ class TimeBasedCover : public cover::Cover, public Component {
bool has_built_in_endstop_{false}; bool has_built_in_endstop_{false};
bool manual_control_{false}; bool manual_control_{false};
bool assumed_state_{false}; bool assumed_state_{false};
cover::CoverOperation last_operation_{cover::COVER_OPERATION_OPENING}; // Keep track of the previous operation the cover was in.
// Useful for instance to decide how to issue a command on the basis on the last received command.
cover::CoverOperation last_operation_{cover::COVER_OPERATION_IDLE};
// Keep track of the previous direction the cover was moving towards (either OPENING or CLOSING).
// Used by the toggle() action to know the previous direction of travel of the cover.
cover::CoverOperation last_moving_operation_{cover::COVER_OPERATION_OPENING};
}; };
} // namespace time_based } // namespace time_based