diff --git a/components/ratgdo/__init__.py b/components/ratgdo/__init__.py index 6b52ddc..44a5f9a 100644 --- a/components/ratgdo/__init__.py +++ b/components/ratgdo/__init__.py @@ -3,6 +3,7 @@ import esphome.config_validation as cv import voluptuous as vol from esphome import automation, pins from esphome.const import CONF_ID, CONF_TRIGGER_ID +from esphome.components import binary_sensor DEPENDENCIES = ["preferences"] MULTI_CONF = True @@ -36,7 +37,22 @@ PROTOCOL_SECPLUSV2 = "secplusv2" PROTOCOL_DRYCONTACT = "drycontact" SUPPORTED_PROTOCOLS = [PROTOCOL_SECPLUSV1, PROTOCOL_SECPLUSV2, PROTOCOL_DRYCONTACT] -CONFIG_SCHEMA = cv.Schema( +CONF_DRY_CONTACT_OPEN_SENSOR = "dry_contact_open_sensor" +CONF_DRY_CONTACT_CLOSE_SENSOR = "dry_contact_close_sensor" +CONF_DRY_CONTACT_SENSOR_GROUP = "dry_contact_sensor_group" + +def validate_protocol(config): + print("Validation") + if config.get(CONF_PROTOCOL, None) == PROTOCOL_DRYCONTACT and (CONF_DRY_CONTACT_CLOSE_SENSOR not in config or CONF_DRY_CONTACT_OPEN_SENSOR not in config): + raise cv.Invalid("dry_contact_close_sensor and dry_contact_open_sensor are required when using protocol drycontact") + if config.get(CONF_PROTOCOL, None) != PROTOCOL_DRYCONTACT and (CONF_DRY_CONTACT_CLOSE_SENSOR in config or CONF_DRY_CONTACT_OPEN_SENSOR in config): + raise cv.Invalid("dry_contact_close_sensor and dry_contact_open_sensor are only valid when using protocol drycontact") +# if config.get(CONF_PROTOCOL, None) == PROTOCOL_DRYCONTACT and CONF_DRY_CONTACT_OPEN_SENSOR not in config: +# raise cv.Invalid("dry_contact_open_sensor is required when using protocol drycontact") + return config + +CONFIG_SCHEMA = cv.All( + cv.Schema( { cv.GenerateID(): cv.declare_id(RATGDO), cv.Optional( @@ -53,11 +69,17 @@ CONFIG_SCHEMA = cv.Schema( cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SyncFailed), } ), - cv.Optional(CONF_PROTOCOL, default=PROTOCOL_SECPLUSV2): vol.In( + cv.Optional(CONF_PROTOCOL, default=PROTOCOL_SECPLUSV2): cv.All(vol.In( SUPPORTED_PROTOCOLS - ), + )), + # cv.Inclusive(CONF_DRY_CONTACT_OPEN_SENSOR,CONF_DRY_CONTACT_SENSOR_GROUP): cv.use_id(binary_sensor.BinarySensor), + # cv.Inclusive(CONF_DRY_CONTACT_CLOSE_SENSOR,CONF_DRY_CONTACT_SENSOR_GROUP): cv.use_id(binary_sensor.BinarySensor), + cv.Optional(CONF_DRY_CONTACT_OPEN_SENSOR): cv.use_id(binary_sensor.BinarySensor), + cv.Optional(CONF_DRY_CONTACT_CLOSE_SENSOR): cv.use_id(binary_sensor.BinarySensor), } -).extend(cv.COMPONENT_SCHEMA) + ).extend(cv.COMPONENT_SCHEMA), + validate_protocol, +) RATGDO_CLIENT_SCHMEA = cv.Schema( { @@ -82,6 +104,14 @@ async def to_code(config): pin = await cg.gpio_pin_expression(config[CONF_INPUT_OBST]) cg.add(var.set_input_obst_pin(pin)) + if CONF_DRY_CONTACT_OPEN_SENSOR in config and config[CONF_DRY_CONTACT_OPEN_SENSOR]: + dry_contact_open_sensor = await cg.get_variable(config[CONF_DRY_CONTACT_OPEN_SENSOR]) + cg.add(var.set_dry_contact_open_sensor(dry_contact_open_sensor)) + + if CONF_DRY_CONTACT_CLOSE_SENSOR in config and config[CONF_DRY_CONTACT_CLOSE_SENSOR]: + dry_contact_close_sensor = await cg.get_variable(config[CONF_DRY_CONTACT_CLOSE_SENSOR]) + cg.add(var.set_dry_contact_close_sensor(dry_contact_close_sensor)) + for conf in config.get(CONF_ON_SYNC_FAILED, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [], conf) diff --git a/components/ratgdo/dry_contact.cpp b/components/ratgdo/dry_contact.cpp index 76dbc91..159506f 100644 --- a/components/ratgdo/dry_contact.cpp +++ b/components/ratgdo/dry_contact.cpp @@ -5,6 +5,7 @@ #include "esphome/core/gpio.h" #include "esphome/core/log.h" #include "esphome/core/scheduler.h" +#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" namespace esphome { namespace ratgdo { diff --git a/components/ratgdo/dry_contact.h b/components/ratgdo/dry_contact.h index ee5a69e..162c367 100644 --- a/components/ratgdo/dry_contact.h +++ b/components/ratgdo/dry_contact.h @@ -2,6 +2,8 @@ #include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial #include "esphome/core/optional.h" +#include "esphome/core/gpio.h" +#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" #include "callbacks.h" #include "observable.h" @@ -17,6 +19,7 @@ namespace ratgdo { namespace dry_contact { using namespace esphome::ratgdo::protocol; + using namespace esphome::gpio; class DryContact : public Protocol { public: @@ -51,6 +54,7 @@ namespace ratgdo { bool last_open_limit_; bool close_limit_reached_; bool last_close_limit_; + }; } // namespace secplus1 diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index 32479e0..0dd97c5 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -683,5 +683,30 @@ namespace ratgdo { this->learn_state.subscribe([=](LearnState state) { defer("learn_state", [=] { f(state); }); }); } + void RATGDOComponent::set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor) + { + dry_contact_open_sensor_ = dry_contact_open_sensor; + dry_contact_open_sensor_->add_on_state_callback([this](bool sensor_value) + { + if (sensor_value) { + ESP_LOGD(TAG,"Dry Contact Open Sensor Triggered"); + this->set_open_limit(true); + } + } + ); + } + void RATGDOComponent::set_dry_contact_close_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor) + { + dry_contact_close_sensor_ = dry_contact_close_sensor; + dry_contact_close_sensor_->add_on_state_callback([this](bool sensor_value) + { + if (sensor_value) { + ESP_LOGD(TAG,"Dry Contact Close Sensor Triggered"); + set_close_limit(true); + } + } + ); + } + } // namespace ratgdo } // namespace esphome diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index 4e328a2..e221d4f 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -16,6 +16,7 @@ #include "esphome/core/component.h" #include "esphome/core/hal.h" #include "esphome/core/preferences.h" +#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" #include "callbacks.h" #include "macros.h" @@ -168,6 +169,9 @@ namespace ratgdo { void subscribe_sync_failed(std::function&& f); void subscribe_learn_state(std::function&& f); + void set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor_); + void set_dry_contact_close_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor_); + protected: RATGDOStore isr_store_ {}; protocol::Protocol* protocol_; @@ -176,6 +180,8 @@ namespace ratgdo { InternalGPIOPin* output_gdo_pin_; InternalGPIOPin* input_gdo_pin_; InternalGPIOPin* input_obst_pin_; + esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor_; + esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor_; }; // RATGDOComponent } // namespace ratgdo