Optimize _get_states_with_session (#56734)

* Optimize _get_states_with_session

* Move custom filters to derived table

* Remove useless derived table

* Filter old states after grouping

* Split query

* Add comments

* Simplify state update period criteria

* Only apply custom filters if we didn't get an include list of entities

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Erik Montnemery
2021-09-29 17:08:27 +02:00
committed by GitHub
co-authored by J. Nick Koston
parent daebc34f4d
commit 00651a4055
2 changed files with 70 additions and 38 deletions
+12 -1
View File
@@ -48,13 +48,24 @@ def test_get_states(hass_recorder):
wait_recording_done(hass)
# Get states returns everything before POINT
# Get states returns everything before POINT for all entities
for state1, state2 in zip(
states,
sorted(history.get_states(hass, future), key=lambda state: state.entity_id),
):
assert state1 == state2
# Get states returns everything before POINT for tested entities
entities = [f"test.point_in_time_{i % 5}" for i in range(5)]
for state1, state2 in zip(
states,
sorted(
history.get_states(hass, future, entities),
key=lambda state: state.entity_id,
),
):
assert state1 == state2
# Test get_state here because we have a DB setup
assert states[0] == history.get_state(hass, future, states[0].entity_id)