Store rolling code in flash rather than exposing it
This commit is contained in:
@@ -16,7 +16,6 @@ NumberType = ratgdo_ns.enum("NumberType")
|
|||||||
CONF_TYPE = "type"
|
CONF_TYPE = "type"
|
||||||
TYPES = {
|
TYPES = {
|
||||||
"client_id": NumberType.RATGDO_CLIENT_ID,
|
"client_id": NumberType.RATGDO_CLIENT_ID,
|
||||||
"rolling_code_counter": NumberType.RATGDO_ROLLING_CODE_COUNTER,
|
|
||||||
"opening_duration": NumberType.RATGDO_OPENING_DURATION,
|
"opening_duration": NumberType.RATGDO_OPENING_DURATION,
|
||||||
"closing_duration": NumberType.RATGDO_CLOSING_DURATION,
|
"closing_duration": NumberType.RATGDO_CLOSING_DURATION,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
namespace esphome::ratgdo {
|
namespace esphome::ratgdo {
|
||||||
|
|
||||||
using protocol::SetClientID;
|
using protocol::SetClientID;
|
||||||
using protocol::SetRollingCodeCounter;
|
|
||||||
|
|
||||||
float normalize_client_id(float client_id)
|
float normalize_client_id(float client_id)
|
||||||
{
|
{
|
||||||
@@ -25,9 +24,6 @@ void RATGDONumber::dump_config()
|
|||||||
case RATGDO_CLIENT_ID:
|
case RATGDO_CLIENT_ID:
|
||||||
ESP_LOGCONFIG(TAG, " Type: Client ID");
|
ESP_LOGCONFIG(TAG, " Type: Client ID");
|
||||||
break;
|
break;
|
||||||
case RATGDO_ROLLING_CODE_COUNTER:
|
|
||||||
ESP_LOGCONFIG(TAG, " Type: Rolling Code Counter");
|
|
||||||
break;
|
|
||||||
case RATGDO_OPENING_DURATION:
|
case RATGDO_OPENING_DURATION:
|
||||||
ESP_LOGCONFIG(TAG, " Type: Opening Duration");
|
ESP_LOGCONFIG(TAG, " Type: Opening Duration");
|
||||||
break;
|
break;
|
||||||
@@ -61,11 +57,6 @@ void RATGDONumber::setup()
|
|||||||
this->control(value);
|
this->control(value);
|
||||||
|
|
||||||
switch (this->number_type_) {
|
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:
|
case RATGDO_OPENING_DURATION:
|
||||||
this->parent_->subscribe_opening_duration([this](float value) {
|
this->parent_->subscribe_opening_duration([this](float value) {
|
||||||
this->update_state(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_min_value(0.0);
|
||||||
this->traits.set_max_value(180.0);
|
this->traits.set_max_value(180.0);
|
||||||
break;
|
break;
|
||||||
case RATGDO_ROLLING_CODE_COUNTER:
|
|
||||||
this->traits.set_max_value(0xfffffff);
|
|
||||||
break;
|
|
||||||
case RATGDO_CLIENT_ID:
|
case RATGDO_CLIENT_ID:
|
||||||
this->traits.set_step(0x1000);
|
this->traits.set_step(0x1000);
|
||||||
this->traits.set_min_value(0x539);
|
this->traits.set_min_value(0x539);
|
||||||
@@ -116,9 +104,6 @@ void RATGDONumber::update_state(float value)
|
|||||||
void RATGDONumber::control(float value)
|
void RATGDONumber::control(float value)
|
||||||
{
|
{
|
||||||
switch (this->number_type_) {
|
switch (this->number_type_) {
|
||||||
case RATGDO_ROLLING_CODE_COUNTER:
|
|
||||||
this->parent_->call_protocol(SetRollingCodeCounter { static_cast<uint32_t>(value) });
|
|
||||||
break;
|
|
||||||
case RATGDO_OPENING_DURATION:
|
case RATGDO_OPENING_DURATION:
|
||||||
this->parent_->set_opening_duration(value);
|
this->parent_->set_opening_duration(value);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ namespace esphome::ratgdo {
|
|||||||
|
|
||||||
enum NumberType {
|
enum NumberType {
|
||||||
RATGDO_CLIENT_ID,
|
RATGDO_CLIENT_ID,
|
||||||
RATGDO_ROLLING_CODE_COUNTER,
|
|
||||||
RATGDO_OPENING_DURATION,
|
RATGDO_OPENING_DURATION,
|
||||||
RATGDO_CLOSING_DURATION,
|
RATGDO_CLOSING_DURATION,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,11 +16,6 @@ class RATGDOComponent;
|
|||||||
|
|
||||||
namespace protocol {
|
namespace protocol {
|
||||||
|
|
||||||
struct SetRollingCodeCounter {
|
|
||||||
uint32_t counter;
|
|
||||||
};
|
|
||||||
struct GetRollingCodeCounter {
|
|
||||||
};
|
|
||||||
struct SetClientID {
|
struct SetClientID {
|
||||||
uint64_t client_id;
|
uint64_t client_id;
|
||||||
};
|
};
|
||||||
@@ -31,18 +26,11 @@ namespace protocol {
|
|||||||
|
|
||||||
// a poor man's sum-type, because C++
|
// a poor man's sum-type, because C++
|
||||||
SUM_TYPE(Args,
|
SUM_TYPE(Args,
|
||||||
(SetRollingCodeCounter, set_rolling_code_counter),
|
|
||||||
(GetRollingCodeCounter, get_rolling_code_counter),
|
|
||||||
(SetClientID, set_client_id),
|
(SetClientID, set_client_id),
|
||||||
(QueryStatus, query_status),
|
(QueryStatus, query_status),
|
||||||
(QueryOpenings, query_openings), )
|
(QueryOpenings, query_openings), )
|
||||||
|
|
||||||
struct RollingCodeCounter {
|
SUM_TYPE(Result, )
|
||||||
single_observable<uint32_t>* value;
|
|
||||||
};
|
|
||||||
|
|
||||||
SUM_TYPE(Result,
|
|
||||||
(RollingCodeCounter, rolling_code_counter), )
|
|
||||||
|
|
||||||
class Protocol {
|
class Protocol {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -166,8 +166,6 @@ public:
|
|||||||
// (3 * sizeof(void*)), e.g. [this] or [this, f] lambdas.
|
// (3 * sizeof(void*)), e.g. [this] or [this, f] lambdas.
|
||||||
// Enforced at compile time by Callback::create().
|
// Enforced at compile time by Callback::create().
|
||||||
template <typename F>
|
template <typename F>
|
||||||
void subscribe_rolling_code_counter(F&& f);
|
|
||||||
template <typename F>
|
|
||||||
void subscribe_opening_duration(F&& f);
|
void subscribe_opening_duration(F&& f);
|
||||||
template <typename F>
|
template <typename F>
|
||||||
void subscribe_closing_duration(F&& f);
|
void subscribe_closing_duration(F&& f);
|
||||||
@@ -230,8 +228,7 @@ namespace scheduler_ids {
|
|||||||
|
|
||||||
// Single-subscriber IDs
|
// Single-subscriber IDs
|
||||||
enum : uint32_t {
|
enum : uint32_t {
|
||||||
DEFER_ROLLING_CODE = DEFER_DISTANCE_END,
|
DEFER_OPENING_DURATION = DEFER_DISTANCE_END,
|
||||||
DEFER_OPENING_DURATION,
|
|
||||||
DEFER_CLOSING_DURATION,
|
DEFER_CLOSING_DURATION,
|
||||||
DEFER_OPENINGS,
|
DEFER_OPENINGS,
|
||||||
DEFER_LIGHT_STATE,
|
DEFER_LIGHT_STATE,
|
||||||
@@ -251,19 +248,6 @@ namespace scheduler_ids {
|
|||||||
// fires multiple times during one loop iteration, only the last value
|
// fires multiple times during one loop iteration, only the last value
|
||||||
// is dispatched to the child component.
|
// is dispatched to the child component.
|
||||||
|
|
||||||
template <typename F>
|
|
||||||
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 <typename F>
|
template <typename F>
|
||||||
void RATGDOComponent::subscribe_opening_duration(F&& f)
|
void RATGDOComponent::subscribe_opening_duration(F&& f)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -35,6 +35,17 @@ namespace secplus2 {
|
|||||||
this->tx_pin_ = tx_pin;
|
this->tx_pin_ = tx_pin;
|
||||||
this->rx_pin_ = rx_pin;
|
this->rx_pin_ = rx_pin;
|
||||||
|
|
||||||
|
this->rolling_code_pref_ = global_preferences->make_preference<uint32_t>(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_.begin(9600, RATGDO_UART_8N1, rx_pin->get_pin(), tx_pin->get_pin(), true);
|
||||||
this->uart_.enableIntTx(false);
|
this->uart_.enableIntTx(false);
|
||||||
this->uart_.enableAutoBaud(true);
|
this->uart_.enableAutoBaud(true);
|
||||||
@@ -138,10 +149,6 @@ namespace secplus2 {
|
|||||||
this->send_command(CommandType::GET_STATUS);
|
this->send_command(CommandType::GET_STATUS);
|
||||||
} else if (args.tag == Tag::query_openings) {
|
} else if (args.tag == Tag::query_openings) {
|
||||||
this->send_command(CommandType::GET_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) {
|
} else if (args.tag == Tag::set_client_id) {
|
||||||
this->set_client_id(args.value.set_client_id.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)
|
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)
|
void Secplus2::set_rolling_code_counter(uint32_t counter)
|
||||||
{
|
{
|
||||||
ESP_LOGV(TAG, "Set rolling code counter to %d", counter);
|
ESP_LOGV(TAG, "Set rolling code counter to %d", counter);
|
||||||
this->rolling_code_counter_ = counter;
|
this->rolling_code_counter_ = counter;
|
||||||
|
this->rolling_code_pref_.save(&counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Secplus2::set_client_id(uint64_t client_id)
|
void Secplus2::set_client_id(uint64_t client_id)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "esphome/core/optional.h"
|
#include "esphome/core/optional.h"
|
||||||
|
#include "esphome/core/preferences.h"
|
||||||
#include "ratgdo_uart.h"
|
#include "ratgdo_uart.h"
|
||||||
|
|
||||||
#include "callbacks.h"
|
#include "callbacks.h"
|
||||||
@@ -136,6 +137,7 @@ namespace secplus2 {
|
|||||||
|
|
||||||
// Larger structures
|
// Larger structures
|
||||||
single_observable<uint32_t> rolling_code_counter_ { 0 };
|
single_observable<uint32_t> rolling_code_counter_ { 0 };
|
||||||
|
ESPPreferenceObject rolling_code_pref_;
|
||||||
OnceCallbacks<void()> on_command_sent_;
|
OnceCallbacks<void()> on_command_sent_;
|
||||||
RatgdoUART uart_;
|
RatgdoUART uart_;
|
||||||
|
|
||||||
|
|||||||
@@ -102,13 +102,6 @@ switch:
|
|||||||
entity_category: config
|
entity_category: config
|
||||||
|
|
||||||
number:
|
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
|
- platform: ratgdo
|
||||||
id: garage_gate_opening_duration
|
id: garage_gate_opening_duration
|
||||||
type: opening_duration
|
type: opening_duration
|
||||||
|
|||||||
Reference in New Issue
Block a user