Files
homeassistant-core/homeassistant/components/knx/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
3.3 KiB
Python
Raw Normal View History

2019-02-14 05:35:12 +01:00
"""Support for KNX/IP binary sensors."""
2021-03-18 13:07:04 +01:00
from __future__ import annotations
2021-05-04 13:50:06 +01:00
from typing import Any
2020-09-20 23:40:36 +02:00
from xknx import XKNX
from xknx.devices import BinarySensor as XknxBinarySensor
from homeassistant import config_entries
2021-05-23 10:42:17 +02:00
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.const import (
CONF_DEVICE_CLASS,
CONF_ENTITY_CATEGORY,
CONF_NAME,
STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
2021-12-03 18:29:38 +01:00
Platform,
)
from homeassistant.core import HomeAssistant
2021-05-04 13:50:06 +01:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
2021-12-03 18:29:38 +01:00
from .const import ATTR_COUNTER, ATTR_SOURCE, DATA_KNX_CONFIG, DOMAIN
from .knx_entity import KnxEntity
from .schema import BinarySensorSchema
async def async_setup_entry(
hass: HomeAssistant,
config_entry: config_entries.ConfigEntry,
2021-05-04 13:50:06 +01:00
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the KNX binary sensor platform."""
xknx: XKNX = hass.data[DOMAIN].xknx
config: ConfigType = hass.data[DATA_KNX_CONFIG]
2021-06-26 14:30:36 +02:00
async_add_entities(
KNXBinarySensor(xknx, entity_config)
2021-12-03 18:29:38 +01:00
for entity_config in config[Platform.BINARY_SENSOR]
2021-06-26 14:30:36 +02:00
)
class KNXBinarySensor(KnxEntity, BinarySensorEntity, RestoreEntity):
"""Representation of a KNX binary sensor."""
2021-06-26 14:30:36 +02:00
_device: XknxBinarySensor
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
2018-01-21 07:35:38 +01:00
"""Initialize of KNX binary sensor."""
super().__init__(
device=XknxBinarySensor(
xknx,
name=config[CONF_NAME],
group_address_state=config[BinarySensorSchema.CONF_STATE_ADDRESS],
invert=config[BinarySensorSchema.CONF_INVERT],
sync_state=config[BinarySensorSchema.CONF_SYNC_STATE],
ignore_internal_state=config[
BinarySensorSchema.CONF_IGNORE_INTERNAL_STATE
],
context_timeout=config.get(BinarySensorSchema.CONF_CONTEXT_TIMEOUT),
reset_after=config.get(BinarySensorSchema.CONF_RESET_AFTER),
)
)
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
2021-06-26 14:30:36 +02:00
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
self._attr_force_update = self._device.ignore_internal_state
self._attr_unique_id = str(self._device.remote_value.group_address_state)
async def async_added_to_hass(self) -> None:
"""Restore last state."""
await super().async_added_to_hass()
if (
last_state := await self.async_get_last_state()
) and last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
await self._device.remote_value.update_value(last_state.state == STATE_ON)
@property
def is_on(self) -> bool:
"""Return true if the binary sensor is on."""
return self._device.is_on()
2020-09-20 23:40:36 +02:00
@property
2021-03-18 13:07:04 +01:00
def extra_state_attributes(self) -> dict[str, Any] | None:
2020-09-20 23:40:36 +02:00
"""Return device specific state attributes."""
attr: dict[str, Any] = {}
2021-02-20 19:45:04 +01:00
if self._device.counter is not None:
attr[ATTR_COUNTER] = self._device.counter
if self._device.last_telegram is not None:
attr[ATTR_SOURCE] = str(self._device.last_telegram.source_address)
return attr