From 9f8e0401b8a9d6381597308ac25310f33c0139f5 Mon Sep 17 00:00:00 2001 From: Marius Muja Date: Sun, 8 Oct 2023 21:36:09 -0700 Subject: [PATCH] Added workaround to stop the door by sending toggle twice for those garage openers that ignore the stop command while door is closing. --- base.yaml | 12 ++++++++++++ components/ratgdo/ratgdo.cpp | 26 ++++++++++++++++++++++---- components/ratgdo/ratgdo.h | 4 ++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/base.yaml b/base.yaml index 68e8977..0afd2bc 100644 --- a/base.yaml +++ b/base.yaml @@ -58,6 +58,18 @@ switch: output: true name: "Status obstruction" entity_category: diagnostic + - platform: template + id: ${id_prefix}_stop_while_closing_workaround + entity_category: config + name: "Stop while closing workaround" + optimistic: true + restore_mode: RESTORE_DEFAULT_OFF + turn_on_action: + - lambda: |- + id(${id_prefix})->set_stop_while_closing_workaround(true); + turn_off_action: + - lambda: |- + id(${id_prefix})->set_stop_while_closing_workaround(false); binary_sensor: - platform: ratgdo diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index b6ad370..3d05c24 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -16,8 +16,8 @@ #include "esphome/core/log.h" -#define ESP_LOG1 ESP_LOGV -#define ESP_LOG2 ESP_LOGV +#define ESP_LOG1 ESP_LOGD +#define ESP_LOG2 ESP_LOGD namespace esphome { namespace ratgdo { @@ -275,8 +275,8 @@ namespace ratgdo { auto now = millis(); auto duration = this->door_move_delta > 0 ? *this->opening_duration : -*this->closing_duration; auto position = this->door_start_position + (now - this->door_start_moving) / (1000 * duration); - ESP_LOG2(TAG, "[%d] Position update: %f", now, position); this->door_position = clamp(position, 0.0f, 1.0f); + ESP_LOG2(TAG, "[%d] Position update: %f", now, position); } void RATGDOComponent::encode_packet(Command command, uint32_t data, bool increment, WirePacket& packet) @@ -397,6 +397,7 @@ namespace ratgdo { if (ser_byte != 0x55 && ser_byte != 0x01 && ser_byte != 0x00) { ESP_LOG2(TAG, "Ignoring byte: %02X, baud: %d", ser_byte, this->sw_serial_.baudRate()); byte_count = 0; + ESP_LOGD(TAG, "Ignoring serial byte %02X", ser_byte); continue; } msg_start = ((msg_start << 8) | ser_byte) & 0xffffff; @@ -614,7 +615,7 @@ namespace ratgdo { } } - void RATGDOComponent::door_command(uint32_t data) + void RATGDOComponent::door_command_(uint32_t data) { data |= (1 << 16); // button 1 ? data |= (1 << 8); // button press @@ -653,6 +654,23 @@ namespace ratgdo { }); } + void RATGDOComponent::door_command(uint32_t data) + { + if (this->stop_while_closing_workaround && data==data::DOOR_STOP && *this->door_state==DoorState::CLOSING) { + ESP_LOGD(TAG, "Using double toggle door stop"); + + this->door_command_(data::DOOR_TOGGLE); + this->door_state_received.then([=](DoorState s) { + if (s==DoorState::OPENING) { + this->door_command(data::DOOR_TOGGLE); + } + }); + } else { + ESP_LOGD(TAG, "Door command: %X", data); + this->door_command_(data); + } + } + void RATGDOComponent::light_on() { this->light_state = LightState::ON; diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index a42af7e..aa19646 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -109,6 +109,8 @@ namespace ratgdo { float start_closing { -1 }; observable closing_duration { 0 }; + bool stop_while_closing_workaround { false }; + observable openings { 0 }; // number of times the door has been opened observable door_state { DoorState::UNKNOWN }; @@ -148,6 +150,7 @@ namespace ratgdo { void set_rolling_code_counter(uint32_t code); // door + void door_command_(uint32_t data); void door_command(uint32_t data); void ensure_door_command(uint32_t data, uint32_t delay = 1500); void toggle_door(); @@ -161,6 +164,7 @@ namespace ratgdo { void schedule_door_position_sync(float update_period = 500); void door_position_update(); void cancel_position_sync_callbacks(); + void set_stop_while_closing_workaround(bool enabled) { this->stop_while_closing_workaround = enabled; } // light void toggle_light();