mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
Fix Shelly last_reset (#54101)
This commit is contained in:
parent
64c9f9e1cb
commit
58ccfff067
@ -285,7 +285,6 @@ class ShellyBlockAttributeEntity(ShellyBlockEntity, entity.Entity):
|
|||||||
self._unit: None | str | Callable[[dict], str] = unit
|
self._unit: None | str | Callable[[dict], str] = unit
|
||||||
self._unique_id: str = f"{super().unique_id}-{self.attribute}"
|
self._unique_id: str = f"{super().unique_id}-{self.attribute}"
|
||||||
self._name = get_entity_name(wrapper.device, block, self.description.name)
|
self._name = get_entity_name(wrapper.device, block, self.description.name)
|
||||||
self._last_value: str | None = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
"""Sensor for Shelly."""
|
"""Sensor for Shelly."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
from typing import Final, cast
|
from typing import Final, cast
|
||||||
|
|
||||||
|
import aioshelly
|
||||||
|
|
||||||
from homeassistant.components import sensor
|
from homeassistant.components import sensor
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.helpers.typing import StateType
|
||||||
from homeassistant.util import dt
|
from homeassistant.util import dt
|
||||||
|
|
||||||
|
from . import ShellyDeviceWrapper
|
||||||
from .const import LAST_RESET_NEVER, LAST_RESET_UPTIME, SHAIR_MAX_WORK_HOURS
|
from .const import LAST_RESET_NEVER, LAST_RESET_UPTIME, SHAIR_MAX_WORK_HOURS
|
||||||
from .entity import (
|
from .entity import (
|
||||||
BlockAttributeDescription,
|
BlockAttributeDescription,
|
||||||
@ -35,6 +39,8 @@ from .entity import (
|
|||||||
)
|
)
|
||||||
from .utils import get_device_uptime, temperature_unit
|
from .utils import get_device_uptime, temperature_unit
|
||||||
|
|
||||||
|
_LOGGER: Final = logging.getLogger(__name__)
|
||||||
|
|
||||||
SENSORS: Final = {
|
SENSORS: Final = {
|
||||||
("device", "battery"): BlockAttributeDescription(
|
("device", "battery"): BlockAttributeDescription(
|
||||||
name="Battery",
|
name="Battery",
|
||||||
@ -255,9 +261,39 @@ async def async_setup_entry(
|
|||||||
class ShellySensor(ShellyBlockAttributeEntity, SensorEntity):
|
class ShellySensor(ShellyBlockAttributeEntity, SensorEntity):
|
||||||
"""Represent a shelly sensor."""
|
"""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
|
@property
|
||||||
def state(self) -> StateType:
|
def state(self) -> StateType:
|
||||||
"""Return value of sensor."""
|
"""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
|
return self.attribute_value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -265,20 +301,6 @@ class ShellySensor(ShellyBlockAttributeEntity, SensorEntity):
|
|||||||
"""State class of sensor."""
|
"""State class of sensor."""
|
||||||
return self.description.state_class
|
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
|
@property
|
||||||
def unit_of_measurement(self) -> str | None:
|
def unit_of_measurement(self) -> str | None:
|
||||||
"""Return unit of sensor."""
|
"""Return unit of sensor."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user