Remove last_reset
attribute and set state class to total_increasing
for fronius energy sensors (#54830)
This commit is contained in:
parent
09fbc38baa
commit
6aca3b326f
1 changed files with 16 additions and 14 deletions
|
@ -11,6 +11,7 @@ import voluptuous as vol
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
|
STATE_CLASS_TOTAL_INCREASING,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
|
@ -32,7 +33,6 @@ from homeassistant.core import callback
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
from homeassistant.util import dt
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -64,6 +64,17 @@ PREFIX_DEVICE_CLASS_MAPPING = [
|
||||||
("voltage", DEVICE_CLASS_VOLTAGE),
|
("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):
|
def _device_id_validator(config):
|
||||||
"""Ensure that inverters have default id 1 and other devices 0."""
|
"""Ensure that inverters have default id 1 and other devices 0."""
|
||||||
|
@ -281,8 +292,6 @@ class FroniusPowerFlow(FroniusAdapter):
|
||||||
class FroniusTemplateSensor(SensorEntity):
|
class FroniusTemplateSensor(SensorEntity):
|
||||||
"""Sensor for the single values (e.g. pv power, ac power)."""
|
"""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:
|
def __init__(self, parent: FroniusAdapter, key: str) -> None:
|
||||||
"""Initialize a singular value sensor."""
|
"""Initialize a singular value sensor."""
|
||||||
self._key = key
|
self._key = key
|
||||||
|
@ -292,6 +301,10 @@ class FroniusTemplateSensor(SensorEntity):
|
||||||
if self._key.startswith(prefix):
|
if self._key.startswith(prefix):
|
||||||
self._attr_device_class = device_class
|
self._attr_device_class = device_class
|
||||||
break
|
break
|
||||||
|
for prefix, state_class in PREFIX_STATE_CLASS_MAPPING:
|
||||||
|
if self._key.startswith(prefix):
|
||||||
|
self._attr_state_class = state_class
|
||||||
|
break
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
|
@ -311,17 +324,6 @@ class FroniusTemplateSensor(SensorEntity):
|
||||||
self._attr_native_value = round(self._attr_native_value, 2)
|
self._attr_native_value = round(self._attr_native_value, 2)
|
||||||
self._attr_native_unit_of_measurement = state.get("unit")
|
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):
|
async def async_added_to_hass(self):
|
||||||
"""Register at parent component for updates."""
|
"""Register at parent component for updates."""
|
||||||
self.async_on_remove(self._parent.register(self))
|
self.async_on_remove(self._parent.register(self))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue