From fa2e7aa59262a5a65bc095fbcccd54b44c15bae7 Mon Sep 17 00:00:00 2001 From: Carlos Gustavo Sarmiento Date: Mon, 20 Feb 2023 14:57:58 -0600 Subject: [PATCH] Add `state_class = MEASUREMENT` to Derivative sensor (#88408) * Add `state_class = MEASUREMENT` to Derivative sensor One line change per title. * Update sensor.py * Update tests for Derivative Sensor --- homeassistant/components/derivative/sensor.py | 7 ++++++- tests/components/derivative/test_sensor.py | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/derivative/sensor.py b/homeassistant/components/derivative/sensor.py index adf91eb706b..a8cacc0e20d 100644 --- a/homeassistant/components/derivative/sensor.py +++ b/homeassistant/components/derivative/sensor.py @@ -8,7 +8,11 @@ from typing import TYPE_CHECKING 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, @@ -131,6 +135,7 @@ class DerivativeSensor(RestoreEntity, SensorEntity): _attr_icon = ICON _attr_should_poll = False + _attr_state_class = SensorStateClass.MEASUREMENT def __init__( self, diff --git a/tests/components/derivative/test_sensor.py b/tests/components/derivative/test_sensor.py index c1541812d1b..9c2f68fd685 100644 --- a/tests/components/derivative/test_sensor.py +++ b/tests/components/derivative/test_sensor.py @@ -4,6 +4,7 @@ from math import sin import random from unittest.mock import patch +from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass from homeassistant.const import UnitOfPower, UnitOfTime from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component @@ -78,6 +79,7 @@ async def setup_tests(hass, config, times, values, expected_state): assert state is not None assert round(float(state.state), config["sensor"]["round"]) == expected_state + assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT return state