From cdc7780c72bef09a1a8a51e06292a3f17f7e7378 Mon Sep 17 00:00:00 2001 From: Marius Muja Date: Tue, 7 Nov 2023 10:58:16 -0800 Subject: [PATCH] Normalize client_id when manually entered (#97) Co-authored-by: J. Nick Koston --- components/ratgdo/number/ratgdo_number.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/components/ratgdo/number/ratgdo_number.cpp b/components/ratgdo/number/ratgdo_number.cpp index 27b43fb..0f85055 100644 --- a/components/ratgdo/number/ratgdo_number.cpp +++ b/components/ratgdo/number/ratgdo_number.cpp @@ -5,6 +5,15 @@ namespace esphome { namespace ratgdo { + float normalize_client_id(float client_id) + { + uint32_t int_value = static_cast(client_id); + if ((int_value & 0xFFF) != 0x539) { + client_id = ceil((client_id - 0x539) / 0x1000) * 0x1000 + 0x539; + } + return client_id; + } + static const char* const TAG = "ratgdo.number"; void RATGDONumber::dump_config() @@ -27,12 +36,11 @@ namespace ratgdo { this->pref_ = global_preferences->make_preference(this->get_object_id_hash()); if (!this->pref_.load(&value)) { if (this->number_type_ == RATGDO_CLIENT_ID) { - value = ((random_uint32() + 1) % 0x7FF) << 12 | 0x539; // max size limited to be precisely convertible to float + value = ((random_uint32() + 1) % 0x7FF) << 12 | 0x539; // max size limited to be precisely convertible to float } else { value = 0; } - } - else { + } else { if (this->number_type_ == RATGDO_CLIENT_ID) { uint32_t int_value = static_cast(value); if ((int_value & 0xFFF) != 0x539) { @@ -41,7 +49,6 @@ namespace ratgdo { } } } - this->publish_state(value); this->control(value); if (this->number_type_ == RATGDO_ROLLING_CODE_COUNTER) { @@ -90,6 +97,7 @@ namespace ratgdo { } else if (this->number_type_ == RATGDO_CLOSING_DURATION) { this->parent_->set_closing_duration(value); } else if (this->number_type_ == RATGDO_CLIENT_ID) { + value = normalize_client_id(value); this->parent_->set_client_id(value); } this->update_state(value);