From 2091be83126968c4e57316116474578dbfaaa6bd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 26 Oct 2023 12:04:35 -0500 Subject: [PATCH] Use random client_id in lieu of static remote_id (#76) Co-authored-by: Paul Wieland --- README.md | 18 +++--------------- base.yaml | 9 ++++++++- components/ratgdo/__init__.py | 7 ------- components/ratgdo/number/__init__.py | 1 + components/ratgdo/number/ratgdo_number.cpp | 18 ++++++++++++++---- components/ratgdo/number/ratgdo_number.h | 1 + components/ratgdo/ratgdo.cpp | 6 +++--- components/ratgdo/ratgdo.h | 5 ++--- static/index.html | 8 -------- 9 files changed, 32 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 2f66c11..6f60de4 100644 --- a/README.md +++ b/README.md @@ -5,19 +5,11 @@ This is a port of the ratgdo software for the v2.0/v2.5 board to ESPHome. [Visit the github.io page to purchase boards](https://paulwieland.github.io/ratgdo/#order) -## Moving from stock ratgdo +## Installation -If you have not used the stock ratgdo firmware, and are starting with a fresh install of the ESPHome based firmware, [skip these steps](#fresh-install-when-the-stock-ratgdo-firmware-was-not-used-before). +- Flash the ESPHome based firmware using the [Web Installer](https://ratgdo.github.io/esphome-ratgdo/) -- Use the [`Logs & Console`](https://paulwieland.github.io/ratgdo/flash.html) to view and make note of the current rolling code -- Flash the new ESPHome based firmware using the [Web Installer](https://esphome-ratgdo.github.io/esphome-ratgdo/) -- Use the `number` entity in Home Assistant or the built-in ESPHome web-server to set the rolling code. - -number_entity - -## Fresh install when the stock ratgdo firmware was not used before - -- Flash the new ESPHome based firmware using the [Web Installer](https://ratgdo.github.io/esphome-ratgdo/) +It is no longer necessary to save the rolling code counter when switching between firmware. ## First use after adding to Home Assistant @@ -25,10 +17,6 @@ The ESPHome firmware will allow you to open the door to any position after calib position_demo -## Updating from versions older than 2023.07.07 - -When updating from older versions, save the rolling counter value and restore it via the number entity after flashing the new firmware. If you forget to save the code, check the Home Assistant history. - ## ESPHome config - [ESPHome config for v2.0 board with ESP8266 D1 Mini lite](https://github.com/RATGDO/esphome-ratgdo/blob/main/static/v2board_esp8266_d1_mini_lite.yaml) diff --git a/base.yaml b/base.yaml index 86e1ef7..a44560e 100644 --- a/base.yaml +++ b/base.yaml @@ -14,7 +14,6 @@ ratgdo: input_gdo_pin: ${uart_rx_pin} output_gdo_pin: ${uart_tx_pin} input_obst_pin: ${input_obst_pin} - remote_id: 0x539 on_sync_failed: then: - homeassistant.service: @@ -161,6 +160,14 @@ number: name: "Closing duration" unit_of_measurement: "s" + - platform: ratgdo + id: ${id_prefix}_client_id + type: client_id + entity_category: config + ratgdo_id: ${id_prefix} + name: "Client ID" + mode: box + cover: - platform: ratgdo id: ${id_prefix}_garage_door diff --git a/components/ratgdo/__init__.py b/components/ratgdo/__init__.py index 076caa7..d584c33 100644 --- a/components/ratgdo/__init__.py +++ b/components/ratgdo/__init__.py @@ -24,9 +24,6 @@ DEFAULT_INPUT_GDO = ( CONF_INPUT_OBST = "input_obst_pin" DEFAULT_INPUT_OBST = "D7" # D7 black obstruction sensor terminal -CONF_REMOTE_ID = "remote_id" -DEFAULT_REMOTE_ID = 0x539 - CONF_RATGDO_ID = "ratgdo_id" CONF_ON_SYNC_FAILED = "on_sync_failed" @@ -44,9 +41,6 @@ CONFIG_SCHEMA = cv.Schema( cv.Optional( CONF_INPUT_OBST, default=DEFAULT_INPUT_OBST ): pins.gpio_input_pin_schema, - cv.Optional( - CONF_REMOTE_ID, default=DEFAULT_REMOTE_ID - ): cv.uint64_t, cv.Optional(CONF_ON_SYNC_FAILED): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SyncFailed), @@ -76,7 +70,6 @@ async def to_code(config): cg.add(var.set_input_gdo_pin(pin)) pin = await cg.gpio_pin_expression(config[CONF_INPUT_OBST]) cg.add(var.set_input_obst_pin(pin)) - cg.add(var.set_remote_id(config[CONF_REMOTE_ID])) for conf in config.get(CONF_ON_SYNC_FAILED, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) diff --git a/components/ratgdo/number/__init__.py b/components/ratgdo/number/__init__.py index bf56e8f..59bd7d2 100644 --- a/components/ratgdo/number/__init__.py +++ b/components/ratgdo/number/__init__.py @@ -12,6 +12,7 @@ NumberType = ratgdo_ns.enum("NumberType") CONF_TYPE = "type" TYPES = { + "client_id": NumberType.RATGDO_CLIENT_ID, "rolling_code_counter": NumberType.RATGDO_ROLLING_CODE_COUNTER, "opening_duration": NumberType.RATGDO_OPENING_DURATION, "closing_duration": NumberType.RATGDO_CLOSING_DURATION, diff --git a/components/ratgdo/number/ratgdo_number.cpp b/components/ratgdo/number/ratgdo_number.cpp index 27152e0..f78b93b 100644 --- a/components/ratgdo/number/ratgdo_number.cpp +++ b/components/ratgdo/number/ratgdo_number.cpp @@ -10,7 +10,9 @@ namespace ratgdo { void RATGDONumber::dump_config() { LOG_NUMBER("", "RATGDO Number", this); - if (this->number_type_ == RATGDO_ROLLING_CODE_COUNTER) { + if (this->number_type_ == RATGDO_CLIENT_ID) { + ESP_LOGCONFIG(TAG, " Type: Client ID"); + } else if (this->number_type_ == RATGDO_ROLLING_CODE_COUNTER) { ESP_LOGCONFIG(TAG, " Type: Rolling Code Counter"); } else if (this->number_type_ == RATGDO_OPENING_DURATION) { ESP_LOGCONFIG(TAG, " Type: Opening Duration"); @@ -24,7 +26,11 @@ namespace ratgdo { float value; this->pref_ = global_preferences->make_preference(this->get_object_id_hash()); if (!this->pref_.load(&value)) { - value = 0; + if (this->number_type_ == RATGDO_CLIENT_ID) { + value = random(0x1, 0xFFFF); + } else { + value = 0; + } } this->publish_state(value); this->control(value); @@ -51,9 +57,11 @@ namespace ratgdo { this->traits.set_step(0.1); this->traits.set_min_value(0.0); this->traits.set_max_value(180.0); - } - if (this->number_type_ == RATGDO_ROLLING_CODE_COUNTER) { + } else if (this->number_type_ == RATGDO_ROLLING_CODE_COUNTER) { this->traits.set_max_value(0xfffffff); + } else if (this->number_type_ == RATGDO_CLIENT_ID) { + // not sure how large remote_id can be, assuming not more than 24 bits + this->traits.set_max_value(0xffffff); } } @@ -71,6 +79,8 @@ namespace ratgdo { this->parent_->set_opening_duration(value); } else if (this->number_type_ == RATGDO_CLOSING_DURATION) { this->parent_->set_closing_duration(value); + } else if (this->number_type_ == RATGDO_CLIENT_ID) { + this->parent_->set_client_id(value); } this->pref_.save(&value); } diff --git a/components/ratgdo/number/ratgdo_number.h b/components/ratgdo/number/ratgdo_number.h index 08ec6bd..e5f78f6 100644 --- a/components/ratgdo/number/ratgdo_number.h +++ b/components/ratgdo/number/ratgdo_number.h @@ -9,6 +9,7 @@ namespace esphome { namespace ratgdo { enum NumberType { + RATGDO_CLIENT_ID, RATGDO_ROLLING_CODE_COUNTER, RATGDO_OPENING_DURATION, RATGDO_CLOSING_DURATION, diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index 251ad8f..9ff2bb3 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -87,7 +87,7 @@ namespace ratgdo { LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_); } ESP_LOGCONFIG(TAG, " Rolling Code Counter: %d", *this->rolling_code_counter); - ESP_LOGCONFIG(TAG, " Remote ID: %d", this->remote_id_); + ESP_LOGCONFIG(TAG, " Client ID: %d", this->client_id_); } uint16_t RATGDOComponent::decode_packet(const WirePacket& packet) @@ -101,7 +101,7 @@ namespace ratgdo { uint16_t cmd = ((fixed >> 24) & 0xf00) | (data & 0xff); data &= ~0xf000; // clear parity nibble - if ((fixed & 0xfffffff) == this->remote_id_) { // my commands + if ((fixed & 0xfffffff) == this->client_id_) { // my commands ESP_LOG1(TAG, "[%ld] received mine: rolling=%07" PRIx32 " fixed=%010" PRIx64 " data=%08" PRIx32, millis(), rolling, fixed, data); return static_cast(Command::UNKNOWN); } else { @@ -279,7 +279,7 @@ namespace ratgdo { void RATGDOComponent::encode_packet(Command command, uint32_t data, bool increment, WirePacket& packet) { auto cmd = static_cast(command); - uint64_t fixed = ((cmd & ~0xff) << 24) | this->remote_id_; + uint64_t fixed = ((cmd & ~0xff) << 24) | this->client_id_; uint32_t send_data = (data << 8) | (cmd & 0xff); ESP_LOG2(TAG, "[%ld] Encode for transmit rolling=%07" PRIx32 " fixed=%010" PRIx64 " data=%08" PRIx32, millis(), *this->rolling_code_counter, fixed, send_data); diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index 9a27136..e629796 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -133,8 +133,7 @@ namespace ratgdo { void set_output_gdo_pin(InternalGPIOPin* pin) { this->output_gdo_pin_ = pin; } void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; } void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; } - void set_remote_id(uint64_t remote_id) { this->remote_id_ = remote_id & 0xffffff; } // not sure how large remote_id can be, assuming not more than 24 bits - uint64_t get_remote_id() { return this->remote_id_; } + void set_client_id(uint64_t client_id) { this->client_id_ = client_id & 0xffffff; } // not sure how large client_id can be, assuming not more than 24 bits void gdo_state_loop(); uint16_t decode_packet(const WirePacket& packet); @@ -206,7 +205,7 @@ namespace ratgdo { InternalGPIOPin* output_gdo_pin_; InternalGPIOPin* input_gdo_pin_; InternalGPIOPin* input_obst_pin_; - uint64_t remote_id_; + uint64_t client_id_ { 0x539 }; }; // RATGDOComponent diff --git a/static/index.html b/static/index.html index a42042a..3cb1e06 100644 --- a/static/index.html +++ b/static/index.html @@ -155,9 +155,6 @@ Pick your board to flash your ratgdo board with ESPhome for ratgdo. No programming or other software required.

-

- Be sure to save the value of the rolling code counter and set it again or the board will not be able to control the garage door opener. If you forget to do this, you may be able to restore control by pressing the learn button, or factory resetting the garage door opener by pressing and holding the learn button 3 times for 10 seconds. -

-

Updating from versions older than 2023.07.07

-

- When updating from older versions, save the rolling counter value and restore it via the number entity after flashing the new firmware. If you forget to save the code, check the Home Assistant history. -

-

Advanced Users