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