From 2ea7ca6e3e259dc5691874b7552db90e89928e14 Mon Sep 17 00:00:00 2001 From: Marius Muja Date: Tue, 5 Dec 2023 16:12:01 -0800 Subject: [PATCH] Convert lock remotes switch to a lock component (#132) Co-authored-by: J. Nick Koston --- base.yaml | 6 +- .../ratgdo/{switch => lock}/__init__.py | 19 ++----- components/ratgdo/lock/ratgdo_lock.cpp | 55 +++++++++++++++++++ components/ratgdo/lock/ratgdo_lock.h | 21 +++++++ components/ratgdo/switch/ratgdo_switch.cpp | 39 ------------- components/ratgdo/switch/ratgdo_switch.h | 29 ---------- 6 files changed, 84 insertions(+), 85 deletions(-) rename components/ratgdo/{switch => lock}/__init__.py (51%) create mode 100644 components/ratgdo/lock/ratgdo_lock.cpp create mode 100644 components/ratgdo/lock/ratgdo_lock.h delete mode 100644 components/ratgdo/switch/ratgdo_switch.cpp delete mode 100644 components/ratgdo/switch/ratgdo_switch.h diff --git a/base.yaml b/base.yaml index 5d29ea2..68e8977 100644 --- a/base.yaml +++ b/base.yaml @@ -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 diff --git a/components/ratgdo/switch/__init__.py b/components/ratgdo/lock/__init__.py similarity index 51% rename from components/ratgdo/switch/__init__.py rename to components/ratgdo/lock/__init__.py index a827bff..fc646d3 100644 --- a/components/ratgdo/switch/__init__.py +++ b/components/ratgdo/lock/__init__.py @@ -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) diff --git a/components/ratgdo/lock/ratgdo_lock.cpp b/components/ratgdo/lock/ratgdo_lock.cpp new file mode 100644 index 0000000..7758424 --- /dev/null +++ b/components/ratgdo/lock/ratgdo_lock.cpp @@ -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 diff --git a/components/ratgdo/lock/ratgdo_lock.h b/components/ratgdo/lock/ratgdo_lock.h new file mode 100644 index 0000000..3ffb95a --- /dev/null +++ b/components/ratgdo/lock/ratgdo_lock.h @@ -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 diff --git a/components/ratgdo/switch/ratgdo_switch.cpp b/components/ratgdo/switch/ratgdo_switch.cpp deleted file mode 100644 index c6ba4e6..0000000 --- a/components/ratgdo/switch/ratgdo_switch.cpp +++ /dev/null @@ -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 diff --git a/components/ratgdo/switch/ratgdo_switch.h b/components/ratgdo/switch/ratgdo_switch.h deleted file mode 100644 index e29ad99..0000000 --- a/components/ratgdo/switch/ratgdo_switch.h +++ /dev/null @@ -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