[fan] Build the on_state trigger with build_parent_callback_automation (#19612)

This commit is contained in:
J. Nick Koston
2026-10-11 15:54:25 -10:00
committed by GitHub
parent 028db614ca
commit 95de93bebb
4 changed files with 54 additions and 38 deletions
+16 -28
View File
@@ -64,9 +64,6 @@ RESTORE_MODES = {
# Actions
CycleSpeedAction = fan_ns.class_("CycleSpeedAction", automation.Action)
FanStateTrigger = fan_ns.class_(
"FanStateTrigger", automation.Trigger.template(Fan.operator("ptr"))
)
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", automation.Trigger.template())
FanTurnOffTrigger = fan_ns.class_("FanTurnOffTrigger", automation.Trigger.template())
FanDirectionSetTrigger = fan_ns.class_(
@@ -115,11 +112,7 @@ _FAN_SCHEMA = (
cv.Optional(CONF_SPEED_COMMAND_TOPIC): cv.All(
cv.requires_component("mqtt"), cv.subscribe_topic
),
cv.Optional(CONF_ON_STATE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanStateTrigger),
}
),
cv.Optional(CONF_ON_STATE): automation.validate_automation(),
cv.Optional(CONF_ON_TURN_ON): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanTurnOnTrigger),
@@ -221,6 +214,17 @@ def validate_preset_modes(value):
return value
# The edge and set triggers keep their classes; each tracks the previous value.
_TRIGGER_AUTOMATIONS = (
(CONF_ON_TURN_ON, []),
(CONF_ON_TURN_OFF, []),
(CONF_ON_DIRECTION_SET, [(FanDirection, "x")]),
(CONF_ON_OSCILLATING_SET, [(cg.bool_, "x")]),
(CONF_ON_SPEED_SET, [(cg.int_, "x")]),
(CONF_ON_PRESET_SET, [(cg.StringRef, "x")]),
)
@setup_entity("fan")
async def setup_fan_core_(var, config):
cg.add(var.set_restore_mode(config[CONF_RESTORE_MODE]))
@@ -266,26 +270,10 @@ async def setup_fan_core_(var, config):
await web_server.add_entity_config(var, web_server_config)
for conf in config.get(CONF_ON_STATE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(Fan.operator("ptr"), "x")], conf)
for conf in config.get(CONF_ON_TURN_ON, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
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_DIRECTION_SET, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(FanDirection, "x")], conf)
for conf in config.get(CONF_ON_OSCILLATING_SET, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(cg.bool_, "x")], conf)
for conf in config.get(CONF_ON_SPEED_SET, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(cg.int_, "x")], conf)
for conf in config.get(CONF_ON_PRESET_SET, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(cg.StringRef, "x")], conf)
await automation.build_parent_callback_automation(
var, "add_on_state_callback", (Fan.operator("ptr"), "x"), conf
)
await automation.build_trigger_automations(var, config, _TRIGGER_AUTOMATIONS)
async def register_fan(var, config):
-10
View File
@@ -50,16 +50,6 @@ template<typename... Ts> class CycleSpeedAction final : public Action<Ts...> {
Fan *state_;
};
class FanStateTrigger final : public Trigger<Fan *> {
public:
FanStateTrigger(Fan *state) : fan_(state) {
state->add_on_state_callback([this]() { this->trigger(this->fan_); });
}
protected:
Fan *fan_;
};
class FanTurnOnTrigger final : public Trigger<> {
public:
FanTurnOnTrigger(Fan *state) : fan_(state) {
+35
View File
@@ -0,0 +1,35 @@
# on_state with the fan as trigger argument, plus the edge and set triggers.
fan:
- platform: template
name: Fan Triggers
id: fan_triggers
preset_modes:
- Eco
- Turbo
has_oscillating: true
has_direction: true
speed_count: 3
on_state:
- logger.log:
format: "Fan is %s"
args: ['x->state ? "on" : "off"']
on_turn_on:
- logger.log: Fan turned on
on_turn_off:
- logger.log: Fan turned off
on_direction_set:
- logger.log:
format: "Direction %d"
args: ["(int) x"]
on_oscillating_set:
- logger.log:
format: "Oscillating %d"
args: ["x"]
on_speed_set:
- logger.log:
format: "Speed %d"
args: ["x"]
on_preset_set:
- logger.log:
format: "Preset %s"
args: ["x.c_str()"]
@@ -0,0 +1,3 @@
packages:
fan: !include common.yaml
triggers: !include common-triggers.yaml