From 195811843b67fbd2dbd93587c3e88162ce53674c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 26 Apr 2022 13:05:43 -1000 Subject: [PATCH] Remove get_state and get_states history api calls (#70830) --- homeassistant/components/history/__init__.py | 14 -- homeassistant/components/recorder/history.py | 34 ----- tests/components/recorder/test_history.py | 134 +++++++------------ 3 files changed, 50 insertions(+), 132 deletions(-) diff --git a/homeassistant/components/history/__init__.py b/homeassistant/components/history/__init__.py index 2bf285d25e6..8740f352dee 100644 --- a/homeassistant/components/history/__init__.py +++ b/homeassistant/components/history/__init__.py @@ -79,20 +79,6 @@ def get_last_state_changes(hass, number_of_states, entity_id): return history.get_last_state_changes(hass, number_of_states, entity_id) -@deprecated_function("homeassistant.components.recorder.history.get_states") -def get_states(hass, utc_point_in_time, entity_ids=None, run=None, filters=None): - """Return the states at a specific point in time.""" - return history.get_states( - hass, utc_point_in_time, entity_ids=None, run=None, filters=None - ) - - -@deprecated_function("homeassistant.components.recorder.history.get_state") -def get_state(hass, utc_point_in_time, entity_id, run=None): - """Return a state at a specific point in time.""" - return history.get_state(hass, utc_point_in_time, entity_id, run=None) - - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the history hooks.""" conf = config.get(DOMAIN, {}) diff --git a/homeassistant/components/recorder/history.py b/homeassistant/components/recorder/history.py index b58d3ed9e8c..24fe21f101c 100644 --- a/homeassistant/components/recorder/history.py +++ b/homeassistant/components/recorder/history.py @@ -470,28 +470,6 @@ def get_last_state_changes( ) -def get_states( - hass: HomeAssistant, - utc_point_in_time: datetime, - entity_ids: list[str] | None = None, - run: RecorderRuns | None = None, - filters: Any = None, - no_attributes: bool = False, -) -> list[State]: - """Return the states at a specific point in time.""" - if run is None: - run = recorder.get_instance(hass).run_history.get(utc_point_in_time) - - if run is None or process_timestamp(run.start) > utc_point_in_time: - # History did not run before utc_point_in_time - return [] - - with session_scope(hass=hass) as session: - return _get_states_with_session( - hass, session, utc_point_in_time, entity_ids, run, filters, no_attributes - ) - - def _get_states_with_session( hass: HomeAssistant, session: Session, @@ -721,15 +699,3 @@ def _sorted_states_to_dict( # Filter out the empty lists if some states had 0 results. return {key: val for key, val in result.items() if val} - - -def get_state( - hass: HomeAssistant, - utc_point_in_time: datetime, - entity_id: str, - run: RecorderRuns | None = None, - no_attributes: bool = False, -) -> State | None: - """Return a state at a specific point in time.""" - states = get_states(hass, utc_point_in_time, [entity_id], run, None, no_attributes) - return states[0] if states else None diff --git a/tests/components/recorder/test_history.py b/tests/components/recorder/test_history.py index 09759add23f..8f22447cd8e 100644 --- a/tests/components/recorder/test_history.py +++ b/tests/components/recorder/test_history.py @@ -1,4 +1,6 @@ """The tests the History component.""" +from __future__ import annotations + # pylint: disable=protected-access,invalid-name from copy import copy from datetime import datetime, timedelta @@ -12,11 +14,14 @@ from homeassistant.components import recorder from homeassistant.components.recorder import history from homeassistant.components.recorder.models import ( Events, + RecorderRuns, StateAttributes, States, process_timestamp, ) +from homeassistant.components.recorder.util import session_scope import homeassistant.core as ha +from homeassistant.core import HomeAssistant from homeassistant.helpers.json import JSONEncoder import homeassistant.util.dt as dt_util @@ -24,6 +29,26 @@ from tests.common import SetupRecorderInstanceT, mock_state_change_event from tests.components.recorder.common import wait_recording_done +async def _async_get_states( + hass: HomeAssistant, + utc_point_in_time: datetime, + entity_ids: list[str] | None = None, + run: RecorderRuns | None = None, + no_attributes: bool = False, +): + """Get states from the database.""" + + def _get_states_with_session(): + with session_scope(hass=hass) as session: + return history._get_states_with_session( + hass, session, utc_point_in_time, entity_ids, run, None, no_attributes + ) + + return await recorder.get_instance(hass).async_add_executor_job( + _get_states_with_session + ) + + def _add_db_entries( hass: ha.HomeAssistant, point: datetime, entity_ids: list[str] ) -> None: @@ -92,37 +117,6 @@ def _setup_get_states(hass): return now, future, states -def test_get_states(hass_recorder): - """Test getting states at a specific point in time.""" - hass = hass_recorder() - now, future, states = _setup_get_states(hass) - # 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) - - time_before_recorder_ran = now - timedelta(days=1000) - assert history.get_states(hass, time_before_recorder_ran) == [] - - assert history.get_state(hass, time_before_recorder_ran, "demo.id") is None - - def test_get_full_significant_states_with_session_entity_no_matches(hass_recorder): """Test getting states at a specific point in time for entities that never have been recorded.""" hass = hass_recorder() @@ -179,48 +173,6 @@ def test_significant_states_with_session_entity_minimal_response_no_matches( ) -def test_get_states_no_attributes(hass_recorder): - """Test getting states without attributes at a specific point in time.""" - hass = hass_recorder() - now, future, states = _setup_get_states(hass) - for state in states: - state.attributes = {} - - # Get states returns everything before POINT for all entities - for state1, state2 in zip( - states, - sorted( - history.get_states(hass, future, no_attributes=True), - 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, no_attributes=True), - 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, no_attributes=True - ) - - time_before_recorder_ran = now - timedelta(days=1000) - assert history.get_states(hass, time_before_recorder_ran, no_attributes=True) == [] - - assert ( - history.get_state(hass, time_before_recorder_ran, "demo.id", no_attributes=True) - is None - ) - - @pytest.mark.parametrize( "attributes, no_attributes, limit", [ @@ -653,7 +605,9 @@ async def test_state_changes_during_period_query_during_migration_to_schema_25( point = start + timedelta(seconds=1) end = point + timedelta(seconds=1) entity_id = "light.test" - await hass.async_add_executor_job(_add_db_entries, hass, point, [entity_id]) + await recorder.get_instance(hass).async_add_executor_job( + _add_db_entries, hass, point, [entity_id] + ) no_attributes = True hist = history.state_changes_during_period( @@ -701,15 +655,17 @@ async def test_get_states_query_during_migration_to_schema_25( point = start + timedelta(seconds=1) end = point + timedelta(seconds=1) entity_id = "light.test" - await hass.async_add_executor_job(_add_db_entries, hass, point, [entity_id]) + await recorder.get_instance(hass).async_add_executor_job( + _add_db_entries, hass, point, [entity_id] + ) no_attributes = True - hist = history.get_states(hass, end, [entity_id], no_attributes=no_attributes) + hist = await _async_get_states(hass, end, [entity_id], no_attributes=no_attributes) state = hist[0] assert state.attributes == {} no_attributes = False - hist = history.get_states(hass, end, [entity_id], no_attributes=no_attributes) + hist = await _async_get_states(hass, end, [entity_id], no_attributes=no_attributes) state = hist[0] assert state.attributes == {"name": "the shared light"} @@ -720,12 +676,16 @@ async def test_get_states_query_during_migration_to_schema_25( with patch.object(instance, "migration_in_progress", True): no_attributes = True - hist = history.get_states(hass, end, [entity_id], no_attributes=no_attributes) + hist = await _async_get_states( + hass, end, [entity_id], no_attributes=no_attributes + ) state = hist[0] assert state.attributes == {} no_attributes = False - hist = history.get_states(hass, end, [entity_id], no_attributes=no_attributes) + hist = await _async_get_states( + hass, end, [entity_id], no_attributes=no_attributes + ) state = hist[0] assert state.attributes == {"name": "the light"} @@ -744,15 +704,17 @@ async def test_get_states_query_during_migration_to_schema_25_multiple_entities( entity_id_2 = "switch.test" entity_ids = [entity_id_1, entity_id_2] - await hass.async_add_executor_job(_add_db_entries, hass, point, entity_ids) + await recorder.get_instance(hass).async_add_executor_job( + _add_db_entries, hass, point, entity_ids + ) no_attributes = True - hist = history.get_states(hass, end, entity_ids, no_attributes=no_attributes) + hist = await _async_get_states(hass, end, entity_ids, no_attributes=no_attributes) assert hist[0].attributes == {} assert hist[1].attributes == {} no_attributes = False - hist = history.get_states(hass, end, entity_ids, no_attributes=no_attributes) + hist = await _async_get_states(hass, end, entity_ids, no_attributes=no_attributes) assert hist[0].attributes == {"name": "the shared light"} assert hist[1].attributes == {"name": "the shared light"} @@ -763,11 +725,15 @@ async def test_get_states_query_during_migration_to_schema_25_multiple_entities( with patch.object(instance, "migration_in_progress", True): no_attributes = True - hist = history.get_states(hass, end, entity_ids, no_attributes=no_attributes) + hist = await _async_get_states( + hass, end, entity_ids, no_attributes=no_attributes + ) assert hist[0].attributes == {} assert hist[1].attributes == {} no_attributes = False - hist = history.get_states(hass, end, entity_ids, no_attributes=no_attributes) + hist = await _async_get_states( + hass, end, entity_ids, no_attributes=no_attributes + ) assert hist[0].attributes == {"name": "the light"} assert hist[1].attributes == {"name": "the light"}