1 """Models states in for Recorder."""
3 from __future__
import annotations
5 from datetime
import datetime
7 from typing
import TYPE_CHECKING, Any
9 from propcache
import cached_property
10 from sqlalchemy.engine.row
import Row
13 COMPRESSED_STATE_ATTRIBUTES,
14 COMPRESSED_STATE_LAST_CHANGED,
15 COMPRESSED_STATE_LAST_UPDATED,
16 COMPRESSED_STATE_STATE,
21 from .state_attributes
import decode_attributes_from_source
23 _LOGGER = logging.getLogger(__name__)
29 entity_id_to_metadata_id: dict[str, int |
None],
31 """Extract metadata ids from entity_id_to_metadata_id."""
34 for metadata_id
in entity_id_to_metadata_id.values()
35 if metadata_id
is not None
40 """A lazy version of core State after schema 31."""
45 attr_cache: dict[str, dict[str, Any]],
46 start_time_ts: float |
None,
49 last_updated_ts: float |
None,
52 """Init the lazy state."""
56 self._attributes: dict[str, Any] |
None =
None
57 self._last_updated_ts: float |
None = last_updated_ts
or start_time_ts
63 """State attributes."""
65 getattr(self.
_row_row,
"attributes",
None), self.
attr_cacheattr_cache
70 """Last changed timestamp."""
71 return getattr(self.
_row_row,
"last_changed_ts",
None)
75 """Last changed datetime."""
76 return dt_util.utc_from_timestamp(
82 """Last reported timestamp."""
83 return getattr(self.
_row_row,
"last_reported_ts",
None)
87 """Last reported datetime."""
88 return dt_util.utc_from_timestamp(
94 """Last updated datetime."""
96 assert self._last_updated_ts
is not None
97 return dt_util.utc_from_timestamp(self._last_updated_ts)
100 """Return a dict representation of the LazyState.
104 To be used for JSON serialization.
108 last_changed_isoformat = last_updated_isoformat
115 "last_changed": last_changed_isoformat,
116 "last_updated": last_updated_isoformat,
122 attr_cache: dict[str, dict[str, Any]],
123 start_time_ts: float |
None,
126 last_updated_ts: float |
None,
129 """Convert a database row to a compressed state schema 41 and later."""
130 comp_state: dict[str, Any] = {COMPRESSED_STATE_STATE: state}
131 if not no_attributes:
133 getattr(row,
"attributes",
None), attr_cache
135 row_last_updated_ts: float = last_updated_ts
or start_time_ts
136 comp_state[COMPRESSED_STATE_LAST_UPDATED] = row_last_updated_ts
138 (row_last_changed_ts := getattr(row,
"last_changed_ts",
None))
139 and row_last_changed_ts
140 and row_last_updated_ts != row_last_changed_ts
142 comp_state[COMPRESSED_STATE_LAST_CHANGED] = row_last_changed_ts
float|None _last_changed_ts(self)
datetime last_changed(self)
dict[str, Any] attributes(self)
dict[str, Any] as_dict(self)
None __init__(self, Row row, dict[str, dict[str, Any]] attr_cache, float|None start_time_ts, str entity_id, str state, float|None last_updated_ts, bool no_attributes)
float|None _last_reported_ts(self)
datetime last_updated(self)
dict[str, Any] decode_attributes_from_source(Any source, dict[str, dict[str, Any]] attr_cache)
dict[str, Any] row_to_compressed_state(Row row, dict[str, dict[str, Any]] attr_cache, float|None start_time_ts, str entity_id, str state, float|None last_updated_ts, bool no_attributes)
list[int] extract_metadata_ids(dict[str, int|None] entity_id_to_metadata_id)