From 793408186d38f3f9e794947d57bc8601ef9896d9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Jun 2023 10:37:51 -0500 Subject: [PATCH] binary sensor --- components/ratgdo/__init__.py | 75 +++++++++++++------ components/ratgdo/binary_sensor/__init__.py | 28 +++++++ .../binary_sensor/ratgdo_binary_sensor.cpp | 28 +++++++ .../binary_sensor/ratgdo_binary_sensor.h | 31 ++++++++ components/ratgdo/ratgdo_child.h | 1 - 5 files changed, 138 insertions(+), 25 deletions(-) create mode 100644 components/ratgdo/binary_sensor/__init__.py create mode 100644 components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp create mode 100644 components/ratgdo/binary_sensor/ratgdo_binary_sensor.h diff --git a/components/ratgdo/__init__.py b/components/ratgdo/__init__.py index 96e7e93..11fa64c 100644 --- a/components/ratgdo/__init__.py +++ b/components/ratgdo/__init__.py @@ -12,42 +12,69 @@ RATGDO = ratgdo_ns.class_("RATGDOComponent", cg.Component, uart.UARTDevice) CONF_OUTPUT_GDO = "output_gdo_pin" -DEFAULT_OUTPUT_GDO = 2 # D4 ed control terminal / GarageDoorOpener (UART1 TX) pin is D4 on D1 Mini +DEFAULT_OUTPUT_GDO = ( + 2 # D4 ed control terminal / GarageDoorOpener (UART1 TX) pin is D4 on D1 Mini +) CONF_INPUT_GDO = "input_gdo_pin" -DEFAULT_INPUT_GDO = 4 # D2 red control terminal / GarageDoorOpener (UART1 RX) pin is D2 on D1 Mini +DEFAULT_INPUT_GDO = ( + 4 # D2 red control terminal / GarageDoorOpener (UART1 RX) pin is D2 on D1 Mini +) CONF_INPUT_OBST = "input_obst_pin" -DEFAULT_INPUT_OBST = 13 # D7 black obstruction sensor terminal +DEFAULT_INPUT_OBST = 13 # D7 black obstruction sensor terminal CONF_TRIGGER_OPEN = "trigger_open_pin" -DEFAULT_TRIGGER_OPEN = 14 # D5 dry contact for opening door +DEFAULT_TRIGGER_OPEN = 14 # D5 dry contact for opening door CONF_TRIGGER_CLOSE = "trigger_close_pin" -DEFAULT_TRIGGER_CLOSE = 12 # D6 dry contact for closing door +DEFAULT_TRIGGER_CLOSE = 12 # D6 dry contact for closing door CONF_TRIGGER_LIGHT = "trigger_light_pin" -DEFAULT_TRIGGER_LIGHT = 0 # D3 dry contact for triggering light (no discrete light commands, so toggle only) +DEFAULT_TRIGGER_LIGHT = 0 # D3 dry contact for triggering light (no discrete light commands, so toggle only) CONF_STATUS_DOOR = "status_door_pin" -DEFAULT_STATUS_DOOR = 16 # D0 output door status, HIGH for open, LOW for closed +DEFAULT_STATUS_DOOR = 16 # D0 output door status, HIGH for open, LOW for closed CONF_STATUS_OBST = "status_obst_pin" -DEFAULT_STATUS_OBST = 15 # D8 output for obstruction status, HIGH for obstructed, LOW for clear +DEFAULT_STATUS_OBST = ( + 15 # D8 output for obstruction status, HIGH for obstructed, LOW for clear +) +CONFIG_SCHEMA = ( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(RATGDO), + cv.Optional( + CONF_OUTPUT_GDO, default=DEFAULT_OUTPUT_GDO + ): pins.internal_gpio_input_pin_schema, + cv.Optional( + CONF_INPUT_GDO, default=DEFAULT_INPUT_GDO + ): pins.internal_gpio_input_pin_schema, + cv.Optional( + CONF_INPUT_OBST, default=DEFAULT_INPUT_OBST + ): pins.internal_gpio_input_pin_schema, + cv.Optional( + CONF_TRIGGER_OPEN, default=DEFAULT_TRIGGER_OPEN + ): pins.internal_gpio_input_pin_schema, + cv.Optional( + CONF_TRIGGER_CLOSE, default=DEFAULT_TRIGGER_CLOSE + ): pins.internal_gpio_input_pin_schema, + cv.Optional( + CONF_TRIGGER_LIGHT, default=DEFAULT_TRIGGER_LIGHT + ): pins.internal_gpio_input_pin_schema, + cv.Optional( + CONF_STATUS_DOOR, default=DEFAULT_STATUS_DOOR + ): pins.internal_gpio_input_pin_schema, + cv.Optional( + CONF_STATUS_OBST, default=DEFAULT_STATUS_OBST + ): pins.internal_gpio_input_pin_schema, + } + ) + .extend(cv.COMPONENT_SCHEMA) + .extend(uart.UART_DEVICE_SCHEMA) +) -CONFIG_SCHEMA = cv.Schema( - { - cv.GenerateID(): cv.declare_id(RATGDO), - cv.Optional(CONF_OUTPUT_GDO, default=DEFAULT_OUTPUT_GDO): pins.internal_gpio_input_pin_schema, - cv.Optional(CONF_INPUT_GDO, default=DEFAULT_INPUT_GDO): pins.internal_gpio_input_pin_schema, - cv.Optional(CONF_INPUT_OBST, default=DEFAULT_INPUT_OBST): pins.internal_gpio_input_pin_schema, - - cv.Optional(CONF_TRIGGER_OPEN, default=DEFAULT_TRIGGER_OPEN): pins.internal_gpio_input_pin_schema, - cv.Optional(CONF_TRIGGER_CLOSE, default=DEFAULT_TRIGGER_CLOSE): pins.internal_gpio_input_pin_schema, - cv.Optional(CONF_TRIGGER_LIGHT, default=DEFAULT_TRIGGER_LIGHT): pins.internal_gpio_input_pin_schema, - - cv.Optional(CONF_STATUS_DOOR, default=DEFAULT_STATUS_DOOR): pins.internal_gpio_input_pin_schema, - cv.Optional(CONF_STATUS_OBST, default=DEFAULT_STATUS_OBST): pins.internal_gpio_input_pin_schema, - } -).extend(cv.COMPONENT_SCHEMA).extend(uart.UART_DEVICE_SCHEMA) +async def register_ratgdo_child(var, config): + parent = cv.use_id(RATGDO) + cg.add(parent.register_child(var)) async def to_code(config): @@ -56,7 +83,7 @@ async def to_code(config): pin = await cg.gpio_pin_expression(config[CONF_OUTPUT_GDO]) cg.add(var.set_output_gdo_pin(pin)) pin = await cg.gpio_pin_expression(config[CONF_INPUT_GDO]) - cg.add(var.set_input_gdo_pin(pin)) + cg.add(var.set_input_gdo_pin(pin)) pin = await cg.gpio_pin_expression(config[CONF_INPUT_OBST]) cg.add(var.set_input_obst_pin(pin)) diff --git a/components/ratgdo/binary_sensor/__init__.py b/components/ratgdo/binary_sensor/__init__.py new file mode 100644 index 0000000..c23ff34 --- /dev/null +++ b/components/ratgdo/binary_sensor/__init__.py @@ -0,0 +1,28 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import binary_sensor +from .. import ( + ratgdo_ns, + register_ratgdo_child, +) + +DEPENDENCIES = ["ratgdo"] + +RATGDOBinarySensor = ratgdo_ns.class_( + "RATGDOBinarySensor", binary_sensor.BinarySensor, cg.Component +) + +TYPES = {"motion", "obstruction"} + + +CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend( + {cv.Optional(type): binary_sensor.binary_sensor_schema() for type in TYPES} +) + + +async def to_code(config): + for type in TYPES: + if type in config: + conf = config[type] + var = await binary_sensor.new_binary_sensor(conf) + await register_ratgdo_child(var, config) diff --git a/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp new file mode 100644 index 0000000..fea355f --- /dev/null +++ b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp @@ -0,0 +1,28 @@ +#include "ratgdo_binary_sensor.h" +#include "esphome/core/log.h" +#include "../ratgdo_state.h" + +#ifdef USE_ESP32 + +namespace esphome { +namespace ratgdo { + +using namespace esphome::binary_sensor; + +void RATGDOBinarySensor::dump_config() { + LOG_BINARY_SENSOR("", "RATGDO BinarySensor", this); +} + +void RATGDOBinarySensor::setup() {} +void RATGDOBinarySensor::on_door_state(esphome::ratgdo::DoorState state) {} +void RATGDOBinarySensor::on_light_state(esphome::ratgdo::LightState state) {} +void RATGDOBinarySensor::on_lock_state(esphome::ratgdo::LockState state) {} +void RATGDOBinarySensor::on_motion_state(esphome::ratgdo::MotionState state) {} +void RATGDOBinarySensor::on_obstruction_state(esphome::ratgdo::ObstructionState state) {} + +void RATGDOBinarySensor::loop() {} + +} // namespace ratgdo +} // namespace esphome + +#endif diff --git a/components/ratgdo/binary_sensor/ratgdo_binary_sensor.h b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.h new file mode 100644 index 0000000..c29fb14 --- /dev/null +++ b/components/ratgdo/binary_sensor/ratgdo_binary_sensor.h @@ -0,0 +1,31 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/defines.h" +#include "esphome/core/hal.h" +#include "../ratgdo_client.h" +#include "../ratgdo_state.h" +#include "../ratgdo.h" +#include "esphome/components/binary_sensor/binary_sensor.h" + +namespace esphome { +namespace ratgdo { + +class RATGDOBinarySensor : public binary_sensor::BinarySensor, public RATGDOClient, public Component { + public: + void setup() override; + void loop() override; + void update() override; + void dump_config() override; + + void on_door_state(esphome::ratgdo::DoorState state) override; + void on_light_state(esphome::ratgdo::LightState state) override; + void on_lock_state(esphome::ratgdo::LockState state) override; + void on_motion_state(esphome::ratgdo::MotionState state) override; + void on_obstruction_state(esphome::ratgdo::ObstructionState state) override; + +}; + +} // namespace ratgdo +} // namespace esphome + diff --git a/components/ratgdo/ratgdo_child.h b/components/ratgdo/ratgdo_child.h index b58121c..6cea72a 100644 --- a/components/ratgdo/ratgdo_child.h +++ b/components/ratgdo/ratgdo_child.h @@ -21,7 +21,6 @@ namespace ratgdo { protected: friend RATGDOComponent; - virtual std::string describe() = 0; }; } // namespace ratgdo