Set state class to total for Integration sensors (#55872)

This commit is contained in:
Erik Montnemery 2021-09-07 00:27:31 +02:00 committed by GitHub
parent 6895081595
commit b1dbdec2ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View file

@ -8,7 +8,7 @@ from homeassistant.components.sensor import (
DEVICE_CLASS_ENERGY, DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER, DEVICE_CLASS_POWER,
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
STATE_CLASS_TOTAL_INCREASING, STATE_CLASS_TOTAL,
SensorEntity, SensorEntity,
) )
from homeassistant.const import ( from homeassistant.const import (
@ -116,7 +116,7 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
self._unit_of_measurement = unit_of_measurement self._unit_of_measurement = unit_of_measurement
self._unit_prefix = UNIT_PREFIXES[unit_prefix] self._unit_prefix = UNIT_PREFIXES[unit_prefix]
self._unit_time = UNIT_TIME[unit_time] self._unit_time = UNIT_TIME[unit_time]
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING self._attr_state_class = STATE_CLASS_TOTAL
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Handle entity which will be added.""" """Handle entity which will be added."""

View file

@ -2,7 +2,7 @@
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch from unittest.mock import patch
from homeassistant.components.sensor import STATE_CLASS_TOTAL_INCREASING from homeassistant.components.sensor import STATE_CLASS_TOTAL
from homeassistant.const import ( from homeassistant.const import (
DEVICE_CLASS_ENERGY, DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER, DEVICE_CLASS_POWER,
@ -39,7 +39,7 @@ async def test_state(hass) -> None:
state = hass.states.get("sensor.integration") state = hass.states.get("sensor.integration")
assert state is not None assert state is not None
assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING assert state.attributes.get("state_class") == STATE_CLASS_TOTAL
assert "device_class" not in state.attributes assert "device_class" not in state.attributes
future_now = dt_util.utcnow() + timedelta(seconds=3600) future_now = dt_util.utcnow() + timedelta(seconds=3600)
@ -57,7 +57,7 @@ async def test_state(hass) -> None:
assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR
assert state.attributes.get("device_class") == DEVICE_CLASS_ENERGY assert state.attributes.get("device_class") == DEVICE_CLASS_ENERGY
assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING assert state.attributes.get("state_class") == STATE_CLASS_TOTAL
async def test_restore_state(hass: HomeAssistant) -> None: async def test_restore_state(hass: HomeAssistant) -> None:
@ -104,7 +104,9 @@ async def test_restore_state_failed(hass: HomeAssistant) -> None:
State( State(
"sensor.integration", "sensor.integration",
"INVALID", "INVALID",
{}, {
"last_reset": "2019-10-06T21:00:00.000000",
},
), ),
), ),
) )
@ -125,7 +127,7 @@ async def test_restore_state_failed(hass: HomeAssistant) -> None:
assert state assert state
assert state.state == "0" assert state.state == "0"
assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR
assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING assert state.attributes.get("state_class") == STATE_CLASS_TOTAL
assert "device_class" not in state.attributes assert "device_class" not in state.attributes