From be16a745a01e95791829484f76896c8f66672284 Mon Sep 17 00:00:00 2001 From: brgaulin <4007825+brgaulin@users.noreply.github.com> Date: Mon, 22 Jan 2024 21:44:42 -0500 Subject: [PATCH] feat: add cover.on_state_change trigger fixes #192 --- components/ratgdo/cover/__init__.py | 10 ++++++++++ components/ratgdo/cover/automation.h | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/components/ratgdo/cover/__init__.py b/components/ratgdo/cover/__init__.py index eb0f82f..ea8ba1e 100644 --- a/components/ratgdo/cover/__init__.py +++ b/components/ratgdo/cover/__init__.py @@ -18,9 +18,13 @@ CoverOpeningTrigger = ratgdo_ns.class_( CoverClosingTrigger = ratgdo_ns.class_( "CoverClosingTrigger", automation.Trigger.template() ) +CoverStateTrigger = ratgdo_ns.class_( + "CoverStateTrigger", automation.Trigger.template() +) CONF_ON_OPENING = "on_opening" CONF_ON_CLOSING = "on_closing" +CONF_ON_STATE_CHANGE = "on_state_change" CONFIG_SCHEMA = cover.COVER_SCHEMA.extend( { @@ -31,6 +35,9 @@ CONFIG_SCHEMA = cover.COVER_SCHEMA.extend( cv.Optional(CONF_ON_CLOSING): automation.validate_automation( {cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CoverClosingTrigger)} ), + cv.Optional(CONF_ON_STATE_CHANGE): automation.validate_automation( + {cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CoverStateTrigger)} + ), } ).extend(RATGDO_CLIENT_SCHMEA) @@ -46,5 +53,8 @@ async def to_code(config): for conf in config.get(CONF_ON_CLOSING, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [], conf) + for conf in config.get(CONF_ON_STATE_CHANGE, []): + 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 index 93f103f..e8a1b38 100644 --- a/components/ratgdo/cover/automation.h +++ b/components/ratgdo/cover/automation.h @@ -31,5 +31,15 @@ namespace ratgdo { } }; + class CoverStateTrigger : public Trigger<> { + public: + CoverStateTrigger(cover::Cover* a_cover) + { + a_cover->add_on_state_callback([this, a_cover]() { + this->trigger(); + }); + } + }; + } // namespace ratgdo } // namespace esphome