diff --git a/esphome/components/endstop/endstop_cover.cpp b/esphome/components/endstop/endstop_cover.cpp index 67c6a4ebd3..f468d13492 100644 --- a/esphome/components/endstop/endstop_cover.cpp +++ b/esphome/components/endstop/endstop_cover.cpp @@ -12,6 +12,7 @@ using namespace esphome::cover; CoverTraits EndstopCover::get_traits() { auto traits = CoverTraits(); traits.set_supports_position(true); + traits.set_supports_toggle(true); traits.set_is_assumed_state(false); return traits; } @@ -20,6 +21,20 @@ void EndstopCover::control(const CoverCall &call) { this->start_direction_(COVER_OPERATION_IDLE); this->publish_state(); } + if (call.get_toggle().has_value()) { + if (this->current_operation != COVER_OPERATION_IDLE) { + this->start_direction_(COVER_OPERATION_IDLE); + this->publish_state(); + } else { + if (this->position == COVER_CLOSED || this->last_operation_ == COVER_OPERATION_CLOSING) { + this->target_position_ = COVER_OPEN; + this->start_direction_(COVER_OPERATION_OPENING); + } else { + this->target_position_ = COVER_CLOSED; + this->start_direction_(COVER_OPERATION_CLOSING); + } + } + } if (call.get_position().has_value()) { auto pos = *call.get_position(); if (pos == this->position) { @@ -125,9 +140,11 @@ void EndstopCover::start_direction_(CoverOperation dir) { trig = this->stop_trigger_; break; case COVER_OPERATION_OPENING: + this->last_operation_ = dir; trig = this->open_trigger_; break; case COVER_OPERATION_CLOSING: + this->last_operation_ = dir; trig = this->close_trigger_; break; default: diff --git a/esphome/components/endstop/endstop_cover.h b/esphome/components/endstop/endstop_cover.h index f8d2746234..6ae15de8c1 100644 --- a/esphome/components/endstop/endstop_cover.h +++ b/esphome/components/endstop/endstop_cover.h @@ -51,6 +51,7 @@ class EndstopCover : public cover::Cover, public Component { uint32_t start_dir_time_{0}; uint32_t last_publish_time_{0}; float target_position_{0}; + cover::CoverOperation last_operation_{cover::COVER_OPERATION_OPENING}; }; } // namespace endstop diff --git a/tests/test3.yaml b/tests/test3.yaml index 853d7bd389..f0975f9918 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -795,6 +795,7 @@ binary_sensor: on_press: then: - cover.toggle: time_based_cover + - cover.toggle: endstop_cover - platform: template id: 'pzemac_reset_energy' on_press: @@ -1060,6 +1061,7 @@ climate: cover: - platform: endstop name: Endstop Cover + id: endstop_cover stop_action: - switch.turn_on: gpio_switch1 open_endstop: my_binary_sensor