diff --git a/components/ratgdo/cover/__init__.py b/components/ratgdo/cover/__init__.py index a0a746d..eb0f82f 100644 --- a/components/ratgdo/cover/__init__.py +++ b/components/ratgdo/cover/__init__.py @@ -1,7 +1,8 @@ import esphome.codegen as cg import esphome.config_validation as cv +from esphome import automation from esphome.components import cover -from esphome.const import CONF_ID +from esphome.const import CONF_ID, CONF_TRIGGER_ID from .. import RATGDO_CLIENT_SCHMEA, ratgdo_ns, register_ratgdo_child @@ -10,8 +11,27 @@ DEPENDENCIES = ["ratgdo"] RATGDOCover = ratgdo_ns.class_("RATGDOCover", cover.Cover, cg.Component) +# Triggers +CoverOpeningTrigger = ratgdo_ns.class_( + "CoverOpeningTrigger", automation.Trigger.template() +) +CoverClosingTrigger = ratgdo_ns.class_( + "CoverClosingTrigger", automation.Trigger.template() +) + +CONF_ON_OPENING = "on_opening" +CONF_ON_CLOSING = "on_closing" + CONFIG_SCHEMA = cover.COVER_SCHEMA.extend( - {cv.GenerateID(): cv.declare_id(RATGDOCover)} + { + cv.GenerateID(): cv.declare_id(RATGDOCover), + cv.Optional(CONF_ON_OPENING): automation.validate_automation( + {cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CoverOpeningTrigger)} + ), + cv.Optional(CONF_ON_CLOSING): automation.validate_automation( + {cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CoverClosingTrigger)} + ), + } ).extend(RATGDO_CLIENT_SCHMEA) @@ -19,4 +39,12 @@ async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) await cover.register_cover(var, config) + + for conf in config.get(CONF_ON_OPENING, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + for conf in config.get(CONF_ON_CLOSING, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + await register_ratgdo_child(var, config) diff --git a/components/ratgdo/cover/automation.h b/components/ratgdo/cover/automation.h new file mode 100644 index 0000000..93f103f --- /dev/null +++ b/components/ratgdo/cover/automation.h @@ -0,0 +1,35 @@ +#pragma once + +#include "esphome/components/cover/cover.h" +#include "esphome/core/automation.h" +#include "esphome/core/component.h" + +namespace esphome { +namespace ratgdo { + + class CoverOpeningTrigger : public Trigger<> { + public: + CoverOpeningTrigger(cover::Cover* a_cover) + { + a_cover->add_on_state_callback([this, a_cover]() { + if (a_cover->current_operation == cover::COVER_OPERATION_OPENING) { + this->trigger(); + } + }); + } + }; + + class CoverClosingTrigger : public Trigger<> { + public: + CoverClosingTrigger(cover::Cover* a_cover) + { + a_cover->add_on_state_callback([this, a_cover]() { + if (a_cover->current_operation == cover::COVER_OPERATION_CLOSING) { + this->trigger(); + } + }); + } + }; + +} // namespace ratgdo +} // namespace esphome