Remove last_reset attribute and set state class to total_increasing for fronius energy sensors (#54830)

This commit is contained in:
Erik Montnemery 2021-08-18 16:57:19 +02:00 committed by GitHub
parent 09fbc38baa
commit 6aca3b326f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,7 @@ import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.const import (
@ -32,7 +33,6 @@ from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.util import dt
_LOGGER = logging.getLogger(__name__)
@ -64,6 +64,17 @@ PREFIX_DEVICE_CLASS_MAPPING = [
("voltage", DEVICE_CLASS_VOLTAGE),
]
PREFIX_STATE_CLASS_MAPPING = [
("state_of_charge", STATE_CLASS_MEASUREMENT),
("temperature", STATE_CLASS_MEASUREMENT),
("power_factor", STATE_CLASS_MEASUREMENT),
("power", STATE_CLASS_MEASUREMENT),
("energy", STATE_CLASS_TOTAL_INCREASING),
("current", STATE_CLASS_MEASUREMENT),
("timestamp", STATE_CLASS_MEASUREMENT),
("voltage", STATE_CLASS_MEASUREMENT),
]
def _device_id_validator(config):
"""Ensure that inverters have default id 1 and other devices 0."""
@ -281,8 +292,6 @@ class FroniusPowerFlow(FroniusAdapter):
class FroniusTemplateSensor(SensorEntity):
"""Sensor for the single values (e.g. pv power, ac power)."""
_attr_state_class = STATE_CLASS_MEASUREMENT
def __init__(self, parent: FroniusAdapter, key: str) -> None:
"""Initialize a singular value sensor."""
self._key = key
@ -292,6 +301,10 @@ class FroniusTemplateSensor(SensorEntity):
if self._key.startswith(prefix):
self._attr_device_class = device_class
break
for prefix, state_class in PREFIX_STATE_CLASS_MAPPING:
if self._key.startswith(prefix):
self._attr_state_class = state_class
break
@property
def should_poll(self):
@ -311,17 +324,6 @@ class FroniusTemplateSensor(SensorEntity):
self._attr_native_value = round(self._attr_native_value, 2)
self._attr_native_unit_of_measurement = state.get("unit")
@property
def last_reset(self) -> dt.dt.datetime | None:
"""Return the time when the sensor was last reset, if it is a meter."""
if self._key.endswith("day"):
return dt.start_of_local_day()
if self._key.endswith("year"):
return dt.start_of_local_day(dt.dt.date(dt.now().year, 1, 1))
if self._key.endswith("total") or self._key.startswith("energy_real"):
return dt.utc_from_timestamp(0)
return None
async def async_added_to_hass(self):
"""Register at parent component for updates."""
self.async_on_remove(self._parent.register(self))