Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for LinkPlay buttons."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from dataclasses import dataclass
7 import logging
8 from typing import Any
9 
10 from linkplay.bridge import LinkPlayBridge
11 
13  ButtonDeviceClass,
14  ButtonEntity,
15  ButtonEntityDescription,
16 )
17 from homeassistant.const import EntityCategory
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import LinkPlayConfigEntry
22 from .entity import LinkPlayBaseEntity, exception_wrap
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  """Class describing LinkPlay button entities."""
30 
31  remote_function: Callable[[LinkPlayBridge], Coroutine[Any, Any, None]]
32 
33 
34 BUTTON_TYPES: tuple[LinkPlayButtonEntityDescription, ...] = (
36  key="timesync",
37  translation_key="timesync",
38  remote_function=lambda linkplay_bridge: linkplay_bridge.device.timesync(),
39  entity_category=EntityCategory.CONFIG,
40  ),
42  key="restart",
43  device_class=ButtonDeviceClass.RESTART,
44  remote_function=lambda linkplay_bridge: linkplay_bridge.device.reboot(),
45  entity_category=EntityCategory.CONFIG,
46  ),
47 )
48 
49 
51  hass: HomeAssistant,
52  config_entry: LinkPlayConfigEntry,
53  async_add_entities: AddEntitiesCallback,
54 ) -> None:
55  """Set up the LinkPlay buttons from config entry."""
56 
57  # add entities
59  LinkPlayButton(config_entry.runtime_data.bridge, description)
60  for description in BUTTON_TYPES
61  )
62 
63 
65  """Representation of LinkPlay button."""
66 
67  entity_description: LinkPlayButtonEntityDescription
68 
69  def __init__(
70  self,
71  bridge: LinkPlayBridge,
72  description: LinkPlayButtonEntityDescription,
73  ) -> None:
74  """Initialize LinkPlay button."""
75  super().__init__(bridge)
76  self.entity_descriptionentity_description = description
77  self._attr_unique_id_attr_unique_id = f"{bridge.device.uuid}-{description.key}"
78 
79  @exception_wrap
80  async def async_press(self) -> None:
81  """Press the button."""
82  await self.entity_descriptionentity_description.remote_function(self._bridge_bridge)
None __init__(self, LinkPlayBridge bridge, LinkPlayButtonEntityDescription description)
Definition: button.py:73
None async_setup_entry(HomeAssistant hass, LinkPlayConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:54