Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Buttons for the Elexa Guardian integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 
8 from aioguardian import Client
9 
11  ButtonDeviceClass,
12  ButtonEntity,
13  ButtonEntityDescription,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.dispatcher import async_dispatcher_send
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import GuardianData
22 from .const import API_SYSTEM_DIAGNOSTICS, DOMAIN
23 from .entity import ValveControllerEntity, ValveControllerEntityDescription
24 from .util import convert_exceptions_to_homeassistant_error
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  ButtonEntityDescription, ValveControllerEntityDescription
30 ):
31  """Describe a Guardian valve controller button."""
32 
33  push_action: Callable[[Client], Awaitable]
34 
35 
36 BUTTON_KIND_REBOOT = "reboot"
37 BUTTON_KIND_RESET_VALVE_DIAGNOSTICS = "reset_valve_diagnostics"
38 
39 
40 async def _async_reboot(client: Client) -> None:
41  """Reboot the Guardian."""
42  await client.system.reboot()
43 
44 
45 async def _async_valve_reset(client: Client) -> None:
46  """Reset the valve diagnostics on the Guardian."""
47  await client.valve.reset()
48 
49 
50 BUTTON_DESCRIPTIONS = (
52  key=BUTTON_KIND_REBOOT,
53  push_action=_async_reboot,
54  device_class=ButtonDeviceClass.RESTART,
55  # Buttons don't actually need a coordinator; we give them one so they can
56  # properly inherit from GuardianEntity:
57  api_category=API_SYSTEM_DIAGNOSTICS,
58  ),
60  key=BUTTON_KIND_RESET_VALVE_DIAGNOSTICS,
61  translation_key="reset_diagnostics",
62  push_action=_async_valve_reset,
63  # Buttons don't actually need a coordinator; we give them one so they can
64  # properly inherit from GuardianEntity:
65  api_category=API_SYSTEM_DIAGNOSTICS,
66  ),
67 )
68 
69 
71  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
72 ) -> None:
73  """Set up Guardian buttons based on a config entry."""
74  data: GuardianData = hass.data[DOMAIN][entry.entry_id]
75 
77  GuardianButton(entry, data, description) for description in BUTTON_DESCRIPTIONS
78  )
79 
80 
82  """Define a Guardian button."""
83 
84  _attr_device_class = ButtonDeviceClass.RESTART
85  _attr_entity_category = EntityCategory.CONFIG
86 
87  entity_description: ValveControllerButtonDescription
88 
89  def __init__(
90  self,
91  entry: ConfigEntry,
92  data: GuardianData,
93  description: ValveControllerButtonDescription,
94  ) -> None:
95  """Initialize."""
96  super().__init__(entry, data.valve_controller_coordinators, description)
97 
98  self._client_client_client = data.client
99 
100  @convert_exceptions_to_homeassistant_error
101  async def async_press(self) -> None:
102  """Send out a restart command."""
103  async with self._client_client_client:
104  await self.entity_descriptionentity_description.push_action(self._client_client_client)
105 
106  async_dispatcher_send(self.hasshasshass, self.coordinator.signal_reboot_requested)
None __init__(self, ConfigEntry entry, GuardianData data, ValveControllerButtonDescription description)
Definition: button.py:94
None _async_valve_reset(Client client)
Definition: button.py:45
None _async_reboot(Client client)
Definition: button.py:40
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:72
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193