1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-20 16:55:49 +00:00

Added fan triggers on_turn_on and on_turn_off (#1726)

This commit is contained in:
Anthony Uk
2021-05-19 06:12:43 +02:00
committed by GitHub
parent 76f78877f6
commit 96e46db272
2 changed files with 59 additions and 0 deletions

View File

@@ -14,6 +14,9 @@ from esphome.const import (
CONF_SPEED_COMMAND_TOPIC,
CONF_SPEED_STATE_TOPIC,
CONF_NAME,
CONF_ON_TURN_OFF,
CONF_ON_TURN_ON,
CONF_TRIGGER_ID,
)
from esphome.core import CORE, coroutine, coroutine_with_priority
@@ -28,6 +31,9 @@ TurnOnAction = fan_ns.class_("TurnOnAction", automation.Action)
TurnOffAction = fan_ns.class_("TurnOffAction", automation.Action)
ToggleAction = fan_ns.class_("ToggleAction", automation.Action)
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", automation.Trigger.template())
FanTurnOffTrigger = fan_ns.class_("FanTurnOffTrigger", automation.Trigger.template())
FAN_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(FanState),
@@ -44,6 +50,16 @@ FAN_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend(
cv.Optional(CONF_SPEED_COMMAND_TOPIC): cv.All(
cv.requires_component("mqtt"), cv.subscribe_topic
),
cv.Optional(CONF_ON_TURN_ON): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanTurnOnTrigger),
}
),
cv.Optional(CONF_ON_TURN_OFF): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanTurnOffTrigger),
}
),
}
)
@@ -77,6 +93,13 @@ def setup_fan_core_(var, config):
mqtt_.set_custom_speed_command_topic(config[CONF_SPEED_COMMAND_TOPIC])
)
for conf in config.get(CONF_ON_TURN_ON, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_TURN_OFF, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf)
@coroutine
def register_fan(var, config):