mirror of
https://github.com/home-assistant/core.git
synced 2025-08-06 14:15:12 +02:00
adjust
This commit is contained in:
@@ -321,7 +321,6 @@ def _state_changed_during_period_stmt(
|
|||||||
end_time_ts: float | None,
|
end_time_ts: float | None,
|
||||||
single_metadata_id: int,
|
single_metadata_id: int,
|
||||||
no_attributes: bool,
|
no_attributes: bool,
|
||||||
descending: bool,
|
|
||||||
limit: int | None,
|
limit: int | None,
|
||||||
include_start_time_state: bool,
|
include_start_time_state: bool,
|
||||||
run_start_ts: float | None,
|
run_start_ts: float | None,
|
||||||
@@ -348,10 +347,10 @@ def _state_changed_during_period_stmt(
|
|||||||
if not include_start_time_state or not run_start_ts:
|
if not include_start_time_state or not run_start_ts:
|
||||||
return stmt.order_by(
|
return stmt.order_by(
|
||||||
States.metadata_id,
|
States.metadata_id,
|
||||||
States.last_updated_ts.desc() if descending else States.last_updated_ts,
|
States.last_updated_ts,
|
||||||
)
|
)
|
||||||
|
return _select_from_subquery(
|
||||||
union_subquery = union_all(
|
union_all(
|
||||||
_select_from_subquery(
|
_select_from_subquery(
|
||||||
_get_single_entity_start_time_stmt(
|
_get_single_entity_start_time_stmt(
|
||||||
start_time_ts,
|
start_time_ts,
|
||||||
@@ -363,17 +362,13 @@ def _state_changed_during_period_stmt(
|
|||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
_select_from_subquery(
|
_select_from_subquery(
|
||||||
stmt.order_by(States.metadata_id, States.last_updated_ts).subquery(),
|
stmt.subquery(),
|
||||||
no_attributes,
|
no_attributes,
|
||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
).subquery()
|
).subquery(),
|
||||||
stmt = _select_from_subquery(union_subquery, no_attributes, False)
|
no_attributes,
|
||||||
if not descending:
|
False,
|
||||||
return stmt
|
|
||||||
# If descending, we need to reverse the results
|
|
||||||
return stmt.order_by(
|
|
||||||
union_subquery.c.metadata_id, union_subquery.c.last_updated_ts.desc()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -417,7 +412,6 @@ def state_changes_during_period(
|
|||||||
end_time_ts,
|
end_time_ts,
|
||||||
single_metadata_id,
|
single_metadata_id,
|
||||||
no_attributes,
|
no_attributes,
|
||||||
descending,
|
|
||||||
limit,
|
limit,
|
||||||
include_start_time_state,
|
include_start_time_state,
|
||||||
run_start_ts,
|
run_start_ts,
|
||||||
@@ -425,7 +419,6 @@ def state_changes_during_period(
|
|||||||
track_on=[
|
track_on=[
|
||||||
bool(end_time_ts),
|
bool(end_time_ts),
|
||||||
no_attributes,
|
no_attributes,
|
||||||
descending,
|
|
||||||
bool(limit),
|
bool(limit),
|
||||||
include_start_time_state,
|
include_start_time_state,
|
||||||
],
|
],
|
||||||
@@ -437,6 +430,7 @@ def state_changes_during_period(
|
|||||||
start_time_ts if include_start_time_state else None,
|
start_time_ts if include_start_time_state else None,
|
||||||
entity_ids,
|
entity_ids,
|
||||||
entity_id_to_metadata_id,
|
entity_id_to_metadata_id,
|
||||||
|
descending=descending,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -654,6 +648,7 @@ def _sorted_states_to_dict(
|
|||||||
entity_id_to_metadata_id: dict[str, int | None],
|
entity_id_to_metadata_id: dict[str, int | None],
|
||||||
minimal_response: bool = False,
|
minimal_response: bool = False,
|
||||||
compressed_state_format: bool = False,
|
compressed_state_format: bool = False,
|
||||||
|
descending: bool = False,
|
||||||
) -> MutableMapping[str, list[State | dict[str, Any]]]:
|
) -> MutableMapping[str, list[State | dict[str, Any]]]:
|
||||||
"""Convert SQL results into JSON friendly data structure.
|
"""Convert SQL results into JSON friendly data structure.
|
||||||
|
|
||||||
@@ -778,5 +773,9 @@ def _sorted_states_to_dict(
|
|||||||
if (state := row[state_idx]) != prev_state
|
if (state := row[state_idx]) != prev_state
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if descending:
|
||||||
|
for ent_results in result.values():
|
||||||
|
ent_results.reverse()
|
||||||
|
|
||||||
# Filter out the empty lists if some states had 0 results.
|
# Filter out the empty lists if some states had 0 results.
|
||||||
return {key: val for key, val in result.items() if val}
|
return {key: val for key, val in result.items() if val}
|
||||||
|
@@ -303,6 +303,7 @@ def test_state_changes_during_period_descending(
|
|||||||
hist = history.state_changes_during_period(
|
hist = history.state_changes_during_period(
|
||||||
hass, start, end, entity_id, no_attributes=False, descending=False
|
hass, start, end, entity_id, no_attributes=False, descending=False
|
||||||
)
|
)
|
||||||
|
|
||||||
assert_multiple_states_equal_without_context(states, hist[entity_id])
|
assert_multiple_states_equal_without_context(states, hist[entity_id])
|
||||||
|
|
||||||
hist = history.state_changes_during_period(
|
hist = history.state_changes_during_period(
|
||||||
@@ -312,6 +313,24 @@ def test_state_changes_during_period_descending(
|
|||||||
states, list(reversed(list(hist[entity_id])))
|
states, list(reversed(list(hist[entity_id])))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
hist = history.state_changes_during_period(
|
||||||
|
hass,
|
||||||
|
point2, # Pick a point where we will generate a start time state
|
||||||
|
end,
|
||||||
|
entity_id,
|
||||||
|
no_attributes=False,
|
||||||
|
descending=True,
|
||||||
|
include_start_time_state=True,
|
||||||
|
)
|
||||||
|
hist_states = list(hist[entity_id])
|
||||||
|
assert len(hist_states) == 3
|
||||||
|
# Make sure they are in descending order
|
||||||
|
assert (
|
||||||
|
hist_states[0].last_updated
|
||||||
|
> hist_states[1].last_updated
|
||||||
|
> hist_states[2].last_updated
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_get_last_state_changes(hass_recorder: Callable[..., HomeAssistant]) -> None:
|
def test_get_last_state_changes(hass_recorder: Callable[..., HomeAssistant]) -> None:
|
||||||
"""Test number of state changes."""
|
"""Test number of state changes."""
|
||||||
|
Reference in New Issue
Block a user