This commit is contained in:
J. Nick Koston
2023-04-10 22:16:34 -10:00
parent a322219e73
commit 84ec444f45
2 changed files with 32 additions and 53 deletions

View File

@@ -441,10 +441,10 @@ def state_changes_during_period(
) )
def _get_last_state_changes_stmt(number_of_states: int, metadata_id: int) -> Select: def _get_last_state_changes_single_stmt(metadata_id: int) -> Select:
stmt = _stmt_and_join_attributes(False, False) return (
if number_of_states == 1: _stmt_and_join_attributes(False, False)
stmt = stmt.join( .join(
( (
lastest_state_for_metadata_id := ( lastest_state_for_metadata_id := (
select( select(
@@ -464,8 +464,19 @@ def _get_last_state_changes_stmt(number_of_states: int, metadata_id: int) -> Sel
== lastest_state_for_metadata_id.c.max_last_updated, == lastest_state_for_metadata_id.c.max_last_updated,
), ),
) )
else: .outerjoin(
stmt = stmt.where( StateAttributes, States.attributes_id == StateAttributes.attributes_id
)
.order_by(States.state_id.desc())
)
def _get_last_state_changes_multiple_stmt(
number_of_states: int, metadata_id: int
) -> Select:
return (
_stmt_and_join_attributes(False, False)
.where(
States.state_id States.state_id
== ( == (
select(States.state_id) select(States.state_id)
@@ -475,10 +486,11 @@ def _get_last_state_changes_stmt(number_of_states: int, metadata_id: int) -> Sel
.subquery() .subquery()
).c.state_id ).c.state_id
) )
stmt = stmt.outerjoin( .outerjoin(
StateAttributes, States.attributes_id == StateAttributes.attributes_id StateAttributes, States.attributes_id == StateAttributes.attributes_id
).order_by(States.state_id.desc()) )
return stmt .order_by(States.state_id.desc())
)
def get_last_state_changes( def get_last_state_changes(
@@ -502,10 +514,16 @@ def get_last_state_changes(
return {} return {}
metadata_id = possible_metadata_id metadata_id = possible_metadata_id
entity_id_to_metadata_id: dict[str, int | None] = {entity_id_lower: metadata_id} entity_id_to_metadata_id: dict[str, int | None] = {entity_id_lower: metadata_id}
stmt = lambda_stmt( if number_of_states == 1:
lambda: _get_last_state_changes_stmt(number_of_states, metadata_id), stmt = lambda_stmt(
track_on=[number_of_states == 1], lambda: _get_last_state_changes_single_stmt(metadata_id),
) )
else:
stmt = lambda_stmt(
lambda: _get_last_state_changes_multiple_stmt(
number_of_states, metadata_id
),
)
states = list(execute_stmt_lambda_element(session, stmt)) states = list(execute_stmt_lambda_element(session, stmt))
return cast( return cast(
MutableMapping[str, list[State]], MutableMapping[str, list[State]],

View File

@@ -41,7 +41,6 @@ from .common import (
wait_recording_done, wait_recording_done,
) )
from tests.common import mock_state_change_event
from tests.typing import RecorderInstanceGenerator from tests.typing import RecorderInstanceGenerator
@@ -118,44 +117,6 @@ def _add_db_entries(
) )
def _setup_get_states(hass):
"""Set up for testing get_states."""
states = []
now = dt_util.utcnow()
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=now
):
for i in range(5):
state = ha.State(
f"test.point_in_time_{i % 5}",
f"State {i}",
{"attribute_test": i},
)
mock_state_change_event(hass, state)
states.append(state)
wait_recording_done(hass)
future = now + timedelta(seconds=1)
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=future
):
for i in range(5):
state = ha.State(
f"test.point_in_time_{i % 5}",
f"State {i}",
{"attribute_test": i},
)
mock_state_change_event(hass, state)
wait_recording_done(hass)
return now, future, states
def test_get_full_significant_states_with_session_entity_no_matches( def test_get_full_significant_states_with_session_entity_no_matches(
hass_recorder: Callable[..., HomeAssistant] hass_recorder: Callable[..., HomeAssistant]
) -> None: ) -> None: