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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 19 deletions

View file

@ -183,9 +183,9 @@ def _state_diff(
if old_state.state != new_state.state:
additions[COMPRESSED_STATE_STATE] = new_state.state
if old_state.last_changed != new_state.last_changed:
additions[COMPRESSED_STATE_LAST_CHANGED] = new_state.last_changed.timestamp()
additions[COMPRESSED_STATE_LAST_CHANGED] = new_state.last_changed_timestamp
elif old_state.last_updated != new_state.last_updated:
additions[COMPRESSED_STATE_LAST_UPDATED] = new_state.last_updated.timestamp()
additions[COMPRESSED_STATE_LAST_UPDATED] = new_state.last_updated_timestamp
if old_state_context.parent_id != new_state_context.parent_id:
additions[COMPRESSED_STATE_CONTEXT] = {"parent_id": new_state_context.parent_id}
if old_state_context.user_id != new_state_context.user_id: