Add new trigger to fan component `on_speed_set` (#2246)

This commit is contained in:
dgtal1 2021-09-08 05:15:57 +02:00 committed by GitHub
parent dba502c756
commit b0533db2eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 0 deletions

View File

@ -15,6 +15,7 @@ from esphome.const import (
CONF_SPEED_COMMAND_TOPIC,
CONF_SPEED_STATE_TOPIC,
CONF_NAME,
CONF_ON_SPEED_SET,
CONF_ON_TURN_OFF,
CONF_ON_TURN_ON,
CONF_TRIGGER_ID,
@ -41,6 +42,7 @@ ToggleAction = fan_ns.class_("ToggleAction", automation.Action)
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", automation.Trigger.template())
FanTurnOffTrigger = fan_ns.class_("FanTurnOffTrigger", automation.Trigger.template())
FanSpeedSetTrigger = fan_ns.class_("FanSpeedSetTrigger", automation.Trigger.template())
FanIsOnCondition = fan_ns.class_("FanIsOnCondition", automation.Condition.template())
FanIsOffCondition = fan_ns.class_("FanIsOffCondition", automation.Condition.template())
@ -71,6 +73,11 @@ FAN_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanTurnOffTrigger),
}
),
cv.Optional(CONF_ON_SPEED_SET): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanSpeedSetTrigger),
}
),
}
)
@ -110,6 +117,9 @@ async def setup_fan_core_(var, config):
for conf in config.get(CONF_ON_TURN_OFF, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_SPEED_SET, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
async def register_fan(var, config):

View File

@ -103,5 +103,23 @@ class FanTurnOffTrigger : public Trigger<> {
bool last_on_;
};
class FanSpeedSetTrigger : public Trigger<> {
public:
FanSpeedSetTrigger(FanState *state) {
state->add_on_state_callback([this, state]() {
auto speed = state->speed;
auto should_trigger = speed != !this->last_speed_;
this->last_speed_ = speed;
if (should_trigger) {
this->trigger();
}
});
this->last_speed_ = state->speed;
}
protected:
int last_speed_;
};
} // namespace fan
} // namespace esphome

View File

@ -425,6 +425,7 @@ CONF_ON_PRESS = "on_press"
CONF_ON_RAW_VALUE = "on_raw_value"
CONF_ON_RELEASE = "on_release"
CONF_ON_SHUTDOWN = "on_shutdown"
CONF_ON_SPEED_SET = "on_speed_set"
CONF_ON_STATE = "on_state"
CONF_ON_TAG = "on_tag"
CONF_ON_TAG_REMOVED = "on_tag_removed"

View File

@ -1881,6 +1881,9 @@ fan:
oscillation_command_topic: oscillation/command/topic
speed_state_topic: speed/state/topic
speed_command_topic: speed/command/topic
on_speed_set:
then:
- logger.log: "Fan speed was changed!"
interval:
- interval: 10s