2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP sensors."""
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx.devices import Sensor as XknxSensor
|
2016-09-14 01:21:43 +00:00
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
from homeassistant.components.sensor import DEVICE_CLASSES
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-09-14 01:21:43 +00:00
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
from .knx_entity import KnxEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-06-27 05:25:54 +00:00
|
|
|
|
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 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, XknxSensor):
|
|
|
|
entities.append(KNXSensor(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 KNXSensor(KnxEntity, Entity):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Representation of a KNX sensor."""
|
|
|
|
|
2020-08-26 16:03:03 +00:00
|
|
|
def __init__(self, device: XknxSensor):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of a KNX sensor."""
|
2020-09-21 16:08:35 +00:00
|
|
|
super().__init__(device)
|
2016-09-14 01:21:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return the state of the sensor."""
|
2020-09-21 16:08:35 +00:00
|
|
|
return self._device.resolve_state()
|
2016-11-09 05:00:33 +00:00
|
|
|
|
2016-11-27 20:21:05 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return the unit this state is expressed in."""
|
2020-09-21 16:08:35 +00:00
|
|
|
return self._device.unit_of_measurement()
|
2017-06-27 05:25:54 +00:00
|
|
|
|
2019-07-11 20:01:37 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
2020-09-21 16:08:35 +00:00
|
|
|
device_class = self._device.ha_device_class()
|
|
|
|
if device_class in DEVICE_CLASSES:
|
|
|
|
return device_class
|
2017-09-07 07:11:55 +00:00
|
|
|
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.always_callback
|