2019-02-14 05:35:12 +01:00
|
|
|
"""Support for KNX/IP sensors."""
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx.devices import Sensor as XknxSensor
|
2016-09-14 03:21:43 +02:00
|
|
|
|
2017-09-07 09:11:55 +02:00
|
|
|
from homeassistant.core import callback
|
2018-01-21 07:35:38 +01:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-09-14 03:21:43 +02:00
|
|
|
|
2019-03-20 22:56:46 -07:00
|
|
|
from . import ATTR_DISCOVER_DEVICES, DATA_KNX
|
|
|
|
|
2017-06-27 14:25:54 +09:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-09-07 09:11:55 +02:00
|
|
|
"""Set up sensor(s) for KNX platform."""
|
|
|
|
if discovery_info is not None:
|
2018-08-24 16:37:30 +02:00
|
|
|
async_add_entities_discovery(hass, discovery_info, async_add_entities)
|
2017-09-07 09:11:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2018-08-24 16:37:30 +02:00
|
|
|
def async_add_entities_discovery(hass, discovery_info, async_add_entities):
|
2017-09-07 09:11:55 +02:00
|
|
|
"""Set up sensors for KNX platform configured via xknx.yaml."""
|
|
|
|
entities = []
|
|
|
|
for device_name in discovery_info[ATTR_DISCOVER_DEVICES]:
|
|
|
|
device = hass.data[DATA_KNX].xknx.devices[device_name]
|
2018-10-15 03:29:36 +02:00
|
|
|
entities.append(KNXSensor(device))
|
2018-08-24 16:37:30 +02:00
|
|
|
async_add_entities(entities)
|
2017-09-07 09:11:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class KNXSensor(Entity):
|
|
|
|
"""Representation of a KNX sensor."""
|
|
|
|
|
2020-08-26 18:03:03 +02:00
|
|
|
def __init__(self, device: XknxSensor):
|
2018-01-21 07:35:38 +01:00
|
|
|
"""Initialize of a KNX sensor."""
|
2018-08-26 21:25:39 +02:00
|
|
|
self.device = device
|
2017-09-07 09:11:55 +02:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_register_callbacks(self):
|
|
|
|
"""Register callbacks to update hass after device was changed."""
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2018-02-24 19:24:33 +01:00
|
|
|
async def after_update_callback(device):
|
2018-01-21 07:35:38 +01:00
|
|
|
"""Call after device was updated."""
|
2020-04-03 00:34:50 -07:00
|
|
|
self.async_write_ha_state()
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2018-08-26 21:25:39 +02:00
|
|
|
self.device.register_device_updated_cb(after_update_callback)
|
2017-06-27 14:25:54 +09:00
|
|
|
|
2018-10-15 03:29:36 +02:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Store register state change callback."""
|
|
|
|
self.async_register_callbacks()
|
|
|
|
|
2020-05-13 15:19:00 +02:00
|
|
|
async def async_update(self):
|
|
|
|
"""Update the state from KNX."""
|
|
|
|
await self.device.sync()
|
|
|
|
|
2017-09-07 09:11:55 +02:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the KNX device."""
|
2018-08-26 21:25:39 +02:00
|
|
|
return self.device.name
|
2016-09-14 03:21:43 +02:00
|
|
|
|
2018-01-07 22:39:14 +01:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self.hass.data[DATA_KNX].connected
|
|
|
|
|
2017-09-07 09:11:55 +02:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed within KNX."""
|
|
|
|
return False
|
2016-09-14 03:21:43 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2017-09-07 09:11:55 +02:00
|
|
|
"""Return the state of the sensor."""
|
2018-08-26 21:25:39 +02:00
|
|
|
return self.device.resolve_state()
|
2016-11-08 21:00:33 -08:00
|
|
|
|
2016-11-27 21:21:05 +01:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2017-09-07 09:11:55 +02:00
|
|
|
"""Return the unit this state is expressed in."""
|
2018-08-26 21:25:39 +02:00
|
|
|
return self.device.unit_of_measurement()
|
2017-06-27 14:25:54 +09:00
|
|
|
|
2019-07-11 22:01:37 +02:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return self.device.ha_device_class()
|
|
|
|
|
2017-06-27 14:25:54 +09:00
|
|
|
@property
|
2017-09-07 09:11:55 +02:00
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return None
|