From bbd1a85b0947d08be3dccaf413cb3fdca8efdca9 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 28 Jul 2021 15:48:27 -0700 Subject: [PATCH] Add last reset to Shelly (#53654) --- homeassistant/components/shelly/entity.py | 2 ++ homeassistant/components/shelly/sensor.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 743dd07414e..c8b23f71bd7 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio from dataclasses import dataclass +from datetime import datetime import logging from typing import Any, Callable, Final, cast @@ -179,6 +180,7 @@ 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: datetime | None = None @dataclass diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 96ff6e55f8d..7c2ffdbc470 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -1,6 +1,7 @@ """Sensor for Shelly.""" from __future__ import annotations +from datetime import datetime from typing import Final, cast from homeassistant.components import sensor @@ -20,6 +21,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType +from homeassistant.util import dt from .const import SHAIR_MAX_WORK_HOURS from .entity import ( @@ -119,6 +121,7 @@ SENSORS: Final = { value=lambda value: round(value / 1000, 2), device_class=sensor.DEVICE_CLASS_ENERGY, state_class=sensor.STATE_CLASS_MEASUREMENT, + last_reset=dt.utc_from_timestamp(0), ), ("emeter", "energyReturned"): BlockAttributeDescription( name="Energy Returned", @@ -126,6 +129,7 @@ SENSORS: Final = { value=lambda value: round(value / 1000, 2), device_class=sensor.DEVICE_CLASS_ENERGY, state_class=sensor.STATE_CLASS_MEASUREMENT, + last_reset=dt.utc_from_timestamp(0), ), ("light", "energy"): BlockAttributeDescription( name="Energy", @@ -257,6 +261,11 @@ class ShellySensor(ShellyBlockAttributeEntity, SensorEntity): """State class of sensor.""" return self.description.state_class + @property + def last_reset(self) -> datetime | None: + """State class of sensor.""" + return self.description.last_reset + @property def unit_of_measurement(self) -> str | None: """Return unit of sensor."""