From 48f5760a173bae710355a230ea029ae54529d8a2 Mon Sep 17 00:00:00 2001 From: Artem Sheremet Date: Fri, 22 May 2026 21:09:46 +0000 Subject: [PATCH] Add debug info and fix rolling code startup --- components/ratgdo/ratgdo.cpp | 2 +- components/ratgdo/ratgdo.h | 2 ++ components/ratgdo/secplus2.cpp | 32 +++++++++++++++++--------------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index add3ff0..7dde5b7 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -26,7 +26,7 @@ namespace esphome::ratgdo { using namespace protocol; static const char* const TAG = "ratgdo"; -static constexpr int SYNC_DELAY = 1000; +static constexpr int SYNC_DELAY = 5000; // Door state updates arrive over UART every ~200-400ms during movement. // 2 seconds gives ample margin for slow openers while still expiring // stale callbacks before a user could reasonably trigger an unrelated diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index 9e56d48..a74e9e3 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -50,6 +50,8 @@ public: void dump_config() override; void on_shutdown() override; + float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } + void init_protocol(); float start_opening { -1 }; diff --git a/components/ratgdo/secplus2.cpp b/components/ratgdo/secplus2.cpp index 7788616..12bafd0 100644 --- a/components/ratgdo/secplus2.cpp +++ b/components/ratgdo/secplus2.cpp @@ -68,22 +68,21 @@ namespace secplus2 { void Secplus2::sync_helper(uint32_t start, uint32_t delay, uint8_t tries) { - bool synced = true; if (*this->ratgdo_->door_state == DoorState::UNKNOWN) { + ESP_LOGD(TAG, "Sync: querying status (attempt %d)...", tries); this->query_status(); - synced = false; - } - if (*this->ratgdo_->openings == 0) { + } else if (*this->ratgdo_->openings == 0) { + ESP_LOGD(TAG, "Sync: querying openings (attempt %d)...", tries); this->query_openings(); - synced = false; - } - - if (synced) { + } else { + ESP_LOGD(TAG, "Sync successful!"); return; } - if (tries == 2 && *this->ratgdo_->door_state == DoorState::UNKNOWN) { // made a few attempts and no progress (door state is the first sync request) - // increment rolling code counter by some amount in case we crashed without writing to flash the latest value + if (tries == 10 && *this->ratgdo_->door_state == DoorState::UNKNOWN) { + // After 10 failed attempts to even get status, try jumping the rolling code. + // This handles cases where the device rolling code is way behind the GDO. + ESP_LOGW(TAG, "Sync: jumping rolling code counter..."); this->increment_rolling_code_counter(MAX_CODES_WITHOUT_FLASH_WRITE); } @@ -92,17 +91,18 @@ namespace secplus2 { ESP_LOGW(TAG, "Triggering sync failed actions."); this->ratgdo_->sync_failed = true; } else { - if (tries % 3 == 0) { - delay *= 1.5; - } - this->scheduler_->set_timeout(this->ratgdo_, TIMEOUT_SYNC, delay, [this, start, delay, tries]() { - this->sync_helper(start, delay, tries + 1); + // Use a slightly longer delay between queries during sync to avoid bus saturation + uint32_t next_delay = (tries < 5) ? 1000 : 2000; + this->scheduler_->set_timeout(this->ratgdo_, TIMEOUT_SYNC, next_delay, [this, start, next_delay, tries]() { + this->sync_helper(start, next_delay, tries + 1); }); }; } void Secplus2::sync() { + ESP_LOGD(TAG, "Starting sync..."); + this->ratgdo_->sync_failed = false; this->scheduler_->cancel_timeout(this->ratgdo_, TIMEOUT_SYNC); this->sync_helper(millis(), 500, 0); } @@ -268,6 +268,8 @@ namespace secplus2 { void Secplus2::handle_command(const Command& cmd) { + ESP_LOGD(TAG, "Handle command: %s (nibble=%01x byte1=%02x byte2=%02x)", LOG_STR_ARG(CommandType_to_string(cmd.type)), cmd.nibble, cmd.byte1, cmd.byte2); + if (cmd.type == CommandType::STATUS) { this->ratgdo_->received(to_DoorState(cmd.nibble, DoorState::UNKNOWN));