Fix history stats count update immediately after change (#131856)

* Fix history stats count update immediately after change

* rerun CI
pull/132195/head
karwosts 2024-11-29 21:43:31 -08:00 committed by Paulus Schoutsen
parent 8eb52edabf
commit 5bf972ff16
1 changed files with 10 additions and 1 deletions

View File

@ -4,6 +4,8 @@ from __future__ import annotations
from dataclasses import dataclass
import datetime
import logging
import math
from homeassistant.components.recorder import get_instance, history
from homeassistant.core import Event, EventStateChangedData, HomeAssistant, State
@ -14,6 +16,8 @@ from .helpers import async_calculate_period, floored_timestamp
MIN_TIME_UTC = datetime.datetime.min.replace(tzinfo=dt_util.UTC)
_LOGGER = logging.getLogger(__name__)
@dataclass
class HistoryStatsState:
@ -186,8 +190,13 @@ class HistoryStats:
current_state_matches = history_state.state in self._entity_states
state_change_timestamp = history_state.last_changed
if state_change_timestamp > now_timestamp:
if math.floor(state_change_timestamp) > now_timestamp:
# Shouldn't count states that are in the future
_LOGGER.debug(
"Skipping future timestamp %s (now %s)",
state_change_timestamp,
now_timestamp,
)
continue
if previous_state_matches: