Remove last_reset attribute and set state class to total_increasing for Integration sensors (#54815)

This commit is contained in:
Daniel Hjelseth Høyer 2021-08-18 15:54:11 +02:00 committed by GitHub
parent 07c0fc9eba
commit 27849426fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 23 deletions

View file

@ -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(

View file

@ -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