2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP binary sensors."""
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-04 12:50:06 +00:00
|
|
|
from typing import Any
|
2020-09-20 21:40:36 +00:00
|
|
|
|
2021-05-17 13:33:09 +00:00
|
|
|
from xknx import XKNX
|
2020-08-26 16:03:03 +00:00
|
|
|
from xknx.devices import BinarySensor as XknxBinarySensor
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
from homeassistant.components.binary_sensor import DEVICE_CLASSES, BinarySensorEntity
|
2021-05-17 13:33:09 +00:00
|
|
|
from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 12:50:06 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2021-04-10 16:12:43 +00:00
|
|
|
from homeassistant.util import dt
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-04-10 16:12:43 +00:00
|
|
|
from .const import ATTR_COUNTER, ATTR_LAST_KNX_UPDATE, ATTR_SOURCE, DOMAIN
|
2020-09-21 16:08:35 +00:00
|
|
|
from .knx_entity import KnxEntity
|
2021-05-17 13:33:09 +00:00
|
|
|
from .schema import BinarySensorSchema
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
|
2021-03-19 09:22:18 +00:00
|
|
|
async def async_setup_platform(
|
2021-03-27 21:20:11 +00:00
|
|
|
hass: HomeAssistant,
|
2021-03-19 09:22:18 +00:00
|
|
|
config: ConfigType,
|
2021-05-04 12:50:06 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-03-19 09:22:18 +00:00
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Set up binary sensor(s) for KNX platform."""
|
2021-05-17 13:33:09 +00:00
|
|
|
if not discovery_info or not discovery_info["platform_config"]:
|
|
|
|
return
|
|
|
|
|
|
|
|
platform_config = discovery_info["platform_config"]
|
|
|
|
xknx: XKNX = hass.data[DOMAIN].xknx
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
entities = []
|
2021-05-17 13:33:09 +00:00
|
|
|
for entity_config in platform_config:
|
|
|
|
entities.append(KNXBinarySensor(xknx, entity_config))
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
class KNXBinarySensor(KnxEntity, BinarySensorEntity):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Representation of a KNX binary sensor."""
|
|
|
|
|
2021-05-17 13:33:09 +00:00
|
|
|
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of KNX binary sensor."""
|
2021-03-19 09:22:18 +00:00
|
|
|
self._device: XknxBinarySensor
|
2021-05-17 13:33:09 +00:00
|
|
|
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._device_class: str | None = config.get(CONF_DEVICE_CLASS)
|
2021-04-28 13:50:01 +00:00
|
|
|
self._unique_id = f"{self._device.remote_value.group_address_state}"
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
2021-03-19 09:22:18 +00:00
|
|
|
def device_class(self) -> str | None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return the class of this sensor."""
|
2021-05-17 13:33:09 +00:00
|
|
|
if self._device_class in DEVICE_CLASSES:
|
|
|
|
return self._device_class
|
2020-09-21 16:08:35 +00:00
|
|
|
return None
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
2021-03-19 09:22:18 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return true if the binary sensor is on."""
|
2020-09-21 16:08:35 +00:00
|
|
|
return self._device.is_on()
|
2020-09-20 21:40:36 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-18 12:07:04 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2020-09-20 21:40:36 +00:00
|
|
|
"""Return device specific state attributes."""
|
2021-04-10 16:12:43 +00:00
|
|
|
attr: dict[str, Any] = {}
|
|
|
|
|
2021-02-20 18:45:04 +00:00
|
|
|
if self._device.counter is not None:
|
2021-04-10 16:12:43 +00:00
|
|
|
attr[ATTR_COUNTER] = self._device.counter
|
|
|
|
if self._device.last_telegram is not None:
|
|
|
|
attr[ATTR_SOURCE] = str(self._device.last_telegram.source_address)
|
|
|
|
attr[ATTR_LAST_KNX_UPDATE] = str(
|
|
|
|
dt.as_utc(self._device.last_telegram.timestamp)
|
|
|
|
)
|
|
|
|
return attr
|
2020-11-13 08:37:45 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def force_update(self) -> bool:
|
|
|
|
"""
|
|
|
|
Return True if state updates should be forced.
|
|
|
|
|
|
|
|
If True, a state change will be triggered anytime the state property is
|
|
|
|
updated, not just when the value changes.
|
|
|
|
"""
|
|
|
|
return self._device.ignore_internal_state
|