From 4d01e0a773dc18c717bb6f6f65e8091c88a8bd5d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 29 Jul 2024 21:43:08 -0500 Subject: [PATCH] cleanup --- .../components/lutron_caseta/__init__.py | 31 ++++++++++++------- .../components/lutron_caseta/button.py | 6 ++-- .../components/lutron_caseta/event.py | 4 ++- .../components/lutron_caseta/models.py | 4 ++- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/lutron_caseta/__init__.py b/homeassistant/components/lutron_caseta/__init__.py index 9ab62dce7c6..0ffbe16ee19 100644 --- a/homeassistant/components/lutron_caseta/__init__.py +++ b/homeassistant/components/lutron_caseta/__init__.py @@ -222,34 +222,41 @@ def _async_build_button_devices( parent_device_info = parent_keypad["device_info"] parent_name = parent_device_info["name"] - has_device_name = True - if not (device_name := device.get("device_name")): + button_key: str | None = None + button_name: str + device_name = cast(str | None, device.get("device_name")) + user_defined_name = bool(device_name) + if device_name: + button_name = device_name + else: # device name (button name) is missing, probably a caseta pico # try to get the name using the button number from the triggers # disable the button by default - has_device_name = False keypad_device = all_devices[device["parent_device"]] button_numbers = LEAP_TO_DEVICE_TYPE_SUBTYPE_MAP.get( keypad_device["type"], {}, ) - device_name = ( - button_numbers.get( - int(device["button_number"]), - f"button {device['button_number']}", - ) - .replace("_", " ") - .title() + button_key = button_numbers.get(int(device["button_number"])) + button_name = ( + button_key + or f"button {device['button_number']}".replace("_", " ").title() ) # Append the child device name to the end of the parent keypad # name to create the entity name - full_name = f"{parent_name} {device_name}" + full_name = f"{parent_name} {button_name}" # Set the device_info to the same as the Parent Keypad # The entities will be nested inside the keypad device buttons.append( LutronCasetaButtonDevice( - button_id, device, full_name, has_device_name, parent_device_info + button_id, + device, + button_key, + button_name, + full_name, + user_defined_name, + parent_device_info, ), ) diff --git a/homeassistant/components/lutron_caseta/button.py b/homeassistant/components/lutron_caseta/button.py index 297a321aca8..335ae6eb0ec 100644 --- a/homeassistant/components/lutron_caseta/button.py +++ b/homeassistant/components/lutron_caseta/button.py @@ -25,6 +25,8 @@ async def async_setup_entry( class LutronCasetaButton(LutronCasetaDevice, ButtonEntity): """Representation of a Lutron pico and keypad button.""" + _attr_has_entity_name = True + def __init__( self, data: LutronCasetaData, @@ -32,8 +34,8 @@ class LutronCasetaButton(LutronCasetaDevice, ButtonEntity): ) -> None: """Init a button entity.""" super().__init__(button_device.device, data) - self._attr_entity_registry_enabled_default = button_device.has_device_name - self._attr_name = button_device.full_name + self._attr_entity_registry_enabled_default = button_device.user_defined_name + self._attr_name = button_device.button_name self._attr_device_info = button_device.parent_device_info async def async_press(self) -> None: diff --git a/homeassistant/components/lutron_caseta/event.py b/homeassistant/components/lutron_caseta/event.py index 10b42298c55..0332b1e888f 100644 --- a/homeassistant/components/lutron_caseta/event.py +++ b/homeassistant/components/lutron_caseta/event.py @@ -39,6 +39,7 @@ class LutronCasetaButtonEvent(LutronCasetaDevice, EventEntity): _attr_device_class = EventDeviceClass.BUTTON _attr_event_types = [ACTION_PRESS, ACTION_RELEASE] + _attr_has_entity_name = True def __init__( self, @@ -48,7 +49,8 @@ class LutronCasetaButtonEvent(LutronCasetaDevice, EventEntity): ) -> None: """Init a button event entity.""" super().__init__(button_device.device, data) - self._attr_name = button_device.full_name + self._attr_name = button_device.button_name + self._attr_translation_key = button_device.button_key self._attr_device_info = button_device.parent_device_info self._button_id = button_device.button_id self._entry_id = entry_id diff --git a/homeassistant/components/lutron_caseta/models.py b/homeassistant/components/lutron_caseta/models.py index 7c3e9831e4b..3f9f336d097 100644 --- a/homeassistant/components/lutron_caseta/models.py +++ b/homeassistant/components/lutron_caseta/models.py @@ -41,8 +41,10 @@ class LutronCasetaButtonDevice: button_id: int device: dict + button_key: str | None + button_name: str full_name: str - has_device_name: bool + user_defined_name: bool parent_device_info: DeviceInfo