Use SensorDeviceClass enum in KNX (#82947)

This commit is contained in:
Franck Nijhof
2022-11-29 20:29:47 +01:00
committed by GitHub
parent c36dd17780
commit b4e30c4033

View File

@@ -1,6 +1,7 @@
"""Support for KNX/IP sensors.""" """Support for KNX/IP sensors."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from typing import Any from typing import Any
from xknx import XKNX from xknx import XKNX
@@ -9,7 +10,7 @@ from xknx.devices import Sensor as XknxSensor
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
CONF_STATE_CLASS, CONF_STATE_CLASS,
DEVICE_CLASSES, SensorDeviceClass,
SensorEntity, SensorEntity,
) )
from homeassistant.const import ( from homeassistant.const import (
@@ -63,11 +64,10 @@ class KNXSensor(KnxEntity, SensorEntity):
if device_class := config.get(CONF_DEVICE_CLASS): if device_class := config.get(CONF_DEVICE_CLASS):
self._attr_device_class = device_class self._attr_device_class = device_class
else: else:
self._attr_device_class = ( with suppress(ValueError):
self._device.ha_device_class() self._attr_device_class = SensorDeviceClass(
if self._device.ha_device_class() in DEVICE_CLASSES str(self._device.ha_device_class())
else None )
)
self._attr_force_update = self._device.always_callback self._attr_force_update = self._device.always_callback
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY) self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
self._attr_unique_id = str(self._device.sensor_value.group_address_state) self._attr_unique_id = str(self._device.sensor_value.group_address_state)