I can’t for the life of me work out how to provide the state_class and last_reset properties to Home Assistant such that users can access statistics for the values.
@property
def state_class(self):
"""Return the state class if relevent"""
measurement = getattr(self.__statistic, "measurement", False)
if measurement:
_LOGGER.debug("Is measurement class")
return "measurement"
else:
_LOGGER.error("Is not measurement class")
return None
And I’m never seeing those properties being accessed by Home Assistant.
Of course - this means that there are no statistics available when trying to access them in lovelace:
It might make sense that not using a built-in device_class along with its default unit_of_measurement could be the problem, don’t you think?
The only default device_class you seem to use is timestamp and I’m not even sure how it would make sense to extract statistics from this type of measurement.
Just to be on the safe side, I’d try to set last_reset to dt_util.utc_from_timestamp(0) (with from homeassistant.util import dt as dt_util) instead of None by default, but I’m not sure it makes sense for measurements that are not continually accumulating…
I’m not sure I follow? The default is timestamp - that doesn’t mean that everything (or anything) is the default. (see image in previous post) - the unit of measurement for most of the sensors is “cases” or “doses”
Additionally - I’d like to keep last_reset as the actual last reset value - rather than epoch
What I mean is that except for ISO8601, you only seem to use custom unit_of_measurement
def unit_of_measurement(self):
"""Return the unit of measurement"""
if (
self.__statistic.typeName
and self.__statistic.typeName is not None
and self.__statistic.typeName in TIMESTAMP_TYPES
):
return "ISO8601"
if self.__statistic.unit and self.__statistic.unit == "case":
return "cases"
if self.__statistic.unit and self.__statistic.unit == "death":
return "deaths"
if self.__statistic.unit and self.__statistic.unit == "dose":
return "doses"
if self.__statistic.unit and self.__statistic.unit == "test":
return "tests"
return self.__statistic.unit
Same with device_class for which you seem to have timestamp or None
def device_class(self):
"""Return the device class if relevent"""
if (
self.__statistic.typeName
and self.__statistic.typeName is not None
and self.__statistic.typeName in TIMESTAMP_TYPES
):
return "timestamp"
return None
My concern is that HA might only be able to generate statistics for known device_class with suitable unit_of_measurement.
As far as the last_reset, my suggestion is only to replace None in the following; not to return dt_util.utc_from_timestamp(0) systematically.
def last_reset(self):
"""Return the state class if relevent"""
resetting = getattr(self.__statistic, "resetting", False)
if not resetting:
return None
return self.__statistic.published