Adding cover on_opening and on_closing triggers (#81)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
c09136da50
commit
e2f4b6ec5f
|
@ -1,7 +1,8 @@
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
|
from esphome import automation
|
||||||
from esphome.components import cover
|
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
|
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)
|
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(
|
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)
|
).extend(RATGDO_CLIENT_SCHMEA)
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,4 +39,12 @@ async def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
await cover.register_cover(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)
|
await register_ratgdo_child(var, config)
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue