Skip to content

Commit

Permalink
Add last reset to Shelly's energy entities (#53710)
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelli74 authored Jul 29, 2021
1 parent fb3b8b6 commit e9a00ad
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions homeassistant/components/shelly/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@
KELVIN_MIN_VALUE_COLOR: Final = 3000

UPTIME_DEVIATION: Final = 5

LAST_RESET_UPTIME: Final = "uptime"
LAST_RESET_NEVER: Final = "never"
4 changes: 2 additions & 2 deletions homeassistant/components/shelly/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import asyncio
from dataclasses import dataclass
from datetime import datetime
import logging
from typing import Any, Callable, Final, cast

Expand Down Expand Up @@ -180,7 +179,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
last_reset: str | None = None


@dataclass
Expand Down Expand Up @@ -286,6 +285,7 @@ def __init__(
self._unit: None | str | Callable[[dict], str] = unit
self._unique_id: str = f"{super().unique_id}-{self.attribute}"
self._name = get_entity_name(wrapper.device, block, self.description.name)
self._last_value: str | None = None

@property
def unique_id(self) -> str:
Expand Down
21 changes: 17 additions & 4 deletions homeassistant/components/shelly/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from homeassistant.helpers.typing import StateType
from homeassistant.util import dt

from .const import SHAIR_MAX_WORK_HOURS
from .const import LAST_RESET_NEVER, LAST_RESET_UPTIME, SHAIR_MAX_WORK_HOURS
from .entity import (
BlockAttributeDescription,
RestAttributeDescription,
Expand Down Expand Up @@ -114,22 +114,23 @@
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,
),
("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=dt.utc_from_timestamp(0),
last_reset=LAST_RESET_NEVER,
),
("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=dt.utc_from_timestamp(0),
last_reset=LAST_RESET_NEVER,
),
("light", "energy"): BlockAttributeDescription(
name="Energy",
Expand All @@ -138,20 +139,23 @@
device_class=sensor.DEVICE_CLASS_ENERGY,
state_class=sensor.STATE_CLASS_MEASUREMENT,
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,
),
("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,
),
("sensor", "concentration"): BlockAttributeDescription(
name="Gas Concentration",
Expand Down Expand Up @@ -264,7 +268,16 @@ def state_class(self) -> str | None:
@property
def last_reset(self) -> datetime | None:
"""State class of sensor."""
return self.description.last_reset
if self.description.last_reset == LAST_RESET_UPTIME:
self._last_value = get_device_uptime(
self.wrapper.device.status, self._last_value
)
return dt.parse_datetime(self._last_value)

if self.description.last_reset == LAST_RESET_NEVER:
return dt.utc_from_timestamp(0)

return None

@property
def unit_of_measurement(self) -> str | None:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/shelly/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def is_momentary_input(settings: dict[str, Any], block: aioshelly.Block) -> bool
return button_type in ["momentary", "momentary_on_release"]


def get_device_uptime(status: dict[str, Any], last_uptime: str) -> str:
def get_device_uptime(status: dict[str, Any], last_uptime: str | None) -> str:
"""Return device uptime string, tolerate up to 5 seconds deviation."""
delta_uptime = utcnow() - timedelta(seconds=status["uptime"])

Expand Down

0 comments on commit e9a00ad

Please sign in to comment.