From c6b835dd91620484f8a4b4c6593f83f7da3850b7 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Tue, 7 Jun 2022 17:02:12 +0200 Subject: [PATCH] Add missing `state_class` to min_max sensors (#73169) Add missing state_class --- homeassistant/components/min_max/sensor.py | 11 ++++++++++- tests/components/min_max/test_sensor.py | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/min_max/sensor.py b/homeassistant/components/min_max/sensor.py index 5c117357729..99aec4e9e7b 100644 --- a/homeassistant/components/min_max/sensor.py +++ b/homeassistant/components/min_max/sensor.py @@ -6,7 +6,11 @@ import statistics import voluptuous as vol -from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity +from homeassistant.components.sensor import ( + PLATFORM_SCHEMA, + SensorEntity, + SensorStateClass, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, @@ -232,6 +236,11 @@ class MinMaxSensor(SensorEntity): """Return the icon to use in the frontend, if any.""" return ICON + @property + def state_class(self) -> SensorStateClass: + """Return the state class.""" + return SensorStateClass.MEASUREMENT + @callback def _async_min_max_sensor_state_listener(self, event, update_state=True): """Handle the sensor state changes.""" diff --git a/tests/components/min_max/test_sensor.py b/tests/components/min_max/test_sensor.py index e143b26e47f..72728ac20b6 100644 --- a/tests/components/min_max/test_sensor.py +++ b/tests/components/min_max/test_sensor.py @@ -4,6 +4,7 @@ from unittest.mock import patch from homeassistant import config as hass_config from homeassistant.components.min_max.const import DOMAIN +from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, @@ -76,6 +77,7 @@ async def test_min_sensor(hass): assert str(float(MIN_VALUE)) == state.state assert entity_ids[2] == state.attributes.get("min_entity_id") + assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT async def test_max_sensor(hass): @@ -102,6 +104,7 @@ async def test_max_sensor(hass): assert str(float(MAX_VALUE)) == state.state assert entity_ids[1] == state.attributes.get("max_entity_id") + assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT async def test_mean_sensor(hass): @@ -127,6 +130,7 @@ async def test_mean_sensor(hass): state = hass.states.get("sensor.test_mean") assert str(float(MEAN)) == state.state + assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT async def test_mean_1_digit_sensor(hass): @@ -204,6 +208,7 @@ async def test_median_sensor(hass): state = hass.states.get("sensor.test_median") assert str(float(MEDIAN)) == state.state + assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT async def test_not_enough_sensor_value(hass): @@ -327,6 +332,7 @@ async def test_last_sensor(hass): state = hass.states.get("sensor.test_last") assert str(float(value)) == state.state assert entity_id == state.attributes.get("last_entity_id") + assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT async def test_reload(hass):