tweak
This commit is contained in:
parent
ec95baa708
commit
b2b38630a8
|
@ -0,0 +1,36 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID
|
||||
from esphome.components import number
|
||||
from .. import (
|
||||
ratgdo_ns,
|
||||
register_ratgdo_child,
|
||||
RATGDO_CLIENT_SCHMEA
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["ratgdo"]
|
||||
|
||||
RATGDONumber = ratgdo_ns.class_(
|
||||
"RATGDONumber", number.Number, cg.Component
|
||||
)
|
||||
NumberType = ratgdo_ns.enum("NumberType")
|
||||
|
||||
CONF_TYPE = "type"
|
||||
TYPES = {
|
||||
"rolling_code_counter": NumberType.RATGDO_ROLLING_CODE_COUNTER,
|
||||
}
|
||||
|
||||
|
||||
CONFIG_SCHEMA = number.number_schema(RATGDONumber).extend(
|
||||
{
|
||||
cv.Required(CONF_TYPE): cv.enum(TYPES, lower=True),
|
||||
}
|
||||
).extend(RATGDO_CLIENT_SCHMEA)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await number.register_number(var, config, step=1)
|
||||
await cg.register_component(var, config)
|
||||
cg.add(var.set_number_type(config[CONF_TYPE]))
|
||||
await register_ratgdo_child(var, config)
|
|
@ -0,0 +1,28 @@
|
|||
#include "ratgdo_number.h"
|
||||
#include "../ratgdo_state.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ratgdo {
|
||||
|
||||
static const char* const TAG = "ratgdo.number";
|
||||
|
||||
void RATGDONumber::dump_config()
|
||||
{
|
||||
LOG_BINARY_SENSOR("", "RATGDO Number", this);
|
||||
ESP_LOGCONFIG(TAG, " Type: Rolling Code Counter");
|
||||
}
|
||||
|
||||
void RATGDONumber::on_rolling_code_change(uint32_t rollingCodeCounter)
|
||||
{
|
||||
this->publish_state(rollingCodeCounter);
|
||||
}
|
||||
void RATGDONumber::control(float value)
|
||||
{
|
||||
ESP_LOGD(TAG, "name: %s this->type_:%d control: %f", this->get_name(), this->number_type_, value);
|
||||
this->parent_->setRollingCodeCounter(value);
|
||||
this->publish_state(value);
|
||||
}
|
||||
|
||||
} // namespace ratgdo
|
||||
} // namespace esphome
|
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include "../ratgdo.h"
|
||||
#include "../ratgdo_child.h"
|
||||
#include "../ratgdo_state.h"
|
||||
#include "esphome/components/number/number.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ratgdo {
|
||||
|
||||
enum NumberType {
|
||||
RATGDO_ROLLING_CODE_COUNTER
|
||||
};
|
||||
|
||||
class RATGDONumber : public number::Number, public RATGDOClient, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void set_number_type(NumberType number_type_) { this->number_type_ = number_type_; }
|
||||
|
||||
void on_rolling_code_change(uint32_t rollingCodeCounter) override;
|
||||
void RATGDONumber::control(float value) override;
|
||||
|
||||
protected:
|
||||
NumberType number_type_;
|
||||
};
|
||||
|
||||
} // namespace ratgdo
|
||||
} // namespace esphome
|
|
@ -262,10 +262,23 @@ namespace ratgdo {
|
|||
return;
|
||||
}
|
||||
|
||||
void RATGDOComponent::setRollingCodeCounter(uint32_t counter)
|
||||
{
|
||||
ESP_LOGD(TAG, "Set rolling code counter to %d", counter);
|
||||
this->rollingCodeCounter = counter;
|
||||
this->pref_.save(&this->rollingCodeCounter);
|
||||
sendRollingCodeChanged();
|
||||
}
|
||||
|
||||
void RATGDOComponent::incrementRollingCodeCounter()
|
||||
{
|
||||
ESP_LOGD(TAG, "Incrementing rolling code counter");
|
||||
this->rollingCodeCounter = (this->rollingCodeCounter + 1) & 0xfffffff;
|
||||
sendRollingCodeChanged();
|
||||
}
|
||||
|
||||
void RATGDOComponent::sendRollingCodeChanged()
|
||||
{
|
||||
for (auto* child : this->children_) {
|
||||
child->on_rolling_code_change(this->rollingCodeCounter);
|
||||
}
|
||||
|
|
|
@ -127,6 +127,8 @@ namespace ratgdo {
|
|||
void statusUpdateLoop();
|
||||
void readRollingCode(uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction);
|
||||
void incrementRollingCodeCounter();
|
||||
void sendRollingCodeChanged();
|
||||
void setRollingCodeCounter(uint32_t counter);
|
||||
void sendCommandAndSaveCounter(Commands command);
|
||||
/** Register a child component. */
|
||||
void register_child(RATGDOClient* obj);
|
||||
|
|
Loading…
Reference in New Issue