2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP sensors."""
|
2021-03-19 09:22:18 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-11-29 19:29:47 +00:00
|
|
|
from contextlib import suppress
|
2021-05-04 12:50:06 +00:00
|
|
|
from typing import Any
|
2021-03-19 09:22:18 +00:00
|
|
|
|
2021-05-25 18:16:54 +00:00
|
|
|
from xknx import XKNX
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx.devices import Sensor as XknxSensor
|
2016-09-14 01:21:43 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
from homeassistant import config_entries
|
2021-08-27 03:54:50 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
CONF_STATE_CLASS,
|
2022-11-29 19:29:47 +00:00
|
|
|
SensorDeviceClass,
|
2021-08-27 03:54:50 +00:00
|
|
|
SensorEntity,
|
|
|
|
)
|
2022-11-01 13:30:42 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_DEVICE_CLASS,
|
|
|
|
CONF_ENTITY_CATEGORY,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_TYPE,
|
|
|
|
Platform,
|
|
|
|
)
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 12:50:06 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-11-20 10:30:41 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, StateType
|
2016-09-14 01:21:43 +00:00
|
|
|
|
2021-12-03 17:29:38 +00:00
|
|
|
from .const import ATTR_SOURCE, DATA_KNX_CONFIG, DOMAIN
|
2020-09-21 16:08:35 +00:00
|
|
|
from .knx_entity import KnxEntity
|
2021-05-25 18:16:54 +00:00
|
|
|
from .schema import SensorSchema
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-06-27 05:25:54 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
async def async_setup_entry(
|
2021-03-27 21:20:11 +00:00
|
|
|
hass: HomeAssistant,
|
2021-11-20 10:30:41 +00:00
|
|
|
config_entry: config_entries.ConfigEntry,
|
2021-05-04 12:50:06 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-03-19 09:22:18 +00:00
|
|
|
) -> None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Set up sensor(s) for KNX platform."""
|
2021-05-25 18:16:54 +00:00
|
|
|
xknx: XKNX = hass.data[DOMAIN].xknx
|
2021-12-03 17:29:38 +00:00
|
|
|
config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.SENSOR]
|
2021-05-25 18:16:54 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
async_add_entities(KNXSensor(xknx, entity_config) for entity_config in config)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
|
2021-05-25 18:16:54 +00:00
|
|
|
def _create_sensor(xknx: XKNX, config: ConfigType) -> XknxSensor:
|
|
|
|
"""Return a KNX sensor to be used within XKNX."""
|
|
|
|
return XknxSensor(
|
|
|
|
xknx,
|
|
|
|
name=config[CONF_NAME],
|
|
|
|
group_address_state=config[SensorSchema.CONF_STATE_ADDRESS],
|
|
|
|
sync_state=config[SensorSchema.CONF_SYNC_STATE],
|
|
|
|
always_callback=config[SensorSchema.CONF_ALWAYS_CALLBACK],
|
|
|
|
value_type=config[CONF_TYPE],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class KNXSensor(KnxEntity, SensorEntity):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Representation of a KNX sensor."""
|
|
|
|
|
2021-06-26 12:30:36 +00:00
|
|
|
_device: XknxSensor
|
|
|
|
|
2021-05-25 18:16:54 +00:00
|
|
|
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of a KNX sensor."""
|
2021-05-25 18:16:54 +00:00
|
|
|
super().__init__(_create_sensor(xknx, config))
|
2022-11-01 13:30:42 +00:00
|
|
|
if device_class := config.get(CONF_DEVICE_CLASS):
|
|
|
|
self._attr_device_class = device_class
|
|
|
|
else:
|
2022-11-29 19:29:47 +00:00
|
|
|
with suppress(ValueError):
|
|
|
|
self._attr_device_class = SensorDeviceClass(
|
|
|
|
str(self._device.ha_device_class())
|
|
|
|
)
|
2021-06-26 12:30:36 +00:00
|
|
|
self._attr_force_update = self._device.always_callback
|
2021-10-25 03:12:26 +00:00
|
|
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
2021-06-26 12:30:36 +00:00
|
|
|
self._attr_unique_id = str(self._device.sensor_value.group_address_state)
|
2021-08-11 19:17:47 +00:00
|
|
|
self._attr_native_unit_of_measurement = self._device.unit_of_measurement()
|
2021-08-27 03:54:50 +00:00
|
|
|
self._attr_state_class = config.get(CONF_STATE_CLASS)
|
2016-09-14 01:21:43 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:47 +00:00
|
|
|
def native_value(self) -> StateType:
|
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
|
|
|
|
2021-04-10 16:12:43 +00: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)
|
|
|
|
return attr
|