Convert last_reset timestamps to UTC (#56561)

* Convert last_reset timestamps to UTC

* Add test

* Apply suggestion from code review
This commit is contained in:
Erik Montnemery 2021-09-24 09:16:50 +02:00 committed by GitHub
parent e62c9d338e
commit 7452998081
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 148 additions and 9 deletions

View file

@ -312,6 +312,21 @@ def _wanted_statistics(
return wanted_statistics
def _last_reset_as_utc_isoformat(
last_reset_s: str | None, entity_id: str
) -> str | None:
"""Parse last_reset and convert it to UTC."""
if last_reset_s is None:
return None
last_reset = dt_util.parse_datetime(last_reset_s)
if last_reset is None:
_LOGGER.warning(
"Ignoring invalid last reset '%s' for %s", last_reset_s, entity_id
)
return None
return dt_util.as_utc(last_reset).isoformat()
def compile_statistics( # noqa: C901
hass: HomeAssistant, start: datetime.datetime, end: datetime.datetime
) -> list[StatisticResult]:
@ -424,7 +439,11 @@ def compile_statistics( # noqa: C901
reset = False
if (
state_class != STATE_CLASS_TOTAL_INCREASING
and (last_reset := state.attributes.get("last_reset"))
and (
last_reset := _last_reset_as_utc_isoformat(
state.attributes.get("last_reset"), entity_id
)
)
!= old_last_reset
):
if old_state is None: