Home Assistant Unofficial Reference 2024.12.1
helpers.py
Go to the documentation of this file.
1 """Helpers for the history integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Iterable
6 from datetime import datetime as dt
7 
8 from homeassistant.components.recorder import get_instance
9 from homeassistant.core import HomeAssistant
10 
11 
13  hass: HomeAssistant, entity_ids: Iterable, start_time: dt, no_attributes: bool
14 ) -> bool:
15  """Check the state machine to see if entities have changed since start time."""
16  for entity_id in entity_ids:
17  state = hass.states.get(entity_id)
18  if state is None:
19  return True
20 
21  state_time = state.last_changed if no_attributes else state.last_updated
22  if state_time > start_time:
23  return True
24 
25  return False
26 
27 
28 def has_states_before(hass: HomeAssistant, run_time: dt) -> bool:
29  """Check if the recorder has states as old or older than run_time.
30 
31  Returns True if there may be such states.
32  """
33  oldest_ts = get_instance(hass).states_manager.oldest_ts
34  return oldest_ts is not None and run_time.timestamp() >= oldest_ts
bool has_states_before(HomeAssistant hass, dt run_time)
Definition: helpers.py:28
bool entities_may_have_state_changes_after(HomeAssistant hass, Iterable entity_ids, dt start_time, bool no_attributes)
Definition: helpers.py:14
Recorder get_instance(HomeAssistant hass)
Definition: recorder.py:74