Remove `last_reset` attribute and set state class to `total_increasing` for PVOutput energy sensors (#54809)

This commit is contained in:
Erik Montnemery 2021-08-18 13:41:57 +02:00 committed by GitHub
parent 7812b50572
commit 3c5ba1fcc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 38 deletions

View File

@ -2,9 +2,8 @@
from __future__ import annotations
from collections import namedtuple
from datetime import datetime, timedelta
from datetime import timedelta
import logging
from typing import cast
import voluptuous as vol
@ -12,7 +11,7 @@ from homeassistant.components.rest.data import RestData
from homeassistant.components.sensor import (
DEVICE_CLASS_ENERGY,
PLATFORM_SCHEMA,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.const import (
@ -26,8 +25,6 @@ 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"
@ -74,15 +71,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async_add_entities([PvoutputSensor(rest, name)])
class PvoutputSensor(SensorEntity, RestoreEntity):
class PvoutputSensor(SensorEntity):
"""Representation of a PVOutput sensor."""
_attr_state_class = STATE_CLASS_MEASUREMENT
_attr_state_class = STATE_CLASS_TOTAL_INCREASING
_attr_device_class = DEVICE_CLASS_ENERGY
_attr_native_unit_of_measurement = ENERGY_WATT_HOUR
_old_state: int | None = None
def __init__(self, rest, name):
"""Initialize a PVOutput sensor."""
self.rest = rest
@ -129,37 +124,8 @@ class PvoutputSensor(SensorEntity, RestoreEntity):
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