diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index 2c58e7aae1533c37d6ac51e3dcd20b524e27b4a5..e1812de44d38a86b43534887c314f412b3e4fdac 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -107,6 +107,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = { "device_registry": "DeviceRegistry", "enable_bluetooth": "None", "enable_custom_integrations": "None", + "enable_missing_statistics": "bool", "enable_nightly_purge": "bool", "enable_statistics": "bool", "enable_schema_validation": "bool", diff --git a/tests/common.py b/tests/common.py index ecf8c4f5418c43c33ba427b7a443e3769b9fec0e..093d2b5c5e1d095623e6389f698c618ed61b80fb 100644 --- a/tests/common.py +++ b/tests/common.py @@ -251,6 +251,7 @@ async def async_test_home_assistant( event_loop: asyncio.AbstractEventLoop | None = None, load_registries: bool = True, config_dir: str | None = None, + initial_state: CoreState = CoreState.running, ) -> AsyncGenerator[HomeAssistant]: """Return a Home Assistant object pointing at test config dir.""" hass = HomeAssistant(config_dir or get_test_config_dir()) @@ -378,7 +379,7 @@ async def async_test_home_assistant( await rs.async_load(hass) hass.data[bootstrap.DATA_REGISTRIES_LOADED] = None - hass.set_state(CoreState.running) + hass.set_state(initial_state) @callback def clear_instance(event): diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index c8e58d58105d9d74c2a063c5979a52933a53a833..74b0adc1b8bfa59f712aed6e0202f58ad7b6c82f 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -1366,7 +1366,7 @@ async def test_statistics_runs_initiated( @pytest.mark.freeze_time("2022-09-13 09:00:00+02:00") @pytest.mark.parametrize("persistent_database", [True]) -@pytest.mark.parametrize("enable_statistics", [True]) +@pytest.mark.parametrize("enable_missing_statistics", [True]) @pytest.mark.usefixtures("hass_storage") # Prevent test hass from writing to storage async def test_compile_missing_statistics( async_test_recorder: RecorderInstanceGenerator, freezer: FrozenDateTimeFactory diff --git a/tests/components/sensor/test_recorder_missing_stats.py b/tests/components/sensor/test_recorder_missing_stats.py index d770c459426e21d97b254c796ea23fef4b3ccdc6..43e18b89e7250ec93a873f371d80a5f308a4f694 100644 --- a/tests/components/sensor/test_recorder_missing_stats.py +++ b/tests/components/sensor/test_recorder_missing_stats.py @@ -1,7 +1,6 @@ """The tests for sensor recorder platform can catch up.""" from datetime import datetime, timedelta -from pathlib import Path import threading from unittest.mock import patch @@ -17,11 +16,15 @@ from homeassistant.components.recorder.statistics import ( from homeassistant.components.recorder.util import session_scope from homeassistant.core import CoreState from homeassistant.helpers import recorder as recorder_helper -from homeassistant.setup import setup_component +from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util -from tests.common import get_test_home_assistant -from tests.components.recorder.common import do_adhoc_statistics, wait_recording_done +from tests.common import async_test_home_assistant +from tests.components.recorder.common import ( + async_wait_recording_done, + do_adhoc_statistics, +) +from tests.typing import RecorderInstanceGenerator POWER_SENSOR_ATTRIBUTES = { "device_class": "energy", @@ -40,37 +43,34 @@ def disable_db_issue_creation(): @pytest.mark.timeout(25) -def test_compile_missing_statistics( - freezer: FrozenDateTimeFactory, recorder_db_url: str, tmp_path: Path +@pytest.mark.parametrize("persistent_database", [True]) +@pytest.mark.parametrize("enable_missing_statistics", [True]) +@pytest.mark.usefixtures("hass_storage") # Prevent test hass from writing to storage +async def test_compile_missing_statistics( + async_test_recorder: RecorderInstanceGenerator, freezer: FrozenDateTimeFactory ) -> None: """Test compile missing statistics.""" - if recorder_db_url == "sqlite://": - # On-disk database because we need to stop and start hass - # and have it persist. - recorder_db_url = "sqlite:///" + str(tmp_path / "pytest.db") - config = { - "db_url": recorder_db_url, - } three_days_ago = datetime(2021, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC) start_time = three_days_ago + timedelta(days=3) freezer.move_to(three_days_ago) - with get_test_home_assistant() as hass: - hass.set_state(CoreState.not_running) + async with ( + async_test_home_assistant(initial_state=CoreState.not_running) as hass, + async_test_recorder(hass, wait_recorder=False), + ): recorder_helper.async_initialize_recorder(hass) - setup_component(hass, "sensor", {}) - setup_component(hass, "recorder", {"recorder": config}) + await async_setup_component(hass, "sensor", {}) get_instance(hass).recorder_and_worker_thread_ids.add(threading.get_ident()) - hass.start() - wait_recording_done(hass) - wait_recording_done(hass) + await hass.async_start() + await async_wait_recording_done(hass) + await async_wait_recording_done(hass) - hass.states.set("sensor.test1", "0", POWER_SENSOR_ATTRIBUTES) - wait_recording_done(hass) + hass.states.async_set("sensor.test1", "0", POWER_SENSOR_ATTRIBUTES) + await async_wait_recording_done(hass) two_days_ago = three_days_ago + timedelta(days=1) freezer.move_to(two_days_ago) do_adhoc_statistics(hass, start=two_days_ago) - wait_recording_done(hass) + await async_wait_recording_done(hass) with session_scope(hass=hass, read_only=True) as session: latest = get_latest_short_term_statistics_with_session( hass, session, {"sensor.test1"}, {"state", "sum"} @@ -82,29 +82,32 @@ def test_compile_missing_statistics( past_time = two_days_ago while past_time <= start_time: freezer.move_to(past_time) - hass.states.set("sensor.test1", str(count), POWER_SENSOR_ATTRIBUTES) + hass.states.async_set("sensor.test1", str(count), POWER_SENSOR_ATTRIBUTES) past_time += timedelta(minutes=5) count += 1 - wait_recording_done(hass) + await async_wait_recording_done(hass) states = get_significant_states( hass, three_days_ago, past_time, ["sensor.test1"] ) assert len(states["sensor.test1"]) == 577 - hass.stop() + await hass.async_stop() + await hass.async_block_till_done() + freezer.move_to(start_time) - with get_test_home_assistant() as hass: - hass.set_state(CoreState.not_running) + async with ( + async_test_home_assistant(initial_state=CoreState.not_running) as hass, + async_test_recorder(hass, wait_recorder=False), + ): recorder_helper.async_initialize_recorder(hass) - setup_component(hass, "sensor", {}) - hass.states.set("sensor.test1", "0", POWER_SENSOR_ATTRIBUTES) - setup_component(hass, "recorder", {"recorder": config}) + await async_setup_component(hass, "sensor", {}) + hass.states.async_set("sensor.test1", "0", POWER_SENSOR_ATTRIBUTES) get_instance(hass).recorder_and_worker_thread_ids.add(threading.get_ident()) - hass.start() - wait_recording_done(hass) - wait_recording_done(hass) + await hass.async_start() + await async_wait_recording_done(hass) + await async_wait_recording_done(hass) with session_scope(hass=hass, read_only=True) as session: latest = get_latest_short_term_statistics_with_session( hass, session, {"sensor.test1"}, {"state", "sum", "max", "mean", "min"} @@ -128,4 +131,4 @@ def test_compile_missing_statistics( assert len(stats["sensor.test1"]) == 48 # Make sure the last mean is 570.5 assert stats["sensor.test1"][-1]["mean"] == 570.5 - hass.stop() + await hass.async_stop() diff --git a/tests/conftest.py b/tests/conftest.py index ea0453e745078bd3a4e0f88da476c78de8ca34f3..df183f955cba3054f96273ea92c6f4de5ac42fb2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1262,6 +1262,16 @@ def enable_statistics() -> bool: return False +@pytest.fixture +def enable_missing_statistics() -> bool: + """Fixture to control enabling of recorder's statistics compilation. + + To enable statistics, tests can be marked with: + @pytest.mark.parametrize("enable_missing_statistics", [True]) + """ + return False + + @pytest.fixture def enable_schema_validation() -> bool: """Fixture to control enabling of recorder's statistics table validation. @@ -1453,6 +1463,7 @@ async def async_test_recorder( recorder_db_url: str, enable_nightly_purge: bool, enable_statistics: bool, + enable_missing_statistics: bool, enable_schema_validation: bool, enable_migrate_context_ids: bool, enable_migrate_event_type_ids: bool, @@ -1511,7 +1522,7 @@ async def async_test_recorder( ) compile_missing = ( recorder.Recorder._schedule_compile_missing_statistics - if enable_statistics + if enable_missing_statistics else None ) migrate_states_context_ids = (