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
|
|
|
|
|
|
|
|
from typing import Any
|
2020-09-20 21:40:36 +00:00
|
|
|
|
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
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
from .const import ATTR_COUNTER, DOMAIN
|
|
|
|
from .knx_entity import KnxEntity
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Set up binary sensor(s) for KNX platform."""
|
|
|
|
entities = []
|
2020-09-21 16:08:35 +00:00
|
|
|
for device in hass.data[DOMAIN].xknx.devices:
|
2020-08-30 18:13:47 +00:00
|
|
|
if isinstance(device, XknxBinarySensor):
|
|
|
|
entities.append(KNXBinarySensor(device))
|
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."""
|
|
|
|
|
2020-08-26 16:03:03 +00:00
|
|
|
def __init__(self, device: XknxBinarySensor):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of KNX binary sensor."""
|
2020-09-21 16:08:35 +00:00
|
|
|
super().__init__(device)
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this sensor."""
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.device_class in DEVICE_CLASSES:
|
|
|
|
return self._device.device_class
|
|
|
|
return None
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""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-02-20 18:45:04 +00:00
|
|
|
if self._device.counter is not None:
|
|
|
|
return {ATTR_COUNTER: self._device.counter}
|
|
|
|
return None
|
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
|