Fix Shelly last_reset (#54101)

This commit is contained in:
Shay Levy 2021-08-06 05:23:05 +03:00 committed by GitHub
parent 64c9f9e1cb
commit 58ccfff067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 16 deletions

View File

@ -285,7 +285,6 @@ class ShellyBlockAttributeEntity(ShellyBlockEntity, entity.Entity):
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:

View File

@ -1,9 +1,12 @@
"""Sensor for Shelly."""
from __future__ import annotations
from datetime import datetime
from datetime import timedelta
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
@ -23,6 +26,7 @@ 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 .entity import (
BlockAttributeDescription,
@ -35,6 +39,8 @@ from .entity import (
)
from .utils import get_device_uptime, temperature_unit
_LOGGER: Final = logging.getLogger(__name__)
SENSORS: Final = {
("device", "battery"): BlockAttributeDescription(
name="Battery",
@ -255,9 +261,39 @@ 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)
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 state(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
@ -265,20 +301,6 @@ class ShellySensor(ShellyBlockAttributeEntity, SensorEntity):
"""State class of sensor."""
return self.description.state_class
@property
def last_reset(self) -> datetime | None:
"""State class of sensor."""
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:
"""Return unit of sensor."""