diff --git a/components/ratgdo/number/__init__.py b/components/ratgdo/number/__init__.py index f5a0b5d..a0f4cbd 100644 --- a/components/ratgdo/number/__init__.py +++ b/components/ratgdo/number/__init__.py @@ -16,7 +16,6 @@ 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 1d89cbe..2139c81 100644 --- a/components/ratgdo/number/ratgdo_number.cpp +++ b/components/ratgdo/number/ratgdo_number.cpp @@ -5,7 +5,6 @@ namespace esphome::ratgdo { using protocol::SetClientID; -using protocol::SetRollingCodeCounter; float normalize_client_id(float client_id) { @@ -25,9 +24,6 @@ void RATGDONumber::dump_config() case RATGDO_CLIENT_ID: ESP_LOGCONFIG(TAG, " Type: Client ID"); break; - case RATGDO_ROLLING_CODE_COUNTER: - ESP_LOGCONFIG(TAG, " Type: Rolling Code Counter"); - break; case RATGDO_OPENING_DURATION: ESP_LOGCONFIG(TAG, " Type: Opening Duration"); break; @@ -61,11 +57,6 @@ void RATGDONumber::setup() this->control(value); switch (this->number_type_) { - case RATGDO_ROLLING_CODE_COUNTER: - this->parent_->subscribe_rolling_code_counter([this](uint32_t value) { - this->update_state(value); - }); - break; case RATGDO_OPENING_DURATION: this->parent_->subscribe_opening_duration([this](float value) { this->update_state(value); @@ -91,9 +82,6 @@ void RATGDONumber::set_number_type(NumberType number_type_) this->traits.set_min_value(0.0); this->traits.set_max_value(180.0); break; - case RATGDO_ROLLING_CODE_COUNTER: - this->traits.set_max_value(0xfffffff); - break; case RATGDO_CLIENT_ID: this->traits.set_step(0x1000); this->traits.set_min_value(0x539); @@ -116,9 +104,6 @@ void RATGDONumber::update_state(float value) void RATGDONumber::control(float value) { switch (this->number_type_) { - case RATGDO_ROLLING_CODE_COUNTER: - this->parent_->call_protocol(SetRollingCodeCounter { static_cast(value) }); - break; case RATGDO_OPENING_DURATION: this->parent_->set_opening_duration(value); break; diff --git a/components/ratgdo/number/ratgdo_number.h b/components/ratgdo/number/ratgdo_number.h index 71cf61a..d2457fa 100644 --- a/components/ratgdo/number/ratgdo_number.h +++ b/components/ratgdo/number/ratgdo_number.h @@ -10,7 +10,6 @@ namespace esphome::ratgdo { enum NumberType { RATGDO_CLIENT_ID, - RATGDO_ROLLING_CODE_COUNTER, RATGDO_OPENING_DURATION, RATGDO_CLOSING_DURATION, }; diff --git a/components/ratgdo/protocol.h b/components/ratgdo/protocol.h index 47b921f..1a773cd 100644 --- a/components/ratgdo/protocol.h +++ b/components/ratgdo/protocol.h @@ -16,11 +16,6 @@ class RATGDOComponent; namespace protocol { - struct SetRollingCodeCounter { - uint32_t counter; - }; - struct GetRollingCodeCounter { - }; struct SetClientID { uint64_t client_id; }; @@ -31,18 +26,11 @@ namespace protocol { // a poor man's sum-type, because C++ SUM_TYPE(Args, - (SetRollingCodeCounter, set_rolling_code_counter), - (GetRollingCodeCounter, get_rolling_code_counter), (SetClientID, set_client_id), (QueryStatus, query_status), (QueryOpenings, query_openings), ) - struct RollingCodeCounter { - single_observable* value; - }; - - SUM_TYPE(Result, - (RollingCodeCounter, rolling_code_counter), ) + SUM_TYPE(Result, ) class Protocol { public: diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index a74e9e3..9f85ddc 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -166,8 +166,6 @@ public: // (3 * sizeof(void*)), e.g. [this] or [this, f] lambdas. // Enforced at compile time by Callback::create(). template - void subscribe_rolling_code_counter(F&& f); - template void subscribe_opening_duration(F&& f); template void subscribe_closing_duration(F&& f); @@ -230,8 +228,7 @@ namespace scheduler_ids { // Single-subscriber IDs enum : uint32_t { - DEFER_ROLLING_CODE = DEFER_DISTANCE_END, - DEFER_OPENING_DURATION, + DEFER_OPENING_DURATION = DEFER_DISTANCE_END, DEFER_CLOSING_DURATION, DEFER_OPENINGS, DEFER_LIGHT_STATE, @@ -251,19 +248,6 @@ namespace scheduler_ids { // fires multiple times during one loop iteration, only the last value // is dispatched to the child component. -template -void RATGDOComponent::subscribe_rolling_code_counter(F&& f) -{ - // change update to children is defered until after component loop - // if multiple changes occur during component loop, only the last one is notified - auto counter = this->protocol_->call(protocol::GetRollingCodeCounter { }); - if (counter.tag == protocol::Result::Tag::rolling_code_counter) { - counter.value.rolling_code_counter.value->subscribe([this, f](uint32_t state) { - defer(scheduler_ids::DEFER_ROLLING_CODE, [f, state] { f(state); }); - }); - } -} - template void RATGDOComponent::subscribe_opening_duration(F&& f) { diff --git a/components/ratgdo/secplus2.cpp b/components/ratgdo/secplus2.cpp index 12bafd0..fb5fb02 100644 --- a/components/ratgdo/secplus2.cpp +++ b/components/ratgdo/secplus2.cpp @@ -35,6 +35,17 @@ namespace secplus2 { this->tx_pin_ = tx_pin; this->rx_pin_ = rx_pin; + this->rolling_code_pref_ = global_preferences->make_preference(1868352652U); // fnv1_hash("ratgdo_rolling_code") + uint32_t rolling_code; + if (this->rolling_code_pref_.load(&rolling_code)) { + this->rolling_code_counter_ = rolling_code; + ESP_LOGI(TAG, "Restored rolling code from flash: %u", rolling_code); + } else { + rolling_code = 1; + this->rolling_code_counter_ = rolling_code; + this->rolling_code_pref_.save(&rolling_code); + } + this->uart_.begin(9600, RATGDO_UART_8N1, rx_pin->get_pin(), tx_pin->get_pin(), true); this->uart_.enableIntTx(false); this->uart_.enableAutoBaud(true); @@ -138,10 +149,6 @@ namespace secplus2 { this->send_command(CommandType::GET_STATUS); } else if (args.tag == Tag::query_openings) { this->send_command(CommandType::GET_OPENINGS); - } else if (args.tag == Tag::get_rolling_code_counter) { - return Result(RollingCodeCounter { std::addressof(this->rolling_code_counter_) }); - } else if (args.tag == Tag::set_rolling_code_counter) { - this->set_rolling_code_counter(args.value.set_rolling_code_counter.counter); } else if (args.tag == Tag::set_client_id) { this->set_client_id(args.value.set_client_id.client_id); } @@ -348,13 +355,16 @@ namespace secplus2 { void Secplus2::increment_rolling_code_counter(int delta) { - this->rolling_code_counter_ = (*this->rolling_code_counter_ + delta) & 0xfffffff; + uint32_t counter = (*this->rolling_code_counter_ + delta) & 0xfffffff; + this->rolling_code_counter_ = counter; + this->rolling_code_pref_.save(&counter); } void Secplus2::set_rolling_code_counter(uint32_t counter) { ESP_LOGV(TAG, "Set rolling code counter to %d", counter); this->rolling_code_counter_ = counter; + this->rolling_code_pref_.save(&counter); } void Secplus2::set_client_id(uint64_t client_id) diff --git a/components/ratgdo/secplus2.h b/components/ratgdo/secplus2.h index 3ee3fd1..b8f6544 100644 --- a/components/ratgdo/secplus2.h +++ b/components/ratgdo/secplus2.h @@ -1,6 +1,7 @@ #pragma once #include "esphome/core/optional.h" +#include "esphome/core/preferences.h" #include "ratgdo_uart.h" #include "callbacks.h" @@ -136,6 +137,7 @@ namespace secplus2 { // Larger structures single_observable rolling_code_counter_ { 0 }; + ESPPreferenceObject rolling_code_pref_; OnceCallbacks on_command_sent_; RatgdoUART uart_; diff --git a/garage-gate.yaml b/garage-gate.yaml index 30e592f..8e73cd7 100644 --- a/garage-gate.yaml +++ b/garage-gate.yaml @@ -102,13 +102,6 @@ switch: entity_category: config number: - - platform: ratgdo - id: garage_gate_rolling_code_counter - type: rolling_code_counter - entity_category: config - name: "Rolling code counter" - mode: box - unit_of_measurement: "codes" - platform: ratgdo id: garage_gate_opening_duration type: opening_duration