Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for pico and keypad buttons."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.button import ButtonEntity
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.device_registry import DeviceInfo
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .device_trigger import LEAP_TO_DEVICE_TYPE_SUBTYPE_MAP
13 from .entity import LutronCasetaEntity
14 from .models import LutronCasetaConfigEntry, LutronCasetaData
15 
16 
18  hass: HomeAssistant,
19  config_entry: LutronCasetaConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up Lutron pico and keypad buttons."""
23  data = config_entry.runtime_data
24  bridge = data.bridge
25  button_devices = bridge.get_buttons()
26  all_devices = data.bridge.get_devices()
27  keypads = data.keypad_data.keypads
28  entities: list[LutronCasetaButton] = []
29 
30  for device in button_devices.values():
31  parent_keypad = keypads[device["parent_device"]]
32  parent_device_info = parent_keypad["device_info"]
33 
34  enabled_default = True
35  if not (device_name := device.get("device_name")):
36  # device name (button name) is missing, probably a caseta pico
37  # try to get the name using the button number from the triggers
38  # disable the button by default
39  enabled_default = False
40  keypad_device = all_devices[device["parent_device"]]
41  button_numbers = LEAP_TO_DEVICE_TYPE_SUBTYPE_MAP.get(
42  keypad_device["type"],
43  {},
44  )
45  device_name = (
46  button_numbers.get(
47  int(device["button_number"]),
48  f"button {device['button_number']}",
49  )
50  .replace("_", " ")
51  .title()
52  )
53 
54  # Append the child device name to the end of the parent keypad
55  # name to create the entity name
56  full_name = f'{parent_device_info.get("name")} {device_name}'
57  # Set the device_info to the same as the Parent Keypad
58  # The entities will be nested inside the keypad device
59  entities.append(
61  device, data, full_name, enabled_default, parent_device_info
62  ),
63  )
64 
65  async_add_entities(entities)
66 
67 
69  """Representation of a Lutron pico and keypad button."""
70 
71  def __init__(
72  self,
73  device: dict[str, Any],
74  data: LutronCasetaData,
75  full_name: str,
76  enabled_default: bool,
77  device_info: DeviceInfo,
78  ) -> None:
79  """Init a button entity."""
80  super().__init__(device, data)
81  self._attr_entity_registry_enabled_default_attr_entity_registry_enabled_default = enabled_default
82  self._attr_name_attr_name_attr_name = full_name
83  self._attr_device_info_attr_device_info_attr_device_info = device_info
84 
85  async def async_press(self) -> None:
86  """Send a button press event."""
87  await self._smartbridge_smartbridge.tap_button(self.device_iddevice_id)
88 
89  @property
90  def serial(self):
91  """Buttons shouldn't have serial numbers, Return None."""
92  return None
None __init__(self, dict[str, Any] device, LutronCasetaData data, str full_name, bool enabled_default, DeviceInfo device_info)
Definition: button.py:78
None async_setup_entry(HomeAssistant hass, LutronCasetaConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:21