Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """OpenGarage button."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any, cast
8 
9 from opengarage import OpenGarage
10 
12  ButtonDeviceClass,
13  ButtonEntity,
14  ButtonEntityDescription,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import EntityCategory
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import DOMAIN
22 from .coordinator import OpenGarageDataUpdateCoordinator
23 from .entity import OpenGarageEntity
24 
25 
26 @dataclass(frozen=True, kw_only=True)
28  """OpenGarage Browser button description."""
29 
30  press_action: Callable[[OpenGarage], Any]
31 
32 
33 BUTTONS: tuple[OpenGarageButtonEntityDescription, ...] = (
35  key="restart",
36  device_class=ButtonDeviceClass.RESTART,
37  entity_category=EntityCategory.CONFIG,
38  press_action=lambda opengarage: opengarage.reboot(),
39  ),
40 )
41 
42 
44  hass: HomeAssistant,
45  config_entry: ConfigEntry,
46  async_add_entities: AddEntitiesCallback,
47 ) -> None:
48  """Set up the OpenGarage button entities."""
49  coordinator: OpenGarageDataUpdateCoordinator = hass.data[DOMAIN][
50  config_entry.entry_id
51  ]
52 
55  coordinator, cast(str, config_entry.unique_id), description
56  )
57  for description in BUTTONS
58  )
59 
60 
62  """Representation of an OpenGarage button."""
63 
64  entity_description: OpenGarageButtonEntityDescription
65 
66  def __init__(
67  self,
68  coordinator: OpenGarageDataUpdateCoordinator,
69  device_id: str,
70  description: OpenGarageButtonEntityDescription,
71  ) -> None:
72  """Initialize the button."""
73  super().__init__(coordinator, device_id, description)
74 
75  async def async_press(self) -> None:
76  """Press the button."""
77  await self.entity_descriptionentity_description.press_action(
78  self.coordinator.open_garage_connection
79  )
80  await self.coordinator.async_refresh()
None __init__(self, OpenGarageDataUpdateCoordinator coordinator, str device_id, OpenGarageButtonEntityDescription description)
Definition: button.py:71
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:47