diff --git a/esphome/components/time_based_tilt/time_based_tilt_cover.cpp b/esphome/components/time_based_tilt/time_based_tilt_cover.cpp index 6a0fe7c6fa..24a0f28212 100644 --- a/esphome/components/time_based_tilt/time_based_tilt_cover.cpp +++ b/esphome/components/time_based_tilt/time_based_tilt_cover.cpp @@ -23,7 +23,7 @@ void TimeBasedTiltCover::dump_config() { ESP_LOGCONFIG(TAG, " Recalibration time: %.3fs", this->recalibration_time_ / 1e3f); } void TimeBasedTiltCover::setup() { - if ( this->tilt_close_duration_ == 0 || this->tilt_open_duration_ == 0 ) { + if (this->tilt_close_duration_ == 0 || this->tilt_open_duration_ == 0) { this->tilt_close_duration_ = 0; this->tilt_open_duration_ = 0; } @@ -60,25 +60,26 @@ bool TimeBasedTiltCover::is_at_target_tilt_() const { } void TimeBasedTiltCover::loop() { - - if ( this->fsm_state_ == STATE_IDLE && this->target_position_ == TARGET_NONE && this->target_tilt_ == TARGET_NONE ) return; + if (this->fsm_state_ == STATE_IDLE && this->target_position_ == TARGET_NONE && this->target_tilt_ == TARGET_NONE) + return; const uint32_t now = millis(); // recalibrating in extreme postions - if ( this->fsm_state_ == STATE_CALIBRATING ) { - if ( now - this->last_recompute_time_ >= this->recalibration_time_ ) { + if (this->fsm_state_ == STATE_CALIBRATING) { + if (now - this->last_recompute_time_ >= this->recalibration_time_) { this->fsm_state_ = STATE_STOPPING; } return; } // STOPPING - determining the direction of the last movement and the stopping time. Needed to support interlocking - if ( this->fsm_state_ == STATE_STOPPING ) { + if (this->fsm_state_ == STATE_STOPPING) { this->stop_trigger_->trigger(); - if (this->current_operation != COVER_OPERATION_IDLE){ + if (this->current_operation != COVER_OPERATION_IDLE) { this->interlocked_time = millis(); - this->interlocked_direction = this->current_operation == COVER_OPERATION_CLOSING ? COVER_OPERATION_OPENING : COVER_OPERATION_CLOSING; + this->interlocked_direction = + this->current_operation == COVER_OPERATION_CLOSING ? COVER_OPERATION_OPENING : COVER_OPERATION_CLOSING; } else { this->interlocked_direction = COVER_OPERATION_IDLE; } @@ -90,16 +91,16 @@ void TimeBasedTiltCover::loop() { } // if the cover is not moving, check whether the new targets are set and if they are, compute move direction - if ( this->fsm_state_ == STATE_IDLE && (this->target_position_ != TARGET_NONE || this->target_tilt_ != TARGET_NONE) ) { - - if (this->target_position_ != TARGET_NONE ) { - this->current_operation = this->compute_direction(this->target_position_,this->position); + if (this->fsm_state_ == STATE_IDLE && (this->target_position_ != TARGET_NONE || this->target_tilt_ != TARGET_NONE)) { + if (this->target_position_ != TARGET_NONE) { + this->current_operation = this->compute_direction(this->target_position_, this->position); } else { - this->current_operation = this->compute_direction(this->target_tilt_,this->tilt); + this->current_operation = this->compute_direction(this->target_tilt_, this->tilt); } // interlocking support - if ( this->current_operation == this->interlocked_direction - && now - this->interlocked_time < this->interlock_wait_time_ ) return; + if (this->current_operation == this->interlocked_direction && + now - this->interlocked_time < this->interlock_wait_time_) + return; Trigger<> *trig = this->current_operation == COVER_OPERATION_CLOSING ? this->close_trigger_ : this->open_trigger_; @@ -110,43 +111,44 @@ void TimeBasedTiltCover::loop() { return; } - //moving state - if ( this->fsm_state_ == STATE_MOVING ) { - + // moving state + if (this->fsm_state_ == STATE_MOVING) { auto travel_time = now - this->last_recompute_time_; this->last_recompute_time_ = now; - float dir_factor = this->current_operation == COVER_OPERATION_CLOSING ? -1.0 : 1.0 ; - auto inertia_time = this->current_operation == COVER_OPERATION_CLOSING ? this->inertia_close_time_ : this->inertia_open_time_ ; + float dir_factor = this->current_operation == COVER_OPERATION_CLOSING ? -1.0 : 1.0; + auto inertia_time = + this->current_operation == COVER_OPERATION_CLOSING ? this->inertia_close_time_ : this->inertia_open_time_; - if ( inertia_time > 0 && this->inertia * dir_factor < 0.5f ) { // inertia before movement + if (inertia_time > 0 && this->inertia * dir_factor < 0.5f) { // inertia before movement auto inertia_step = dir_factor * travel_time / inertia_time; this->inertia += inertia_step; - auto rest = this->inertia - clamp( this->inertia , -0.5f, 0.5f); - this->inertia = clamp( this->inertia, -0.5f, 0.5f); + auto rest = this->inertia - clamp(this->inertia, -0.5f, 0.5f); + this->inertia = clamp(this->inertia, -0.5f, 0.5f); - if ( ! rest ) return; // the movement has not yet actually started - travel_time = dir_factor * rest * inertia_time; // actual movement time taking inertia into account + if (!rest) + return; // the movement has not yet actually started + travel_time = dir_factor * rest * inertia_time; // actual movement time taking inertia into account } - auto tilt_time = this->current_operation == COVER_OPERATION_CLOSING ? this->tilt_close_duration_ : this->tilt_open_duration_; + auto tilt_time = + this->current_operation == COVER_OPERATION_CLOSING ? this->tilt_close_duration_ : this->tilt_open_duration_; - if ( tilt_time > 0 && ( this->tilt - 0.5f ) * dir_factor < 0.5f ) { // tilting before movement - auto tilt_step = dir_factor * travel_time / tilt_time; + if (tilt_time > 0 && (this->tilt - 0.5f) * dir_factor < 0.5f) { // tilting before movement + auto tilt_step = dir_factor * travel_time / tilt_time; this->tilt += tilt_step; - auto rest = this->tilt - 0.5f - clamp( this->tilt - 0.5f , -0.5f, 0.5f); - this->tilt = clamp( this->tilt , 0.0f, 1.0f); + auto rest = this->tilt - 0.5f - clamp(this->tilt - 0.5f, -0.5f, 0.5f); + this->tilt = clamp(this->tilt, 0.0f, 1.0f); - if (this->target_position_ == TARGET_NONE && this->is_at_target_tilt_()) { // only tilting w/o position change + if (this->target_position_ == TARGET_NONE && this->is_at_target_tilt_()) { // only tilting w/o position change this->last_recompute_time_ = now; this->target_tilt_ = TARGET_NONE; - this->last_publish_time_= now; + this->last_publish_time_ = now; // If the cover is in extreme positions, run recalibration - if ( this->recalibration_time_ > 0 && - (((this->position == COVER_CLOSED && (tilt_time == 0 || this->tilt == COVER_CLOSED)) || - (this->position == COVER_OPEN && (tilt_time == 0 || this->tilt == COVER_OPEN))))) - { + if (this->recalibration_time_ > 0 && + (((this->position == COVER_CLOSED && (tilt_time == 0 || this->tilt == COVER_CLOSED)) || + (this->position == COVER_OPEN && (tilt_time == 0 || this->tilt == COVER_OPEN))))) { this->fsm_state_ = STATE_CALIBRATING; this->publish_state(false); } else { @@ -156,34 +158,34 @@ void TimeBasedTiltCover::loop() { return; // only tilting w/o position change so no need to recompute position } - if ( now - this->last_publish_time_ > (( tilt_time / 5 ) > 1000 ? 1000 : ( tilt_time / 5 )) ) { + if (now - this->last_publish_time_ > ((tilt_time / 5) > 1000 ? 1000 : (tilt_time / 5))) { this->publish_state(false); - this->last_publish_time_= now; + this->last_publish_time_ = now; } - if ( ! rest ) return; // the movement has not yet actually started + if (!rest) + return; // the movement has not yet actually started - travel_time = dir_factor * rest * tilt_time; // actual movement time taking tilt into account + travel_time = dir_factor * rest * tilt_time; // actual movement time taking tilt into account } - auto move_time = this->current_operation == COVER_OPERATION_CLOSING ? this->close_duration_ : this->open_duration_ ; + auto move_time = this->current_operation == COVER_OPERATION_CLOSING ? this->close_duration_ : this->open_duration_; - if ( move_time > 0 && ( this->position - 0.5f ) * dir_factor < 0.5f ) { - auto move_step = dir_factor * travel_time / move_time; + if (move_time > 0 && (this->position - 0.5f) * dir_factor < 0.5f) { + auto move_step = dir_factor * travel_time / move_time; this->position += move_step; - this->position = clamp( this->position , 0.0f, 1.0f); + this->position = clamp(this->position, 0.0f, 1.0f); } if (this->is_at_target_position_()) { this->last_recompute_time_ = now; this->target_position_ = TARGET_NONE; - this->last_publish_time_= now; + this->last_publish_time_ = now; // If the cover is in extreme positions, run recalibration - if ( this->recalibration_time_ > 0 && - (((this->position == COVER_CLOSED && (tilt_time == 0 || this->tilt == COVER_CLOSED)) || - (this->position == COVER_OPEN && (tilt_time == 0 || this->tilt == COVER_OPEN))))) - { + if (this->recalibration_time_ > 0 && + (((this->position == COVER_CLOSED && (tilt_time == 0 || this->tilt == COVER_CLOSED)) || + (this->position == COVER_OPEN && (tilt_time == 0 || this->tilt == COVER_OPEN))))) { this->fsm_state_ = STATE_CALIBRATING; this->publish_state(false); } else { @@ -191,19 +193,18 @@ void TimeBasedTiltCover::loop() { } } - if ( now - this->last_publish_time_ > 1000 ) { - this->publish_state(false); - this->last_publish_time_= now; + if (now - this->last_publish_time_ > 1000) { + this->publish_state(false); + this->last_publish_time_ = now; } } - } float TimeBasedTiltCover::get_setup_priority() const { return setup_priority::DATA; } CoverTraits TimeBasedTiltCover::get_traits() { auto traits = CoverTraits(); traits.set_supports_position(true); - traits.set_supports_tilt(this->tilt_close_duration_ !=0 && this->tilt_open_duration_ !=0); + traits.set_supports_tilt(this->tilt_close_duration_ != 0 && this->tilt_open_duration_ != 0); traits.set_supports_toggle(true); traits.set_supports_stop(true); traits.set_is_assumed_state(this->assumed_state_); @@ -212,82 +213,81 @@ CoverTraits TimeBasedTiltCover::get_traits() { void TimeBasedTiltCover::control(const CoverCall &call) { if (call.get_stop()) { this->target_position_ = TARGET_NONE; - this->target_tilt_= TARGET_NONE; + this->target_tilt_ = TARGET_NONE; this->fsm_state_ = STATE_STOPPING; return; } - if (call.get_position().has_value() && call.get_tilt().has_value() ) { - auto pos = *call.get_position(); - auto til = *call.get_tilt(); + if (call.get_position().has_value() && call.get_tilt().has_value()) { + auto pos = *call.get_position(); + auto til = *call.get_tilt(); - if (this->round_position(pos) == this->round_position(this->position)) - pos = TARGET_NONE; - if (this->round_position(til) == this->round_position(this->tilt)) - til = TARGET_NONE; + if (this->round_position(pos) == this->round_position(this->position)) + pos = TARGET_NONE; + if (this->round_position(til) == this->round_position(this->tilt)) + til = TARGET_NONE; - this->target_position_ = pos; - this->target_tilt_ = til; + this->target_position_ = pos; + this->target_tilt_ = til; - if (this->fsm_state_ == STATE_MOVING) { - auto direction = COVER_OPERATION_IDLE; - if ( this->target_position_ != TARGET_NONE && this->target_position_ != this->position) { - direction = this->compute_direction(this->target_position_ , this->position ); - } else if ( this->target_tilt_ != TARGET_NONE && this->target_tilt_ != this->tilt) { - direction = this->compute_direction( this->target_tilt_ , this->tilt ); - } - - if (direction != this->current_operation ) - { - this->fsm_state_ = STATE_STOPPING; - } + if (this->fsm_state_ == STATE_MOVING) { + auto direction = COVER_OPERATION_IDLE; + if (this->target_position_ != TARGET_NONE && this->target_position_ != this->position) { + direction = this->compute_direction(this->target_position_, this->position); + } else if (this->target_tilt_ != TARGET_NONE && this->target_tilt_ != this->tilt) { + direction = this->compute_direction(this->target_tilt_, this->tilt); } + + if (direction != this->current_operation) { + this->fsm_state_ = STATE_STOPPING; + } + } } else if (call.get_position().has_value()) { - auto pos = *call.get_position(); + auto pos = *call.get_position(); - if ( pos == COVER_CLOSED && this->position == COVER_CLOSED && this->tilt != COVER_CLOSED ) { - pos = TARGET_NONE; - this->target_tilt_ = COVER_CLOSED; - } else if ( pos == COVER_OPEN && this->position == COVER_OPEN && this->tilt != COVER_OPEN ) { - pos = TARGET_NONE; - this->target_tilt_ = COVER_OPEN; - } else if ( this->round_position(pos) == this->round_position(this->position) ) { - pos = TARGET_NONE; - } + if (pos == COVER_CLOSED && this->position == COVER_CLOSED && this->tilt != COVER_CLOSED) { + pos = TARGET_NONE; + this->target_tilt_ = COVER_CLOSED; + } else if (pos == COVER_OPEN && this->position == COVER_OPEN && this->tilt != COVER_OPEN) { + pos = TARGET_NONE; + this->target_tilt_ = COVER_OPEN; + } else if (this->round_position(pos) == this->round_position(this->position)) { + pos = TARGET_NONE; + } - this->target_position_ = pos; + this->target_position_ = pos; - if (this->fsm_state_ == STATE_MOVING) { - auto direction = COVER_OPERATION_IDLE; - if ( this->target_position_ != TARGET_NONE && this->target_position_ != this->position) { - direction = this->compute_direction(this->target_position_ , this->position ); - this->target_tilt_ = TARGET_NONE; // unset previous target tilt - } else if ( this->target_tilt_ != TARGET_NONE && this->target_tilt_ != this->tilt) { - direction = this->compute_direction( this->target_tilt_ , this->tilt ); - } - - if (direction != this->current_operation ) { - this->fsm_state_ = STATE_STOPPING; - } + if (this->fsm_state_ == STATE_MOVING) { + auto direction = COVER_OPERATION_IDLE; + if (this->target_position_ != TARGET_NONE && this->target_position_ != this->position) { + direction = this->compute_direction(this->target_position_, this->position); + this->target_tilt_ = TARGET_NONE; // unset previous target tilt + } else if (this->target_tilt_ != TARGET_NONE && this->target_tilt_ != this->tilt) { + direction = this->compute_direction(this->target_tilt_, this->tilt); } - } else if (call.get_tilt().has_value()) { - auto til = *call.get_tilt(); - if ( this->round_position(til) == this->round_position(this->tilt) ) { - til = TARGET_NONE; - } - this->target_tilt_ = til; - - if (this->fsm_state_ == STATE_MOVING) { - auto direction = COVER_OPERATION_IDLE; - if ( this->target_tilt_ != TARGET_NONE && this->target_tilt_ != this->tilt) { - direction = this->compute_direction( this->target_tilt_ , this->tilt ); - this->target_position_ = TARGET_NONE; - } - - if (direction != this->current_operation ) { - this->fsm_state_ = STATE_STOPPING; - } + if (direction != this->current_operation) { + this->fsm_state_ = STATE_STOPPING; } + } + } else if (call.get_tilt().has_value()) { + auto til = *call.get_tilt(); + if (this->round_position(til) == this->round_position(this->tilt)) { + til = TARGET_NONE; + } + + this->target_tilt_ = til; + + if (this->fsm_state_ == STATE_MOVING) { + auto direction = COVER_OPERATION_IDLE; + if (this->target_tilt_ != TARGET_NONE && this->target_tilt_ != this->tilt) { + direction = this->compute_direction(this->target_tilt_, this->tilt); + this->target_position_ = TARGET_NONE; + } + + if (direction != this->current_operation) { + this->fsm_state_ = STATE_STOPPING; + } + } } if (call.get_toggle().has_value()) { @@ -296,11 +296,11 @@ void TimeBasedTiltCover::control(const CoverCall &call) { this->target_position_ = TARGET_NONE; this->target_tilt_ = TARGET_NONE; } else { - if ( this->position == COVER_CLOSED && this->tilt == COVER_CLOSED ) { + if (this->position == COVER_CLOSED && this->tilt == COVER_CLOSED) { this->target_position_ = COVER_OPEN; - } else if ( this->position == COVER_OPEN && this->tilt == COVER_OPEN ) { + } else if (this->position == COVER_OPEN && this->tilt == COVER_OPEN) { this->target_position_ = COVER_CLOSED; - } else if ( this->last_operation_ == COVER_OPERATION_CLOSING ) { + } else if (this->last_operation_ == COVER_OPERATION_CLOSING) { if (this->position != COVER_OPEN) { this->target_position_ = COVER_OPEN; } else { diff --git a/esphome/components/time_based_tilt/time_based_tilt_cover.h b/esphome/components/time_based_tilt/time_based_tilt_cover.h index 137f93be77..7aaeaa5c85 100644 --- a/esphome/components/time_based_tilt/time_based_tilt_cover.h +++ b/esphome/components/time_based_tilt/time_based_tilt_cover.h @@ -25,8 +25,10 @@ class TimeBasedTiltCover : public cover::Cover, public Component { void set_recalibration_time(uint32_t recalibration_time) { this->recalibration_time_ = recalibration_time; } void set_inertia_open_time(uint32_t inertia_time) { this->inertia_open_time_ = inertia_time; } void set_inertia_close_time(uint32_t inertia_time) { this->inertia_close_time_ = inertia_time; } - cover::CoverOperation compute_direction(float target, float current) { return target < current ? cover::COVER_OPERATION_CLOSING : cover::COVER_OPERATION_OPENING ; }; - float round_position(float pos) { return round(100 * pos)/100; }; + cover::CoverOperation compute_direction(float target, float current) { + return target < current ? cover::COVER_OPERATION_CLOSING : cover::COVER_OPERATION_OPENING; + }; + float round_position(float pos) { return round(100 * pos) / 100; }; cover::CoverTraits get_traits() override; void set_assumed_state(bool value) { this->assumed_state_ = value; } @@ -51,12 +53,7 @@ class TimeBasedTiltCover : public cover::Cover, public Component { uint32_t inertia_close_time_; const static float TARGET_NONE; - enum State : uint8_t { - STATE_IDLE, - STATE_MOVING, - STATE_STOPPING, - STATE_CALIBRATING - }; + enum State : uint8_t { STATE_IDLE, STATE_MOVING, STATE_STOPPING, STATE_CALIBRATING }; uint32_t last_recompute_time_{0}; uint32_t last_publish_time_{0};