From f7f2ea8226bd2e9fbc4e842acbf00b6382d3f11d Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 18 Aug 2021 11:23:05 +0200 Subject: [PATCH 1/2] Remove last_reset attribute from shelly energy sensors --- homeassistant/components/shelly/const.py | 3 -- homeassistant/components/shelly/entity.py | 1 - homeassistant/components/shelly/sensor.py | 42 ++++------------------- 3 files changed, 7 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/shelly/const.py b/homeassistant/components/shelly/const.py index ea6b9320cb153b..5646086285d41f 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -109,6 +109,3 @@ KELVIN_MIN_VALUE_COLOR: Final = 3000 UPTIME_DEVIATION: Final = 5 - -LAST_RESET_UPTIME: Final = "uptime" -LAST_RESET_NEVER: Final = "never" diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 0d23f5abffc26b..743dd07414ec3c 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -179,7 +179,6 @@ class BlockAttributeDescription: # Callable (settings, block), return true if entity should be removed removal_condition: Callable[[dict, aioshelly.Block], bool] | None = None extra_state_attributes: Callable[[aioshelly.Block], dict | None] | None = None - last_reset: str | None = None @dataclass diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index e3af10571d594b..935a3b1620646f 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -1,7 +1,6 @@ """Sensor for Shelly.""" from __future__ import annotations -from datetime import timedelta import logging from typing import Final, cast @@ -24,10 +23,9 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType -from homeassistant.util import dt from . import ShellyDeviceWrapper -from .const import LAST_RESET_NEVER, LAST_RESET_UPTIME, SHAIR_MAX_WORK_HOURS +from .const import SHAIR_MAX_WORK_HOURS from .entity import ( BlockAttributeDescription, RestAttributeDescription, @@ -119,49 +117,43 @@ unit=ENERGY_KILO_WATT_HOUR, value=lambda value: round(value / 60 / 1000, 2), device_class=sensor.DEVICE_CLASS_ENERGY, - state_class=sensor.STATE_CLASS_MEASUREMENT, - last_reset=LAST_RESET_UPTIME, + state_class=sensor.STATE_CLASS_TOTAL_INCREASING, ), ("emeter", "energy"): BlockAttributeDescription( name="Energy", unit=ENERGY_KILO_WATT_HOUR, value=lambda value: round(value / 1000, 2), device_class=sensor.DEVICE_CLASS_ENERGY, - state_class=sensor.STATE_CLASS_MEASUREMENT, - last_reset=LAST_RESET_NEVER, + state_class=sensor.STATE_CLASS_TOTAL_INCREASING, ), ("emeter", "energyReturned"): BlockAttributeDescription( name="Energy Returned", unit=ENERGY_KILO_WATT_HOUR, value=lambda value: round(value / 1000, 2), device_class=sensor.DEVICE_CLASS_ENERGY, - state_class=sensor.STATE_CLASS_MEASUREMENT, - last_reset=LAST_RESET_NEVER, + state_class=sensor.STATE_CLASS_TOTAL_INCREASING, ), ("light", "energy"): BlockAttributeDescription( name="Energy", unit=ENERGY_KILO_WATT_HOUR, value=lambda value: round(value / 60 / 1000, 2), device_class=sensor.DEVICE_CLASS_ENERGY, - state_class=sensor.STATE_CLASS_MEASUREMENT, + state_class=sensor.STATE_CLASS_TOTAL_INCREASING, default_enabled=False, - last_reset=LAST_RESET_UPTIME, ), ("relay", "energy"): BlockAttributeDescription( name="Energy", unit=ENERGY_KILO_WATT_HOUR, value=lambda value: round(value / 60 / 1000, 2), device_class=sensor.DEVICE_CLASS_ENERGY, - state_class=sensor.STATE_CLASS_MEASUREMENT, - last_reset=LAST_RESET_UPTIME, + state_class=sensor.STATE_CLASS_TOTAL_INCREASING, ), ("roller", "rollerEnergy"): BlockAttributeDescription( name="Energy", unit=ENERGY_KILO_WATT_HOUR, value=lambda value: round(value / 60 / 1000, 2), device_class=sensor.DEVICE_CLASS_ENERGY, - state_class=sensor.STATE_CLASS_MEASUREMENT, - last_reset=LAST_RESET_UPTIME, + state_class=sensor.STATE_CLASS_TOTAL_INCREASING, ), ("sensor", "concentration"): BlockAttributeDescription( name="Gas Concentration", @@ -270,30 +262,10 @@ def __init__( ) -> None: """Initialize sensor.""" super().__init__(wrapper, block, attribute, description) - self._last_value: float | None = None - - if description.last_reset == LAST_RESET_NEVER: - self._attr_last_reset = dt.utc_from_timestamp(0) - elif description.last_reset == LAST_RESET_UPTIME: - self._attr_last_reset = ( - dt.utcnow() - timedelta(seconds=wrapper.device.status["uptime"]) - ).replace(second=0, microsecond=0) @property def native_value(self) -> StateType: """Return value of sensor.""" - if ( - self.description.last_reset == LAST_RESET_UPTIME - and self.attribute_value is not None - ): - value = cast(float, self.attribute_value) - - if self._last_value and self._last_value > value: - self._attr_last_reset = dt.utcnow().replace(second=0, microsecond=0) - _LOGGER.info("Energy reset detected for entity %s", self.name) - - self._last_value = value - return self.attribute_value @property From 16c3bb0b85d76fb4f042ab443f517bd2a235ff24 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 18 Aug 2021 11:46:43 +0200 Subject: [PATCH 2/2] Remove useless __init__ in subclass --- homeassistant/components/shelly/sensor.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 935a3b1620646f..13cf56d3b3d3b9 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -1,11 +1,8 @@ """Sensor for Shelly.""" from __future__ import annotations -import logging from typing import Final, cast -import aioshelly - from homeassistant.components import sensor from homeassistant.components.sensor import SensorEntity from homeassistant.config_entries import ConfigEntry @@ -24,7 +21,6 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType -from . import ShellyDeviceWrapper from .const import SHAIR_MAX_WORK_HOURS from .entity import ( BlockAttributeDescription, @@ -37,8 +33,6 @@ ) from .utils import get_device_uptime, temperature_unit -_LOGGER: Final = logging.getLogger(__name__) - SENSORS: Final = { ("device", "battery"): BlockAttributeDescription( name="Battery", @@ -253,16 +247,6 @@ async def async_setup_entry( class ShellySensor(ShellyBlockAttributeEntity, SensorEntity): """Represent a shelly sensor.""" - def __init__( - self, - wrapper: ShellyDeviceWrapper, - block: aioshelly.Block, - attribute: str, - description: BlockAttributeDescription, - ) -> None: - """Initialize sensor.""" - super().__init__(wrapper, block, attribute, description) - @property def native_value(self) -> StateType: """Return value of sensor."""