Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for KNX/IP buttons."""
2 
3 from __future__ import annotations
4 
5 from xknx.devices import RawValue as XknxRawValue
6 
7 from homeassistant import config_entries
8 from homeassistant.components.button import ButtonEntity
9 from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, CONF_PAYLOAD, Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType
13 
14 from . import KNXModule
15 from .const import CONF_PAYLOAD_LENGTH, KNX_ADDRESS, KNX_MODULE_KEY
16 from .entity import KnxYamlEntity
17 
18 
20  hass: HomeAssistant,
21  config_entry: config_entries.ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up the KNX binary sensor platform."""
25  knx_module = hass.data[KNX_MODULE_KEY]
26  config: list[ConfigType] = knx_module.config_yaml[Platform.BUTTON]
27 
28  async_add_entities(KNXButton(knx_module, entity_config) for entity_config in config)
29 
30 
32  """Representation of a KNX button."""
33 
34  _device: XknxRawValue
35 
36  def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
37  """Initialize a KNX button."""
38  super().__init__(
39  knx_module=knx_module,
40  device=XknxRawValue(
41  xknx=knx_module.xknx,
42  name=config[CONF_NAME],
43  payload_length=config[CONF_PAYLOAD_LENGTH],
44  group_address=config[KNX_ADDRESS],
45  ),
46  )
47  self._payload_payload = config[CONF_PAYLOAD]
48  self._attr_entity_category_attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
49  self._attr_unique_id_attr_unique_id = (
50  f"{self._device.remote_value.group_address}_{self._payload}"
51  )
52 
53  async def async_press(self) -> None:
54  """Press the button."""
55  await self._device_device.set(self._payload_payload)
None __init__(self, KNXModule knx_module, ConfigType config)
Definition: button.py:36
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:23