Improve performance of fetching and storing history and events with the database (#84870)

This commit is contained in:
J. Nick Koston 2023-01-02 13:26:08 -10:00 committed by GitHub
parent 0ad16e25ef
commit b8a1537b58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 3696 additions and 281 deletions

View file

@ -2,7 +2,6 @@
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime as dt
import json
from typing import Any, cast
@ -10,6 +9,7 @@ from sqlalchemy.engine.row import Row
from homeassistant.const import ATTR_ICON, EVENT_STATE_CHANGED
from homeassistant.core import Context, Event, State, callback
import homeassistant.util.dt as dt_util
class LazyEventPartialState:
@ -66,7 +66,7 @@ class EventAsRow:
data: dict[str, Any]
context: Context
context_id: str
time_fired: dt
time_fired_ts: float
state_id: int
event_data: str | None = None
old_format_icon: None = None
@ -92,7 +92,7 @@ def async_event_to_row(event: Event) -> EventAsRow | None:
context_id=event.context.id,
context_user_id=event.context.user_id,
context_parent_id=event.context.parent_id,
time_fired=event.time_fired,
time_fired_ts=dt_util.utc_to_timestamp(event.time_fired),
state_id=hash(event),
)
# States are prefiltered so we never get states
@ -107,7 +107,7 @@ def async_event_to_row(event: Event) -> EventAsRow | None:
context_id=new_state.context.id,
context_user_id=new_state.context.user_id,
context_parent_id=new_state.context.parent_id,
time_fired=new_state.last_updated,
time_fired_ts=dt_util.utc_to_timestamp(new_state.last_updated),
state_id=hash(event),
icon=new_state.attributes.get(ATTR_ICON),
)