1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Avoid duplicate timestamp conversions for websocket api and recorder (#108144)

* Avoid duplicate timestamp conversions for websocket api and recorder

We convert the time from datetime to timestamps one per
open websocket connection and the recorder for every
state update. Only do the conversion once since its
~30% of the cost of building the state diff

* more

* two more

* two more in live history
This commit is contained in:
J. Nick Koston
2024-01-16 03:05:01 -10:00
committed by GitHub
parent 26058bf922
commit 3d595fff13
6 changed files with 54 additions and 19 deletions

View File

@@ -1077,6 +1077,11 @@ class Event:
if not context.origin_event:
context.origin_event = self
@cached_property
def time_fired_timestamp(self) -> float:
"""Return time fired as a timestamp."""
return self.time_fired.timestamp()
@cached_property
def _as_dict(self) -> dict[str, Any]:
"""Create a dict representation of this Event.
@@ -1445,6 +1450,16 @@ class State:
"_", " "
)
@cached_property
def last_updated_timestamp(self) -> float:
"""Timestamp of last update."""
return self.last_updated.timestamp()
@cached_property
def last_changed_timestamp(self) -> float:
"""Timestamp of last change."""
return self.last_changed.timestamp()
@cached_property
def _as_dict(self) -> dict[str, Any]:
"""Return a dict representation of the State.
@@ -1526,12 +1541,12 @@ class State:
COMPRESSED_STATE_STATE: self.state,
COMPRESSED_STATE_ATTRIBUTES: self.attributes,
COMPRESSED_STATE_CONTEXT: context,
COMPRESSED_STATE_LAST_CHANGED: dt_util.utc_to_timestamp(self.last_changed),
COMPRESSED_STATE_LAST_CHANGED: self.last_changed_timestamp,
}
if self.last_changed != self.last_updated:
compressed_state[COMPRESSED_STATE_LAST_UPDATED] = dt_util.utc_to_timestamp(
self.last_updated
)
compressed_state[
COMPRESSED_STATE_LAST_UPDATED
] = self.last_updated_timestamp
return compressed_state
@cached_property