mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Improve alignment of live logbook and historical logbook models (#123070)
* Improve alignment of live logbook and historical logbook models - Make EventAsRow as NamedType which is better aligned with sqlalchemy Row - Use getitem to fetch results for both Row and EventAsRow since its an order of magnitude faster fetching sqlalchemy Row object values. * final * fix * unused * fix more tests * cleanup * reduce * tweak
This commit is contained in:
@@ -4,10 +4,8 @@ from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import collections
|
||||
from collections.abc import Callable
|
||||
from contextlib import suppress
|
||||
import json
|
||||
import logging
|
||||
from timeit import default_timer as timer
|
||||
|
||||
@@ -18,7 +16,7 @@ from homeassistant.helpers.event import (
|
||||
async_track_state_change,
|
||||
async_track_state_change_event,
|
||||
)
|
||||
from homeassistant.helpers.json import JSON_DUMP, JSONEncoder
|
||||
from homeassistant.helpers.json import JSON_DUMP
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs
|
||||
# mypy: no-warn-return-any
|
||||
@@ -310,48 +308,3 @@ async def json_serialize_states(hass):
|
||||
start = timer()
|
||||
JSON_DUMP(states)
|
||||
return timer() - start
|
||||
|
||||
|
||||
def _create_state_changed_event_from_old_new(
|
||||
entity_id, event_time_fired, old_state, new_state
|
||||
):
|
||||
"""Create a state changed event from a old and new state."""
|
||||
attributes = {}
|
||||
if new_state is not None:
|
||||
attributes = new_state.get("attributes")
|
||||
attributes_json = json.dumps(attributes, cls=JSONEncoder)
|
||||
if attributes_json == "null":
|
||||
attributes_json = "{}"
|
||||
row = collections.namedtuple( # noqa: PYI024
|
||||
"Row",
|
||||
[
|
||||
"event_type"
|
||||
"event_data"
|
||||
"time_fired"
|
||||
"context_id"
|
||||
"context_user_id"
|
||||
"state"
|
||||
"entity_id"
|
||||
"domain"
|
||||
"attributes"
|
||||
"state_id",
|
||||
"old_state_id",
|
||||
],
|
||||
)
|
||||
|
||||
row.event_type = EVENT_STATE_CHANGED
|
||||
row.event_data = "{}"
|
||||
row.attributes = attributes_json
|
||||
row.time_fired = event_time_fired
|
||||
row.state = new_state and new_state.get("state")
|
||||
row.entity_id = entity_id
|
||||
row.domain = entity_id and core.split_entity_id(entity_id)[0]
|
||||
row.context_id = None
|
||||
row.context_user_id = None
|
||||
row.old_state_id = old_state and 1
|
||||
row.state_id = new_state and 1
|
||||
|
||||
# pylint: disable-next=import-outside-toplevel
|
||||
from homeassistant.components import logbook
|
||||
|
||||
return logbook.LazyEventPartialState(row, {})
|
||||
|
||||
Reference in New Issue
Block a user