Skip to content

Commit

Permalink
Fix history stats count update immediately after change (home-assista…
Browse files Browse the repository at this point in the history
…nt#131856)

* Fix history stats count update immediately after change

* rerun CI
  • Loading branch information
karwosts authored and zweckj committed Dec 1, 2024
1 parent 8cb17ff commit 26a1ae5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion homeassistant/components/history_stats/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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
Expand All @@ -14,6 +16,8 @@

MIN_TIME_UTC = datetime.datetime.min.replace(tzinfo=dt_util.UTC)

_LOGGER = logging.getLogger(__name__)


@dataclass
class HistoryStatsState:
Expand Down Expand Up @@ -186,8 +190,13 @@ def _async_compute_seconds_and_changes(
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:
Expand Down

0 comments on commit 26a1ae5

Please sign in to comment.