From 91210c5ef543878ff6c685bcc5b1d1ba7d981018 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 25 Sep 2026 08:37:03 +0100 Subject: [PATCH] [button] Use register_apply_action for button.press (#19598) --- esphome/components/button/__init__.py | 18 ++++-------------- esphome/components/button/automation.h | 19 ------------------- tests/components/button/common.yaml | 6 ++++++ 3 files changed, 10 insertions(+), 33 deletions(-) delete mode 100644 esphome/components/button/automation.h diff --git a/esphome/components/button/__init__.py b/esphome/components/button/__init__.py index ee24002b8a..bdb438a99d 100644 --- a/esphome/components/button/__init__.py +++ b/esphome/components/button/__init__.py @@ -16,14 +16,14 @@ from esphome.const import ( DEVICE_CLASS_RESTART, DEVICE_CLASS_UPDATE, ) -from esphome.core import CORE, ID, CoroPriority, coroutine_with_priority +from esphome.core import CORE, CoroPriority, coroutine_with_priority from esphome.core.entity_helpers import ( entity_duplicate_validator, queue_entity_register, setup_device_class, setup_entity, ) -from esphome.cpp_generator import MockObj, MockObjClass, TemplateArgsType +from esphome.cpp_generator import MockObj, MockObjClass from esphome.types import ConfigType, SafeExpType CODEOWNERS = ["@esphome/core"] @@ -40,8 +40,6 @@ button_ns = cg.esphome_ns.namespace("button") Button = button_ns.class_("Button", cg.EntityBase) ButtonPtr = Button.operator("ptr") -PressAction = button_ns.class_("PressAction", automation.Action) - validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True, space="_") @@ -123,17 +121,9 @@ BUTTON_PRESS_SCHEMA = maybe_simple_id( ) -@automation.register_action( - "button.press", PressAction, BUTTON_PRESS_SCHEMA, synchronous=True +automation.register_apply_action( + "button.press", BUTTON_PRESS_SCHEMA, automation.ApplyCall("press()") ) -async def button_press_to_code( - config: ConfigType, - action_id: ID, - template_arg: cg.TemplateArguments, - args: TemplateArgsType, -) -> MockObj: - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) @coroutine_with_priority(CoroPriority.CORE) diff --git a/esphome/components/button/automation.h b/esphome/components/button/automation.h deleted file mode 100644 index 683328b394..0000000000 --- a/esphome/components/button/automation.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "esphome/components/button/button.h" -#include "esphome/core/automation.h" -#include "esphome/core/component.h" - -namespace esphome::button { - -template class PressAction final : public Action { - public: - explicit PressAction(Button *button) : button_(button) {} - - void play(const Ts &...x) override { this->button_->press(); } - - protected: - Button *button_; -}; - -} // namespace esphome::button diff --git a/tests/components/button/common.yaml b/tests/components/button/common.yaml index d5978601f4..352a4c2923 100644 --- a/tests/components/button/common.yaml +++ b/tests/components/button/common.yaml @@ -4,3 +4,9 @@ button: id: some_button on_press: - logger.log: Button pressed + +esphome: + on_boot: + - button.press: some_button + - button.press: + id: some_button