Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for OpenTherm Gateway buttons."""
2 
3 from collections.abc import Awaitable, Callable
4 from dataclasses import dataclass
5 
6 import pyotgw.vars as gw_vars
7 
9  ButtonDeviceClass,
10  ButtonEntity,
11  ButtonEntityDescription,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import CONF_ID, EntityCategory
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import OpenThermGatewayHub
19 from .const import DATA_GATEWAYS, DATA_OPENTHERM_GW, GATEWAY_DEVICE_DESCRIPTION
20 from .entity import OpenThermEntity, OpenThermEntityDescription
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  ButtonEntityDescription, OpenThermEntityDescription
26 ):
27  """Describes an opentherm_gw button entity."""
28 
29  action: Callable[[OpenThermGatewayHub], Awaitable]
30 
31 
32 BUTTON_DESCRIPTIONS: tuple[OpenThermButtonEntityDescription, ...] = (
34  key="restart_button",
35  device_class=ButtonDeviceClass.RESTART,
36  device_description=GATEWAY_DEVICE_DESCRIPTION,
37  action=lambda hub: hub.gateway.set_mode(gw_vars.OTGW_MODE_RESET),
38  ),
39 )
40 
41 
43  hass: HomeAssistant,
44  config_entry: ConfigEntry,
45  async_add_entities: AddEntitiesCallback,
46 ) -> None:
47  """Set up the OpenTherm Gateway buttons."""
48  gw_hub = hass.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][config_entry.data[CONF_ID]]
49 
51  OpenThermButton(gw_hub, description) for description in BUTTON_DESCRIPTIONS
52  )
53 
54 
56  """Representation of an OpenTherm button."""
57 
58  _attr_entity_category = EntityCategory.CONFIG
59  entity_description: OpenThermButtonEntityDescription
60 
61  async def async_press(self) -> None:
62  """Perform button action."""
63  await self.entity_descriptionentity_description.action(self._gateway_gateway)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:46