From 93d404993bb0a860a99a8dacd59dec3861e01e49 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 9 Jun 2023 15:54:20 -0500 Subject: [PATCH] fix --- base.yml | 15 +++++++--- components/ratgdo/ratgdo.cpp | 2 +- components/ratgdo/switch/__init__.py | 34 ++++++++++++++++++++++ components/ratgdo/switch/ratgdo_switch.cpp | 34 ++++++++++++++++++++++ components/ratgdo/switch/ratgdo_switch.h | 30 +++++++++++++++++++ 5 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 components/ratgdo/switch/__init__.py create mode 100644 components/ratgdo/switch/ratgdo_switch.cpp create mode 100644 components/ratgdo/switch/ratgdo_switch.h diff --git a/base.yml b/base.yml index e5d852c..4edcadf 100644 --- a/base.yml +++ b/base.yml @@ -24,13 +24,20 @@ ratgdo: switch: + - platform: ratgdo + id: ${id_prefix}_lock_remotes + type: lock + entity_category: config + ratgdo_id: ${id_prefix} + name: "${friendly_name} Lock remotes" + - platform: gpio id: "${id_prefix}_status_door" pin: number: D0 # D0 output door status, HIGH for open, LOW for closed mode: output: true - name: "${friendly_name} Status Door" + name: "${friendly_name} Status door" entity_category: diagnostic - platform: gpio id: "${id_prefix}_status_obstruction" @@ -38,7 +45,7 @@ switch: number: D8 # D8 output for obstruction status, HIGH for obstructed, LOW for clear mode: output: true - name: "${friendly_name} Status Obstruction" + name: "${friendly_name} Status obstruction" entity_category: diagnostic binary_sensor: @@ -116,7 +123,7 @@ number: type: rolling_code_counter entity_category: config ratgdo_id: ${id_prefix} - name: "${friendly_name} Rolling Code Counter" + name: "${friendly_name} Rolling code counter" mode: box unit_of_measurement: "codes" @@ -152,7 +159,7 @@ button: - platform: restart name: "${friendly_name} Restart" - platform: safe_mode - name: "${friendly_name} Safe Mode Boot" + name: "${friendly_name} Safe mode boot" entity_category: diagnostic - platform: ratgdo id: ${id_prefix}_sync diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index 58b8544..60f196f 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -436,7 +436,7 @@ namespace ratgdo { if (this->lightState == LightState::LIGHT_STATE_ON) { ESP_LOGD(TAG, "The light is already on"); return; - } + } toggleLight(); // We don't always get the state back so be optimistic this->previousLightState = this->lightState; diff --git a/components/ratgdo/switch/__init__.py b/components/ratgdo/switch/__init__.py new file mode 100644 index 0000000..e89b8f9 --- /dev/null +++ b/components/ratgdo/switch/__init__.py @@ -0,0 +1,34 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.const import CONF_ID +from esphome.components import switch +from .. import ratgdo_ns, register_ratgdo_child, RATGDO_CLIENT_SCHMEA + +DEPENDENCIES = ["ratgdo"] + +RATGDOSwitch = ratgdo_ns.class_("RATGDOSwitch", switch.Switch, cg.Component) +SwitchType = ratgdo_ns.enum("SwitchType") + +CONF_TYPE = "type" +TYPES = { + "lock": SwitchType.RATGDO_LOCK, +} + + +CONFIG_SCHEMA = ( + switch.switch_schema(RATGDOSwitch) + .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 switch.register_switch(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/switch/ratgdo_switch.cpp b/components/ratgdo/switch/ratgdo_switch.cpp new file mode 100644 index 0000000..35a3ade --- /dev/null +++ b/components/ratgdo/switch/ratgdo_switch.cpp @@ -0,0 +1,34 @@ +#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_NUMBER("", "RATGDO Switch", this); + ESP_LOGCONFIG(TAG, " Type: Lock"); + } + + void RATGDOSwitch::on_lock_state(LockState state) + { + this->state(state == LockState::LOCK_STATE_LOCKED) this->publish_state(); + } + void RATGDOSwitch::turn_on() + { + ESP_LOGD(TAG, "name: %s this->type_:%d ON", this->get_name(), this->switch_type_); + this->parent_->lock(value); + this->publish_state(value); + } + void RATGDOSwitch::turn_off() + { + ESP_LOGD(TAG, "name: %s this->type_:%d OFF", this->get_name(), this->switch_type_); + this->parent_->unlock(value); + this->publish_state(value); + } + +} // namespace ratgdo +} // namespace esphome diff --git a/components/ratgdo/switch/ratgdo_switch.h b/components/ratgdo/switch/ratgdo_switch.h new file mode 100644 index 0000000..2d427b5 --- /dev/null +++ b/components/ratgdo/switch/ratgdo_switch.h @@ -0,0 +1,30 @@ +#pragma once + +#include "../ratgdo.h" +#include "../ratgdo_child.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 set_switch_type(SwitchType switch_type_) { this->switch_type_ = switch_type_; } + + void on_lock_state(LockState state) override; + void turn_off() override; + void turn_on() override; + + protected: + SwitchType switch_type_; + }; + +} // namespace ratgdo +} // namespace esphome