Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Platform for button integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 
8 from devolo_plc_api.device import Device
9 from devolo_plc_api.exceptions.device import DevicePasswordProtected, DeviceUnavailable
10 
12  ButtonDeviceClass,
13  ButtonEntity,
14  ButtonEntityDescription,
15 )
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import HomeAssistantError
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import DevoloHomeNetworkConfigEntry
22 from .const import DOMAIN, IDENTIFY, PAIRING, RESTART, START_WPS
23 from .entity import DevoloEntity
24 
25 PARALLEL_UPDATES = 0
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """Describes devolo button entity."""
31 
32  press_func: Callable[[Device], Awaitable[bool]]
33 
34 
35 BUTTON_TYPES: dict[str, DevoloButtonEntityDescription] = {
37  key=IDENTIFY,
38  entity_category=EntityCategory.DIAGNOSTIC,
39  device_class=ButtonDeviceClass.IDENTIFY,
40  press_func=lambda device: device.plcnet.async_identify_device_start(), # type: ignore[union-attr]
41  ),
43  key=PAIRING,
44  press_func=lambda device: device.plcnet.async_pair_device(), # type: ignore[union-attr]
45  ),
47  key=RESTART,
48  device_class=ButtonDeviceClass.RESTART,
49  entity_category=EntityCategory.CONFIG,
50  press_func=lambda device: device.device.async_restart(), # type: ignore[union-attr]
51  ),
53  key=START_WPS,
54  press_func=lambda device: device.device.async_start_wps(), # type: ignore[union-attr]
55  ),
56 }
57 
58 
60  hass: HomeAssistant,
61  entry: DevoloHomeNetworkConfigEntry,
62  async_add_entities: AddEntitiesCallback,
63 ) -> None:
64  """Get all devices and buttons and setup them via config entry."""
65  device = entry.runtime_data.device
66 
67  entities: list[DevoloButtonEntity] = []
68  if device.plcnet:
69  entities.append(
71  entry,
72  BUTTON_TYPES[IDENTIFY],
73  )
74  )
75  entities.append(
77  entry,
78  BUTTON_TYPES[PAIRING],
79  )
80  )
81  if device.device and "restart" in device.device.features:
82  entities.append(
84  entry,
85  BUTTON_TYPES[RESTART],
86  )
87  )
88  if device.device and "wifi1" in device.device.features:
89  entities.append(
91  entry,
92  BUTTON_TYPES[START_WPS],
93  )
94  )
95  async_add_entities(entities)
96 
97 
99  """Representation of a devolo button."""
100 
101  entity_description: DevoloButtonEntityDescription
102 
103  def __init__(
104  self,
105  entry: DevoloHomeNetworkConfigEntry,
106  description: DevoloButtonEntityDescription,
107  ) -> None:
108  """Initialize entity."""
109  self.entity_descriptionentity_description = description
110  super().__init__(entry)
111 
112  async def async_press(self) -> None:
113  """Handle the button press."""
114  try:
115  await self.entity_descriptionentity_description.press_func(self.devicedevice)
116  except DevicePasswordProtected as ex:
117  self.entryentry.async_start_reauth(self.hasshass)
118  raise HomeAssistantError(
119  translation_domain=DOMAIN,
120  translation_key="password_protected",
121  translation_placeholders={"title": self.entryentry.title},
122  ) from ex
123  except DeviceUnavailable as ex:
124  raise HomeAssistantError(
125  translation_domain=DOMAIN,
126  translation_key="no_response",
127  translation_placeholders={"title": self.entryentry.title},
128  ) from ex
None __init__(self, DevoloHomeNetworkConfigEntry entry, DevoloButtonEntityDescription description)
Definition: button.py:107
None async_setup_entry(HomeAssistant hass, DevoloHomeNetworkConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:63