Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for ZHA button."""
2 
3 from __future__ import annotations
4 
5 import functools
6 import logging
7 
8 from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.dispatcher import async_dispatcher_connect
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .entity import ZHAEntity
16 from .helpers import (
17  SIGNAL_ADD_ENTITIES,
18  EntityData,
19  async_add_entities as zha_async_add_entities,
20  convert_zha_error_to_ha_error,
21  get_zha_data,
22 )
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
28  hass: HomeAssistant,
29  config_entry: ConfigEntry,
30  async_add_entities: AddEntitiesCallback,
31 ) -> None:
32  """Set up the Zigbee Home Automation button from config entry."""
33  zha_data = get_zha_data(hass)
34  entities_to_create = zha_data.platforms[Platform.BUTTON]
35 
37  hass,
38  SIGNAL_ADD_ENTITIES,
39  functools.partial(
40  zha_async_add_entities, async_add_entities, ZHAButton, entities_to_create
41  ),
42  )
43  config_entry.async_on_unload(unsub)
44 
45 
47  """Defines a ZHA button."""
48 
49  def __init__(self, entity_data: EntityData) -> None:
50  """Initialize the ZHA binary sensor."""
51  super().__init__(entity_data)
52  if self.entity_data.entity.info_object.device_class is not None:
53  self._attr_device_class_attr_device_class = ButtonDeviceClass(
54  self.entity_data.entity.info_object.device_class
55  )
56 
57  @convert_zha_error_to_ha_error
58  async def async_press(self) -> None:
59  """Send out a update command."""
60  await self.entity_data.entity.async_press()
None __init__(self, EntityData entity_data)
Definition: button.py:49
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:31
HAZHAData get_zha_data(HomeAssistant hass)
Definition: helpers.py:1020
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103