Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for Freebox devices (Freebox v6 and Freebox mini 4K)."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 
9  ButtonDeviceClass,
10  ButtonEntity,
11  ButtonEntityDescription,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import EntityCategory
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import DOMAIN
19 from .router import FreeboxRouter
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  """Class describing Freebox button entities."""
25 
26  async_press: Callable[[FreeboxRouter], Awaitable]
27 
28 
29 BUTTON_DESCRIPTIONS: tuple[FreeboxButtonEntityDescription, ...] = (
31  key="reboot",
32  name="Reboot Freebox",
33  device_class=ButtonDeviceClass.RESTART,
34  entity_category=EntityCategory.CONFIG,
35  async_press=lambda router: router.reboot(),
36  ),
38  key="mark_calls_as_read",
39  name="Mark calls as read",
40  entity_category=EntityCategory.DIAGNOSTIC,
41  async_press=lambda router: router.call.mark_calls_log_as_read(),
42  ),
43 )
44 
45 
47  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
48 ) -> None:
49  """Set up the buttons."""
50  router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id]
51  entities = [
52  FreeboxButton(router, description) for description in BUTTON_DESCRIPTIONS
53  ]
54  async_add_entities(entities, True)
55 
56 
58  """Representation of a Freebox button."""
59 
60  entity_description: FreeboxButtonEntityDescription
61 
62  def __init__(
63  self, router: FreeboxRouter, description: FreeboxButtonEntityDescription
64  ) -> None:
65  """Initialize a Freebox button."""
66  self.entity_descriptionentity_description = description
67  self._router_router = router
68  self._attr_device_info_attr_device_info = router.device_info
69  self._attr_unique_id_attr_unique_id = f"{router.mac} {description.name}"
70 
71  async def async_press(self) -> None:
72  """Press the button."""
73  await self.entity_descriptionentity_description.async_press(self._router_router)
None __init__(self, FreeboxRouter router, FreeboxButtonEntityDescription description)
Definition: button.py:64
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:48