Avoid additional timestamp conversion to set state (#118885)

Avoid addtional timestamp conversion to set state

Since we already have the timestamp, we can pass it on to the State
object and avoid the additional timestamp conversion which can be as
much as 30% of the state write runtime.

Since datetime objects are limited to microsecond precision, we need
to adjust some tests to account for the additional precision that we
will now be able to get in the database
This commit is contained in:
J. Nick Koston 2024-06-05 22:43:34 -05:00 committed by GitHub
parent 475c20d529
commit f9205cd88d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 332 additions and 143 deletions

View file

@ -96,9 +96,7 @@ async def test_state_diff_event(hass: HomeAssistant) -> None:
message = _state_diff_event(last_state_event)
assert message == {
"c": {
"light.window": {
"+": {"lc": new_state.last_changed.timestamp(), "s": "off"}
}
"light.window": {"+": {"lc": new_state.last_changed_timestamp, "s": "off"}}
}
}
@ -117,7 +115,7 @@ async def test_state_diff_event(hass: HomeAssistant) -> None:
"light.window": {
"+": {
"c": {"parent_id": "new-parent-id"},
"lc": new_state.last_changed.timestamp(),
"lc": new_state.last_changed_timestamp,
"s": "red",
}
}
@ -144,7 +142,7 @@ async def test_state_diff_event(hass: HomeAssistant) -> None:
"parent_id": "another-new-parent-id",
"user_id": "new-user-id",
},
"lc": new_state.last_changed.timestamp(),
"lc": new_state.last_changed_timestamp,
"s": "green",
}
}
@ -168,7 +166,7 @@ async def test_state_diff_event(hass: HomeAssistant) -> None:
"light.window": {
"+": {
"c": {"user_id": "another-new-user-id"},
"lc": new_state.last_changed.timestamp(),
"lc": new_state.last_changed_timestamp,
"s": "blue",
}
}
@ -194,7 +192,7 @@ async def test_state_diff_event(hass: HomeAssistant) -> None:
"light.window": {
"+": {
"c": "id-new",
"lc": new_state.last_changed.timestamp(),
"lc": new_state.last_changed_timestamp,
"s": "yellow",
}
}
@ -216,7 +214,7 @@ async def test_state_diff_event(hass: HomeAssistant) -> None:
"+": {
"a": {"new": "attr"},
"c": {"id": new_context.id, "parent_id": None, "user_id": None},
"lc": new_state.last_changed.timestamp(),
"lc": new_state.last_changed_timestamp,
"s": "purple",
}
}
@ -232,7 +230,7 @@ async def test_state_diff_event(hass: HomeAssistant) -> None:
assert message == {
"c": {
"light.window": {
"+": {"lc": new_state.last_changed.timestamp(), "s": "green"},
"+": {"lc": new_state.last_changed_timestamp, "s": "green"},
"-": {"a": ["new"]},
}
}
@ -254,7 +252,7 @@ async def test_state_diff_event(hass: HomeAssistant) -> None:
"light.window": {
"+": {
"a": {"list_attr": ["a", "b", "c", "d"], "list_attr_2": ["a", "b"]},
"lu": new_state.last_updated.timestamp(),
"lu": new_state.last_updated_timestamp,
}
}
}
@ -275,7 +273,7 @@ async def test_state_diff_event(hass: HomeAssistant) -> None:
"light.window": {
"+": {
"a": {"list_attr": ["a", "b", "c", "e"]},
"lu": new_state.last_updated.timestamp(),
"lu": new_state.last_updated_timestamp,
},
"-": {"a": ["list_attr_2"]},
}