Convert lock remotes switch to a lock component (#132)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
ffbef7901f
commit
2ea7ca6e3e
|
@ -33,13 +33,13 @@ sensor:
|
|||
unit_of_measurement: "openings"
|
||||
icon: mdi:open-in-app
|
||||
|
||||
switch:
|
||||
lock:
|
||||
- platform: ratgdo
|
||||
id: ${id_prefix}_lock_remotes
|
||||
type: lock
|
||||
entity_category: config
|
||||
ratgdo_id: ${id_prefix}
|
||||
name: "Lock remotes"
|
||||
|
||||
switch:
|
||||
- platform: gpio
|
||||
id: "${id_prefix}_status_door"
|
||||
internal: true
|
||||
|
|
|
@ -1,35 +1,26 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import switch
|
||||
from esphome.components import lock
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
from .. import RATGDO_CLIENT_SCHMEA, ratgdo_ns, register_ratgdo_child
|
||||
|
||||
DEPENDENCIES = ["ratgdo"]
|
||||
|
||||
RATGDOSwitch = ratgdo_ns.class_("RATGDOSwitch", switch.Switch, cg.Component)
|
||||
SwitchType = ratgdo_ns.enum("SwitchType")
|
||||
|
||||
CONF_TYPE = "type"
|
||||
TYPES = {
|
||||
"lock": SwitchType.RATGDO_LOCK,
|
||||
}
|
||||
|
||||
RATGDOLock = ratgdo_ns.class_("RATGDOLock", lock.Lock, cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
switch.switch_schema(RATGDOSwitch)
|
||||
lock.LOCK_SCHEMA
|
||||
.extend(
|
||||
{
|
||||
cv.Required(CONF_TYPE): cv.enum(TYPES, lower=True),
|
||||
cv.GenerateID(): cv.declare_id(RATGDOLock),
|
||||
}
|
||||
)
|
||||
.extend(RATGDO_CLIENT_SCHMEA)
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await switch.register_switch(var, config)
|
||||
await lock.register_lock(var, config)
|
||||
await cg.register_component(var, config)
|
||||
cg.add(var.set_switch_type(config[CONF_TYPE]))
|
||||
await register_ratgdo_child(var, config)
|
|
@ -0,0 +1,55 @@
|
|||
#include "ratgdo_lock.h"
|
||||
#include "../ratgdo_state.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ratgdo {
|
||||
|
||||
static const char* const TAG = "ratgdo.lock";
|
||||
|
||||
void RATGDOLock::dump_config()
|
||||
{
|
||||
LOG_LOCK("", "RATGDO Lock", this);
|
||||
ESP_LOGCONFIG(TAG, " Type: Lock");
|
||||
}
|
||||
|
||||
void RATGDOLock::setup()
|
||||
{
|
||||
this->parent_->subscribe_lock_state([=](LockState state) {
|
||||
this->on_lock_state(state);
|
||||
});
|
||||
}
|
||||
|
||||
void RATGDOLock::on_lock_state(LockState state)
|
||||
{
|
||||
if (state == LockState::LOCKED && this->state == lock::LockState::LOCK_STATE_LOCKED) {
|
||||
return;
|
||||
}
|
||||
if (state == LockState::UNLOCKED && this->state == lock::LockState::LOCK_STATE_UNLOCKED) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto call = this->make_call();
|
||||
if (state == LockState::LOCKED) {
|
||||
call.set_state(lock::LockState::LOCK_STATE_LOCKED);
|
||||
} else if (state == LockState::UNLOCKED) {
|
||||
call.set_state(lock::LockState::LOCK_STATE_UNLOCKED);
|
||||
}
|
||||
this->control(call);
|
||||
}
|
||||
|
||||
void RATGDOLock::control(const lock::LockCall& call)
|
||||
{
|
||||
auto state = *call.get_state();
|
||||
|
||||
if (state == lock::LockState::LOCK_STATE_LOCKED) {
|
||||
this->parent_->lock();
|
||||
} else if (state == lock::LockState::LOCK_STATE_UNLOCKED) {
|
||||
this->parent_->unlock();
|
||||
}
|
||||
|
||||
this->publish_state(state);
|
||||
}
|
||||
|
||||
} // namespace ratgdo
|
||||
} // namespace esphome
|
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#include "../ratgdo.h"
|
||||
#include "../ratgdo_state.h"
|
||||
#include "esphome/components/lock/lock.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ratgdo {
|
||||
|
||||
class RATGDOLock : public lock::Lock, public RATGDOClient, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void setup() override;
|
||||
|
||||
void on_lock_state(LockState state);
|
||||
void control(const lock::LockCall& call) override;
|
||||
};
|
||||
|
||||
} // namespace ratgdo
|
||||
} // namespace esphome
|
|
@ -1,39 +0,0 @@
|
|||
#include "ratgdo_switch.h"
|
||||
#include "../ratgdo_state.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ratgdo {
|
||||
|
||||
static const char* const TAG = "ratgdo.switch";
|
||||
|
||||
void RATGDOSwitch::dump_config()
|
||||
{
|
||||
LOG_SWITCH("", "RATGDO Switch", this);
|
||||
ESP_LOGCONFIG(TAG, " Type: Lock");
|
||||
}
|
||||
|
||||
void RATGDOSwitch::setup()
|
||||
{
|
||||
this->parent_->subscribe_lock_state([=](LockState state) {
|
||||
this->on_lock_state(state);
|
||||
});
|
||||
}
|
||||
|
||||
void RATGDOSwitch::on_lock_state(LockState state)
|
||||
{
|
||||
bool value = state == LockState::LOCKED;
|
||||
this->state = value;
|
||||
this->publish_state(value);
|
||||
}
|
||||
void RATGDOSwitch::write_state(bool state)
|
||||
{
|
||||
if (state) {
|
||||
this->parent_->lock();
|
||||
} else {
|
||||
this->parent_->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ratgdo
|
||||
} // namespace esphome
|
|
@ -1,29 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "../ratgdo.h"
|
||||
#include "../ratgdo_state.h"
|
||||
#include "esphome/components/switch/switch.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ratgdo {
|
||||
|
||||
enum SwitchType {
|
||||
RATGDO_LOCK
|
||||
};
|
||||
|
||||
class RATGDOSwitch : public switch_::Switch, public RATGDOClient, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void setup() override;
|
||||
void set_switch_type(SwitchType switch_type_) { this->switch_type_ = switch_type_; }
|
||||
|
||||
void on_lock_state(LockState state);
|
||||
void write_state(bool state) override;
|
||||
|
||||
protected:
|
||||
SwitchType switch_type_;
|
||||
};
|
||||
|
||||
} // namespace ratgdo
|
||||
} // namespace esphome
|
Loading…
Reference in New Issue