2019-02-14 05:35:12 +01:00
|
|
|
"""Support for KNX/IP sensors."""
|
2021-03-19 10:22:18 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-20 17:40:41 +02:00
|
|
|
from collections.abc import Iterable
|
|
|
|
from typing import Any, Callable
|
2021-03-19 10:22:18 +01:00
|
|
|
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx.devices import Sensor as XknxSensor
|
2016-09-14 03:21:43 +02:00
|
|
|
|
2021-03-22 19:59:03 +01:00
|
|
|
from homeassistant.components.sensor import DEVICE_CLASSES, SensorEntity
|
2021-03-27 22:20:11 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-01-21 07:35:38 +01:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2021-03-27 22:20:11 +01:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
|
2021-04-10 18:12:43 +02:00
|
|
|
from homeassistant.util import dt
|
2016-09-14 03:21:43 +02:00
|
|
|
|
2021-04-10 18:12:43 +02:00
|
|
|
from .const import ATTR_LAST_KNX_UPDATE, ATTR_SOURCE, DOMAIN
|
2020-09-21 18:08:35 +02:00
|
|
|
from .knx_entity import KnxEntity
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2017-06-27 14:25:54 +09:00
|
|
|
|
2021-03-19 10:22:18 +01:00
|
|
|
async def async_setup_platform(
|
2021-03-27 22:20:11 +01:00
|
|
|
hass: HomeAssistant,
|
2021-03-19 10:22:18 +01:00
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: Callable[[Iterable[Entity]], None],
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-09-07 09:11:55 +02:00
|
|
|
"""Set up sensor(s) for KNX platform."""
|
|
|
|
entities = []
|
2020-09-21 18:08:35 +02:00
|
|
|
for device in hass.data[DOMAIN].xknx.devices:
|
2020-08-30 20:13:47 +02:00
|
|
|
if isinstance(device, XknxSensor):
|
|
|
|
entities.append(KNXSensor(device))
|
2018-08-24 16:37:30 +02:00
|
|
|
async_add_entities(entities)
|
2017-09-07 09:11:55 +02:00
|
|
|
|
|
|
|
|
2021-03-22 19:59:03 +01:00
|
|
|
class KNXSensor(KnxEntity, SensorEntity):
|
2017-09-07 09:11:55 +02:00
|
|
|
"""Representation of a KNX sensor."""
|
|
|
|
|
2021-03-19 10:22:18 +01:00
|
|
|
def __init__(self, device: XknxSensor) -> None:
|
2018-01-21 07:35:38 +01:00
|
|
|
"""Initialize of a KNX sensor."""
|
2021-03-19 10:22:18 +01:00
|
|
|
self._device: XknxSensor
|
2020-09-21 18:08:35 +02:00
|
|
|
super().__init__(device)
|
2021-04-28 15:50:01 +02:00
|
|
|
self._unique_id = f"{self._device.sensor_value.group_address_state}"
|
2016-09-14 03:21:43 +02:00
|
|
|
|
|
|
|
@property
|
2021-03-19 10:22:18 +01:00
|
|
|
def state(self) -> StateType:
|
2017-09-07 09:11:55 +02:00
|
|
|
"""Return the state of the sensor."""
|
2020-09-21 18:08:35 +02:00
|
|
|
return self._device.resolve_state()
|
2016-11-08 21:00:33 -08:00
|
|
|
|
2016-11-27 21:21:05 +01:00
|
|
|
@property
|
2021-03-19 10:22:18 +01:00
|
|
|
def unit_of_measurement(self) -> str | None:
|
2017-09-07 09:11:55 +02:00
|
|
|
"""Return the unit this state is expressed in."""
|
2020-09-21 18:08:35 +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
|
2021-03-19 10:22:18 +01:00
|
|
|
def device_class(self) -> str | None:
|
2019-07-11 22:01:37 +02:00
|
|
|
"""Return the device class of the sensor."""
|
2020-09-21 18:08:35 +02:00
|
|
|
device_class = self._device.ha_device_class()
|
|
|
|
if device_class in DEVICE_CLASSES:
|
|
|
|
return device_class
|
2017-09-07 09:11:55 +02:00
|
|
|
return None
|
2020-11-13 09:37:45 +01:00
|
|
|
|
2021-04-10 18:12:43 +02:00
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
|
|
|
"""Return device specific state attributes."""
|
|
|
|
attr: dict[str, Any] = {}
|
|
|
|
|
|
|
|
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 09:37:45 +01: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
|