Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for KNX/IP binary sensors."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from xknx.devices import BinarySensor as XknxBinarySensor
8 
9 from homeassistant import config_entries
10 from homeassistant.components.binary_sensor import BinarySensorEntity
11 from homeassistant.const import (
12  CONF_DEVICE_CLASS,
13  CONF_ENTITY_CATEGORY,
14  CONF_NAME,
15  STATE_ON,
16  STATE_UNAVAILABLE,
17  STATE_UNKNOWN,
18  Platform,
19 )
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.restore_state import RestoreEntity
23 from homeassistant.helpers.typing import ConfigType
24 
25 from . import KNXModule
26 from .const import ATTR_COUNTER, ATTR_SOURCE, KNX_MODULE_KEY
27 from .entity import KnxYamlEntity
28 from .schema import BinarySensorSchema
29 
30 
32  hass: HomeAssistant,
33  config_entry: config_entries.ConfigEntry,
34  async_add_entities: AddEntitiesCallback,
35 ) -> None:
36  """Set up the KNX binary sensor platform."""
37  knx_module = hass.data[KNX_MODULE_KEY]
38  config: list[ConfigType] = knx_module.config_yaml[Platform.BINARY_SENSOR]
39 
41  KNXBinarySensor(knx_module, entity_config) for entity_config in config
42  )
43 
44 
46  """Representation of a KNX binary sensor."""
47 
48  _device: XknxBinarySensor
49 
50  def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
51  """Initialize of KNX binary sensor."""
52  super().__init__(
53  knx_module=knx_module,
54  device=XknxBinarySensor(
55  xknx=knx_module.xknx,
56  name=config[CONF_NAME],
57  group_address_state=config[BinarySensorSchema.CONF_STATE_ADDRESS],
58  invert=config[BinarySensorSchema.CONF_INVERT],
59  sync_state=config[BinarySensorSchema.CONF_SYNC_STATE],
60  ignore_internal_state=config[
61  BinarySensorSchema.CONF_IGNORE_INTERNAL_STATE
62  ],
63  context_timeout=config.get(BinarySensorSchema.CONF_CONTEXT_TIMEOUT),
64  reset_after=config.get(BinarySensorSchema.CONF_RESET_AFTER),
65  ),
66  )
67  self._attr_entity_category_attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
68  self._attr_device_class_attr_device_class = config.get(CONF_DEVICE_CLASS)
69  self._attr_force_update_attr_force_update = self._device_device.ignore_internal_state
70  self._attr_unique_id_attr_unique_id = str(self._device_device.remote_value.group_address_state)
71 
72  async def async_added_to_hass(self) -> None:
73  """Restore last state."""
74  await super().async_added_to_hass()
75  if (
76  last_state := await self.async_get_last_stateasync_get_last_state()
77  ) and last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
78  self._device_device.remote_value.update_value(last_state.state == STATE_ON)
79 
80  @property
81  def is_on(self) -> bool:
82  """Return true if the binary sensor is on."""
83  return self._device_device.is_on()
84 
85  @property
86  def extra_state_attributes(self) -> dict[str, Any] | None:
87  """Return device specific state attributes."""
88  attr: dict[str, Any] = {}
89 
90  if self._device_device.counter is not None:
91  attr[ATTR_COUNTER] = self._device_device.counter
92  if self._device_device.last_telegram is not None:
93  attr[ATTR_SOURCE] = str(self._device_device.last_telegram.source_address)
94  return attr
None __init__(self, KNXModule knx_module, ConfigType config)
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)