2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP binary sensors."""
|
2020-08-26 16:03:03 +00:00
|
|
|
from xknx.devices import BinarySensor as XknxBinarySensor
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2020-08-26 16:03:03 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2017-09-07 07:11:55 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
|
2020-08-30 18:13:47 +00:00
|
|
|
from . import DATA_KNX
|
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-08-30 18:13:47 +00:00
|
|
|
for device in hass.data[DATA_KNX].xknx.devices:
|
|
|
|
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-04-23 19:57:07 +00:00
|
|
|
class KNXBinarySensor(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."""
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device = device
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_register_callbacks(self):
|
|
|
|
"""Register callbacks to update hass after device was changed."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def after_update_callback(device):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Call after device was updated."""
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device.register_device_updated_cb(after_update_callback)
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2018-10-15 01:29:36 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Store register state change callback."""
|
|
|
|
self.async_register_callbacks()
|
|
|
|
|
2020-05-13 13:19:00 +00:00
|
|
|
async def async_update(self):
|
|
|
|
"""Request a state update from KNX bus."""
|
|
|
|
await self.device.sync()
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the KNX device."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.name
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2018-01-07 21:39:14 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self.hass.data[DATA_KNX].connected
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed within KNX."""
|
|
|
|
return False
|
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."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.device_class
|
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."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.is_on()
|