This commit is contained in:
J. Nick Koston 2024-07-29 21:43:08 -05:00
parent 15d8b84074
commit 4d01e0a773
No known key found for this signature in database
4 changed files with 29 additions and 16 deletions

View File

@ -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,
),
)

View File

@ -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:

View File

@ -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

View File

@ -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