From a17285afc799fec4932cfe26ae96afb4890a7aa0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 25 Sep 2026 15:23:03 +0100 Subject: [PATCH] [fan] Use register_apply_action for fan.toggle and fan.turn_off (#19600) --- esphome/components/fan/__init__.py | 19 ++----------------- esphome/components/fan/automation.h | 18 ------------------ tests/components/fan/common.yaml | 3 +++ 3 files changed, 5 insertions(+), 35 deletions(-) diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index d00375ae96..526499fe07 100644 --- a/esphome/components/fan/__init__.py +++ b/esphome/components/fan/__init__.py @@ -61,8 +61,6 @@ RESTORE_MODES = { } # Actions -TurnOffAction = fan_ns.class_("TurnOffAction", automation.Action) -ToggleAction = fan_ns.class_("ToggleAction", automation.Action) CycleSpeedAction = fan_ns.class_("CycleSpeedAction", automation.Action) FanStateTrigger = fan_ns.class_( @@ -310,21 +308,8 @@ FAN_ACTION_SCHEMA = maybe_simple_id( ) -@automation.register_action( - "fan.toggle", ToggleAction, FAN_ACTION_SCHEMA, synchronous=True -) -async def fan_toggle_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) - - -@automation.register_action( - "fan.turn_off", TurnOffAction, FAN_ACTION_SCHEMA, synchronous=True -) -async def fan_turn_off_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) - +automation.register_apply_action("fan.toggle", FAN_ACTION_SCHEMA, call="toggle") +automation.register_apply_action("fan.turn_off", FAN_ACTION_SCHEMA, call="turn_off") automation.register_apply_action( "fan.turn_on", diff --git a/esphome/components/fan/automation.h b/esphome/components/fan/automation.h index 5f0843581b..4c587904b7 100644 --- a/esphome/components/fan/automation.h +++ b/esphome/components/fan/automation.h @@ -6,24 +6,6 @@ namespace esphome::fan { -template class TurnOffAction final : public Action { - public: - explicit TurnOffAction(Fan *state) : state_(state) {} - - void play(const Ts &...x) override { this->state_->turn_off().perform(); } - - Fan *state_; -}; - -template class ToggleAction final : public Action { - public: - explicit ToggleAction(Fan *state) : state_(state) {} - - void play(const Ts &...x) override { this->state_->toggle().perform(); } - - Fan *state_; -}; - template class CycleSpeedAction final : public Action { public: explicit CycleSpeedAction(Fan *state) : state_(state) {} diff --git a/tests/components/fan/common.yaml b/tests/components/fan/common.yaml index 4867804394..12c7b6c872 100644 --- a/tests/components/fan/common.yaml +++ b/tests/components/fan/common.yaml @@ -104,6 +104,9 @@ button: fan.is_on: test_fan then: - fan.turn_off: test_fan + - fan.toggle: test_fan + - fan.turn_off: + id: test_fan - if: condition: fan.is_off: