From 72391389a31c6000a02af63a80fb3806b8a2910c Mon Sep 17 00:00:00 2001 From: Regev Brody Date: Sun, 19 Feb 2023 21:54:03 +0200 Subject: [PATCH] add SUB_BUTTON macro and ability to button schema to define the class (#4450) * add ability to button schema to define the class * add SUB_BUTTON macro --- esphome/components/button/__init__.py | 5 +++++ esphome/components/button/button.h | 7 +++++++ esphome/components/copy/button/__init__.py | 3 +-- .../components/factory_reset/button/__init__.py | 15 ++++++--------- esphome/components/restart/button/__init__.py | 12 +++++------- esphome/components/safe_mode/button/__init__.py | 2 +- esphome/components/shutdown/button/__init__.py | 8 +++----- 7 files changed, 28 insertions(+), 24 deletions(-) diff --git a/esphome/components/button/__init__.py b/esphome/components/button/__init__.py index 7329a7955d..9d250bbc8b 100644 --- a/esphome/components/button/__init__.py +++ b/esphome/components/button/__init__.py @@ -17,6 +17,7 @@ from esphome.const import ( ) from esphome.core import CORE, coroutine_with_priority from esphome.cpp_helpers import setup_entity +from esphome.cpp_generator import MockObjClass CODEOWNERS = ["@esphome/core"] IS_PLATFORM_COMPONENT = True @@ -56,11 +57,15 @@ _UNDEF = object() def button_schema( + class_: MockObjClass = _UNDEF, + *, icon: str = _UNDEF, entity_category: str = _UNDEF, device_class: str = _UNDEF, ) -> cv.Schema: schema = BUTTON_SCHEMA + if class_ is not _UNDEF: + schema = schema.extend({cv.GenerateID(): cv.declare_id(class_)}) if icon is not _UNDEF: schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon}) if entity_category is not _UNDEF: diff --git a/esphome/components/button/button.h b/esphome/components/button/button.h index 398d398cd9..3b5f338887 100644 --- a/esphome/components/button/button.h +++ b/esphome/components/button/button.h @@ -15,6 +15,13 @@ namespace button { } \ } +#define SUB_BUTTON(name) \ + protected: \ + button::Button *name##_button_{nullptr}; \ +\ + public: \ + void set_##name##_button(button::Button *button) { this->name##_button_ = button; } + /** Base class for all buttons. * * A button is just a momentary switch that does not have a state, only a trigger. diff --git a/esphome/components/copy/button/__init__.py b/esphome/components/copy/button/__init__.py index 65d956601a..626a5a8db1 100644 --- a/esphome/components/copy/button/__init__.py +++ b/esphome/components/copy/button/__init__.py @@ -16,10 +16,9 @@ CopyButton = copy_ns.class_("CopyButton", button.Button, cg.Component) CONFIG_SCHEMA = ( - button.button_schema() + button.button_schema(CopyButton) .extend( { - cv.GenerateID(): cv.declare_id(CopyButton), cv.Required(CONF_SOURCE_ID): cv.use_id(button.Button), } ) diff --git a/esphome/components/factory_reset/button/__init__.py b/esphome/components/factory_reset/button/__init__.py index d5beac34b5..010691ac7f 100644 --- a/esphome/components/factory_reset/button/__init__.py +++ b/esphome/components/factory_reset/button/__init__.py @@ -13,15 +13,12 @@ FactoryResetButton = factory_reset_ns.class_( "FactoryResetButton", button.Button, cg.Component ) -CONFIG_SCHEMA = ( - button.button_schema( - device_class=DEVICE_CLASS_RESTART, - entity_category=ENTITY_CATEGORY_CONFIG, - icon=ICON_RESTART_ALERT, - ) - .extend({cv.GenerateID(): cv.declare_id(FactoryResetButton)}) - .extend(cv.COMPONENT_SCHEMA) -) +CONFIG_SCHEMA = button.button_schema( + FactoryResetButton, + device_class=DEVICE_CLASS_RESTART, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_RESTART_ALERT, +).extend(cv.COMPONENT_SCHEMA) async def to_code(config): diff --git a/esphome/components/restart/button/__init__.py b/esphome/components/restart/button/__init__.py index 1a0e9cdc3d..1b2c991261 100644 --- a/esphome/components/restart/button/__init__.py +++ b/esphome/components/restart/button/__init__.py @@ -10,13 +10,11 @@ from esphome.const import ( restart_ns = cg.esphome_ns.namespace("restart") RestartButton = restart_ns.class_("RestartButton", button.Button, cg.Component) -CONFIG_SCHEMA = ( - button.button_schema( - device_class=DEVICE_CLASS_RESTART, entity_category=ENTITY_CATEGORY_CONFIG - ) - .extend({cv.GenerateID(): cv.declare_id(RestartButton)}) - .extend(cv.COMPONENT_SCHEMA) -) +CONFIG_SCHEMA = button.button_schema( + RestartButton, + device_class=DEVICE_CLASS_RESTART, + entity_category=ENTITY_CATEGORY_CONFIG, +).extend(cv.COMPONENT_SCHEMA) async def to_code(config): diff --git a/esphome/components/safe_mode/button/__init__.py b/esphome/components/safe_mode/button/__init__.py index 2cd8892afb..307e4e372e 100644 --- a/esphome/components/safe_mode/button/__init__.py +++ b/esphome/components/safe_mode/button/__init__.py @@ -17,11 +17,11 @@ SafeModeButton = safe_mode_ns.class_("SafeModeButton", button.Button, cg.Compone CONFIG_SCHEMA = ( button.button_schema( + SafeModeButton, device_class=DEVICE_CLASS_RESTART, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_RESTART_ALERT, ) - .extend({cv.GenerateID(): cv.declare_id(SafeModeButton)}) .extend({cv.GenerateID(CONF_OTA): cv.use_id(OTAComponent)}) .extend(cv.COMPONENT_SCHEMA) ) diff --git a/esphome/components/shutdown/button/__init__.py b/esphome/components/shutdown/button/__init__.py index 51cd6d6da2..79d0b23935 100644 --- a/esphome/components/shutdown/button/__init__.py +++ b/esphome/components/shutdown/button/__init__.py @@ -10,11 +10,9 @@ from esphome.const import ( shutdown_ns = cg.esphome_ns.namespace("shutdown") ShutdownButton = shutdown_ns.class_("ShutdownButton", button.Button, cg.Component) -CONFIG_SCHEMA = ( - button.button_schema(entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_POWER) - .extend({cv.GenerateID(): cv.declare_id(ShutdownButton)}) - .extend(cv.COMPONENT_SCHEMA) -) +CONFIG_SCHEMA = button.button_schema( + ShutdownButton, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_POWER +).extend(cv.COMPONENT_SCHEMA) async def to_code(config):