Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Interfaces with TotalConnect buttons."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 
6 from total_connect_client.location import TotalConnectLocation
7 from total_connect_client.zone import TotalConnectZone
8 
9 from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import EntityCategory
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import DOMAIN
16 from .coordinator import TotalConnectDataUpdateCoordinator
17 from .entity import TotalConnectLocationEntity, TotalConnectZoneEntity
18 
19 
20 @dataclass(frozen=True, kw_only=True)
22  """TotalConnect button description."""
23 
24  press_fn: Callable[[TotalConnectLocation], None]
25 
26 
27 PANEL_BUTTONS: tuple[TotalConnectButtonEntityDescription, ...] = (
29  key="clear_bypass",
30  translation_key="clear_bypass",
31  press_fn=lambda location: location.clear_bypass(),
32  ),
34  key="bypass_all",
35  translation_key="bypass_all",
36  press_fn=lambda location: location.zone_bypass_all(),
37  ),
38 )
39 
40 
42  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
43 ) -> None:
44  """Set up TotalConnect buttons based on a config entry."""
45  buttons: list = []
46  coordinator: TotalConnectDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
47 
48  for location_id, location in coordinator.client.locations.items():
49  buttons.extend(
50  TotalConnectPanelButton(coordinator, location, description)
51  for description in PANEL_BUTTONS
52  )
53 
54  buttons.extend(
55  TotalConnectZoneBypassButton(coordinator, zone, location_id)
56  for zone in location.zones.values()
57  if zone.can_be_bypassed
58  )
59 
60  async_add_entities(buttons)
61 
62 
64  """Represent a TotalConnect zone bypass button."""
65 
66  _attr_translation_key = "bypass"
67  _attr_entity_category = EntityCategory.DIAGNOSTIC
68 
69  def __init__(
70  self,
71  coordinator: TotalConnectDataUpdateCoordinator,
72  zone: TotalConnectZone,
73  location_id: str,
74  ) -> None:
75  """Initialize the TotalConnect status."""
76  super().__init__(coordinator, zone, location_id, "bypass")
77 
78  def press(self) -> None:
79  """Press the bypass button."""
80  self._zone_zone.bypass()
81 
82 
84  """Generic TotalConnect panel button."""
85 
86  entity_description: TotalConnectButtonEntityDescription
87 
88  def __init__(
89  self,
90  coordinator: TotalConnectDataUpdateCoordinator,
91  location: TotalConnectLocation,
92  entity_description: TotalConnectButtonEntityDescription,
93  ) -> None:
94  """Initialize the TotalConnect button."""
95  super().__init__(coordinator, location)
96  self.entity_descriptionentity_description = entity_description
97  self._attr_unique_id_attr_unique_id = f"{location.location_id}_{entity_description.key}"
98 
99  def press(self) -> None:
100  """Press the button."""
101  self.entity_descriptionentity_description.press_fn(self._location_location)
None __init__(self, TotalConnectDataUpdateCoordinator coordinator, TotalConnectLocation location, TotalConnectButtonEntityDescription entity_description)
Definition: button.py:93
None __init__(self, TotalConnectDataUpdateCoordinator coordinator, TotalConnectZone zone, str location_id)
Definition: button.py:74
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:43