From 27849426fe37094516fe78b138a81fa32695e9ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= <mail@dahoiv.net> Date: Wed, 18 Aug 2021 15:54:11 +0200 Subject: [PATCH] Remove last_reset attribute and set state class to total_increasing for Integration sensors (#54815) --- homeassistant/components/integration/sensor.py | 13 ++----------- tests/components/integration/test_sensor.py | 17 +++++------------ 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/integration/sensor.py b/homeassistant/components/integration/sensor.py index cabcb2fd394..cf91fd46dad 100644 --- a/homeassistant/components/integration/sensor.py +++ b/homeassistant/components/integration/sensor.py @@ -5,11 +5,10 @@ import logging import voluptuous as vol from homeassistant.components.sensor import ( - ATTR_LAST_RESET, DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, PLATFORM_SCHEMA, - STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, SensorEntity, ) from homeassistant.const import ( @@ -28,7 +27,6 @@ from homeassistant.core import callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.restore_state import RestoreEntity -from homeassistant.util import dt as dt_util # mypy: allow-untyped-defs, no-check-untyped-defs @@ -124,25 +122,18 @@ class IntegrationSensor(RestoreEntity, SensorEntity): self._unit_prefix = UNIT_PREFIXES[unit_prefix] self._unit_time = UNIT_TIME[unit_time] - self._attr_state_class = STATE_CLASS_MEASUREMENT + self._attr_state_class = STATE_CLASS_TOTAL_INCREASING async def async_added_to_hass(self): """Handle entity which will be added.""" await super().async_added_to_hass() state = await self.async_get_last_state() - self._attr_last_reset = dt_util.utcnow() if state: try: self._state = Decimal(state.state) except (DecimalException, ValueError) as err: _LOGGER.warning("Could not restore last state: %s", err) else: - last_reset = dt_util.parse_datetime( - state.attributes.get(ATTR_LAST_RESET, "") - ) - self._attr_last_reset = ( - last_reset if last_reset else dt_util.utc_from_timestamp(0) - ) self._attr_device_class = state.attributes.get(ATTR_DEVICE_CLASS) self._unit_of_measurement = state.attributes.get( diff --git a/tests/components/integration/test_sensor.py b/tests/components/integration/test_sensor.py index 36d3d4b3b30..e8aaf906936 100644 --- a/tests/components/integration/test_sensor.py +++ b/tests/components/integration/test_sensor.py @@ -2,7 +2,7 @@ from datetime import timedelta from unittest.mock import patch -from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT +from homeassistant.components.sensor import STATE_CLASS_TOTAL_INCREASING from homeassistant.const import ( DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, @@ -39,8 +39,7 @@ async def test_state(hass) -> None: state = hass.states.get("sensor.integration") assert state is not None - assert state.attributes.get("last_reset") == now.isoformat() - assert state.attributes.get("state_class") == STATE_CLASS_MEASUREMENT + assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING assert "device_class" not in state.attributes future_now = dt_util.utcnow() + timedelta(seconds=3600) @@ -58,8 +57,7 @@ async def test_state(hass) -> None: assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR assert state.attributes.get("device_class") == DEVICE_CLASS_ENERGY - assert state.attributes.get("state_class") == STATE_CLASS_MEASUREMENT - assert state.attributes.get("last_reset") == now.isoformat() + assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING async def test_restore_state(hass: HomeAssistant) -> None: @@ -71,7 +69,6 @@ async def test_restore_state(hass: HomeAssistant) -> None: "sensor.integration", "100.0", { - "last_reset": "2019-10-06T21:00:00", "device_class": DEVICE_CLASS_ENERGY, "unit_of_measurement": ENERGY_KILO_WATT_HOUR, }, @@ -97,7 +94,6 @@ async def test_restore_state(hass: HomeAssistant) -> None: assert state.state == "100.00" assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR assert state.attributes.get("device_class") == DEVICE_CLASS_ENERGY - assert state.attributes.get("last_reset") == "2019-10-06T21:00:00" async def test_restore_state_failed(hass: HomeAssistant) -> None: @@ -108,9 +104,7 @@ async def test_restore_state_failed(hass: HomeAssistant) -> None: State( "sensor.integration", "INVALID", - { - "last_reset": "2019-10-06T21:00:00.000000", - }, + {}, ), ), ) @@ -131,8 +125,7 @@ async def test_restore_state_failed(hass: HomeAssistant) -> None: assert state assert state.state == "0" assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR - assert state.attributes.get("state_class") == STATE_CLASS_MEASUREMENT - assert state.attributes.get("last_reset") != "2019-10-06T21:00:00" + assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING assert "device_class" not in state.attributes -- GitLab