mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Remove last_reset attribute and set state class to total_increasing for Integration sensors (#54815)
This commit is contained in:
parent
07c0fc9eba
commit
27849426fe
@ -5,11 +5,10 @@ import logging
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
ATTR_LAST_RESET,
|
|
||||||
DEVICE_CLASS_ENERGY,
|
DEVICE_CLASS_ENERGY,
|
||||||
DEVICE_CLASS_POWER,
|
DEVICE_CLASS_POWER,
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_TOTAL_INCREASING,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -28,7 +27,6 @@ from homeassistant.core import callback
|
|||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
from homeassistant.util import dt as dt_util
|
|
||||||
|
|
||||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
# 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_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_MEASUREMENT
|
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING
|
||||||
|
|
||||||
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."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
state = await self.async_get_last_state()
|
state = await self.async_get_last_state()
|
||||||
self._attr_last_reset = dt_util.utcnow()
|
|
||||||
if state:
|
if state:
|
||||||
try:
|
try:
|
||||||
self._state = Decimal(state.state)
|
self._state = Decimal(state.state)
|
||||||
except (DecimalException, ValueError) as err:
|
except (DecimalException, ValueError) as err:
|
||||||
_LOGGER.warning("Could not restore last state: %s", err)
|
_LOGGER.warning("Could not restore last state: %s", err)
|
||||||
else:
|
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._attr_device_class = state.attributes.get(ATTR_DEVICE_CLASS)
|
||||||
|
|
||||||
self._unit_of_measurement = state.attributes.get(
|
self._unit_of_measurement = state.attributes.get(
|
||||||
|
@ -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_MEASUREMENT
|
from homeassistant.components.sensor import STATE_CLASS_TOTAL_INCREASING
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
DEVICE_CLASS_ENERGY,
|
DEVICE_CLASS_ENERGY,
|
||||||
DEVICE_CLASS_POWER,
|
DEVICE_CLASS_POWER,
|
||||||
@ -39,8 +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("last_reset") == now.isoformat()
|
assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING
|
||||||
assert state.attributes.get("state_class") == STATE_CLASS_MEASUREMENT
|
|
||||||
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)
|
||||||
@ -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("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_MEASUREMENT
|
assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING
|
||||||
assert state.attributes.get("last_reset") == now.isoformat()
|
|
||||||
|
|
||||||
|
|
||||||
async def test_restore_state(hass: HomeAssistant) -> None:
|
async def test_restore_state(hass: HomeAssistant) -> None:
|
||||||
@ -71,7 +69,6 @@ async def test_restore_state(hass: HomeAssistant) -> None:
|
|||||||
"sensor.integration",
|
"sensor.integration",
|
||||||
"100.0",
|
"100.0",
|
||||||
{
|
{
|
||||||
"last_reset": "2019-10-06T21:00:00",
|
|
||||||
"device_class": DEVICE_CLASS_ENERGY,
|
"device_class": DEVICE_CLASS_ENERGY,
|
||||||
"unit_of_measurement": ENERGY_KILO_WATT_HOUR,
|
"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.state == "100.00"
|
||||||
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("last_reset") == "2019-10-06T21:00:00"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_restore_state_failed(hass: HomeAssistant) -> None:
|
async def test_restore_state_failed(hass: HomeAssistant) -> None:
|
||||||
@ -108,9 +104,7 @@ 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",
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@ -131,8 +125,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_MEASUREMENT
|
assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING
|
||||||
assert state.attributes.get("last_reset") != "2019-10-06T21:00:00"
|
|
||||||
assert "device_class" not in state.attributes
|
assert "device_class" not in state.attributes
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user