Add statistics support for the PVOutput sensor (#54149)
parent
483a4535c8
commit
ddbd455827
homeassistant/components/pvoutput
|
@ -1,7 +1,10 @@
|
|||
"""Support for getting collected information from PVOutput."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import namedtuple
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
|
@ -9,6 +12,7 @@ from homeassistant.components.rest.data import RestData
|
|||
from homeassistant.components.sensor import (
|
||||
DEVICE_CLASS_ENERGY,
|
||||
PLATFORM_SCHEMA,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
SensorEntity,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
|
@ -22,6 +26,8 @@ from homeassistant.const import (
|
|||
)
|
||||
from homeassistant.core import callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
_ENDPOINT = "http://pvoutput.org/service/r2/getstatus.jsp"
|
||||
|
@ -68,12 +74,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||
async_add_entities([PvoutputSensor(rest, name)])
|
||||
|
||||
|
||||
class PvoutputSensor(SensorEntity):
|
||||
class PvoutputSensor(SensorEntity, RestoreEntity):
|
||||
"""Representation of a PVOutput sensor."""
|
||||
|
||||
_attr_state_class = STATE_CLASS_MEASUREMENT
|
||||
_attr_device_class = DEVICE_CLASS_ENERGY
|
||||
_attr_unit_of_measurement = ENERGY_WATT_HOUR
|
||||
|
||||
_old_state: int | None = None
|
||||
|
||||
def __init__(self, rest, name):
|
||||
"""Initialize a PVOutput sensor."""
|
||||
self.rest = rest
|
||||
|
@ -120,8 +129,37 @@ class PvoutputSensor(SensorEntity):
|
|||
await self.rest.async_update()
|
||||
self._async_update_from_rest_data()
|
||||
|
||||
new_state: int | None = None
|
||||
state = cast("str | None", self.state)
|
||||
if state is not None:
|
||||
new_state = int(state)
|
||||
|
||||
did_reset = False
|
||||
if new_state is None:
|
||||
did_reset = False
|
||||
elif self._old_state is None:
|
||||
did_reset = True
|
||||
elif new_state == 0:
|
||||
did_reset = self._old_state != 0
|
||||
elif new_state < self._old_state:
|
||||
did_reset = True
|
||||
|
||||
if did_reset:
|
||||
self._attr_last_reset = dt_util.utcnow()
|
||||
|
||||
if new_state is not None:
|
||||
self._old_state = new_state
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Ensure the data from the initial update is reflected in the state."""
|
||||
last_state = await self.async_get_last_state()
|
||||
if last_state is not None:
|
||||
if "last_reset" in last_state.attributes:
|
||||
self._attr_last_reset = dt_util.as_utc(
|
||||
datetime.fromisoformat(last_state.attributes["last_reset"])
|
||||
)
|
||||
self._old_state = int(last_state.state)
|
||||
|
||||
self._async_update_from_rest_data()
|
||||
|
||||
@callback
|
||||
|
|
Loading…
Reference in New Issue