Adding cover on_opening and on_closing triggers (#81)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Marius Muja 2023-11-05 11:49:59 -08:00 committed by GitHub
parent c09136da50
commit e2f4b6ec5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 2 deletions

View File

@ -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)

View File

@ -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