From 98877924d390848d4a8a5901670f7aa6b9e1d55f Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Thu, 5 Aug 2021 19:11:01 +0200 Subject: [PATCH] Add `state_class` for KNX sensors (#53996) --- homeassistant/components/knx/schema.py | 3 +++ homeassistant/components/knx/sensor.py | 1 + 2 files changed, 4 insertions(+) diff --git a/homeassistant/components/knx/schema.py b/homeassistant/components/knx/schema.py index 11b2504d129..196c171c9b5 100644 --- a/homeassistant/components/knx/schema.py +++ b/homeassistant/components/knx/schema.py @@ -17,6 +17,7 @@ from homeassistant.components.binary_sensor import ( DEVICE_CLASSES as BINARY_SENSOR_DEVICE_CLASSES, ) from homeassistant.components.cover import DEVICE_CLASSES as COVER_DEVICE_CLASSES +from homeassistant.components.sensor import STATE_CLASSES_SCHEMA from homeassistant.const import ( CONF_DEVICE_CLASS, CONF_ENTITY_ID, @@ -724,6 +725,7 @@ class SensorSchema(KNXPlatformSchema): CONF_ALWAYS_CALLBACK = "always_callback" CONF_STATE_ADDRESS = CONF_STATE_ADDRESS + CONF_STATE_CLASS = "state_class" CONF_SYNC_STATE = CONF_SYNC_STATE DEFAULT_NAME = "KNX Sensor" @@ -732,6 +734,7 @@ class SensorSchema(KNXPlatformSchema): vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_SYNC_STATE, default=True): sync_state_validator, vol.Optional(CONF_ALWAYS_CALLBACK, default=False): cv.boolean, + vol.Optional(CONF_STATE_CLASS): STATE_CLASSES_SCHEMA, vol.Required(CONF_TYPE): sensor_type_validator, vol.Required(CONF_STATE_ADDRESS): ga_list_validator, } diff --git a/homeassistant/components/knx/sensor.py b/homeassistant/components/knx/sensor.py index e095b2aee47..5fee8446e91 100644 --- a/homeassistant/components/knx/sensor.py +++ b/homeassistant/components/knx/sensor.py @@ -63,6 +63,7 @@ class KNXSensor(KnxEntity, SensorEntity): self._attr_force_update = self._device.always_callback self._attr_unique_id = str(self._device.sensor_value.group_address_state) self._attr_unit_of_measurement = self._device.unit_of_measurement() + self._attr_state_class = config.get(SensorSchema.CONF_STATE_CLASS) @property def state(self) -> StateType: