77import tempfile
88from pathlib import Path
99from typing import TYPE_CHECKING
10- from unittest .mock import AsyncMock , patch
10+ from unittest .mock import AsyncMock , Mock , patch
1111
1212import homeassistant .core as ha
1313import homeassistant .util .dt as dt_util
@@ -56,13 +56,20 @@ async def async_setup(hass: HomeAssistant, config: ConfigType | None = None) ->
5656 await hass .async_block_till_done (wait_background_tasks = True )
5757
5858
59- async def async_next_day (hass : HomeAssistant , freezer : FrozenDateTimeFactory ) -> None :
60- """Jump to the next day and execute all pending timers."""
61- freezer .move_to (dt_util .now () + datetime .timedelta (days = 1 ))
59+ async def async_next_minutes (
60+ hass : HomeAssistant , freezer : FrozenDateTimeFactory , minutes : float = 1
61+ ) -> None :
62+ """Jump to the next minutes and execute all pending timers."""
63+ freezer .move_to (dt_util .now () + datetime .timedelta (minutes = minutes ))
6264 async_fire_time_changed (hass )
6365 await hass .async_block_till_done (wait_background_tasks = True )
6466
6567
68+ async def async_next_day (hass : HomeAssistant , freezer : FrozenDateTimeFactory ) -> None :
69+ """Jump to the next day and execute all pending timers."""
70+ await async_next_minutes (hass , freezer , 60 * 24 )
71+
72+
6673async def test_empty_config (
6774 hass : HomeAssistant , freezer : FrozenDateTimeFactory
6875) -> None :
@@ -78,7 +85,7 @@ async def test_empty_config(
7885)
7986@patch ("homeassistant.helpers.event.async_track_point_in_time" )
8087async def test_delay (
81- async_track_point_in_time_mock : AsyncMock ,
88+ async_track_point_in_time_mock : Mock ,
8289 hass : HomeAssistant ,
8390 freezer : FrozenDateTimeFactory ,
8491 delay : int | None ,
@@ -334,3 +341,27 @@ async def test_expand_path_config(
334341 ),
335342 )
336343 await async_next_day (hass , freezer )
344+
345+
346+ @patch ("homeassistant.helpers.recorder.async_migration_in_progress" )
347+ async def test_wait_for_recorder_migration (
348+ async_migration_in_progress_mock : Mock ,
349+ hass : HomeAssistant ,
350+ freezer : FrozenDateTimeFactory ,
351+ caplog : pytest .LogCaptureFixture ,
352+ ) -> None :
353+ """Test waiting for recorder migration to complete."""
354+
355+ def _delay_count (log : str ) -> int :
356+ return log .count ("Recorder migration in progress. Checking again in a minute." )
357+
358+ await async_setup (hass )
359+ async_migration_in_progress_mock .return_value = True
360+ await async_next_minutes (hass , freezer , DEFAULT_DELAY_SECONDS / 60 )
361+ assert _delay_count (caplog .text ) == 1
362+ for i in range (2 , 10 ):
363+ await async_next_minutes (hass , freezer )
364+ assert _delay_count (caplog .text ) == i
365+ async_migration_in_progress_mock .return_value = False
366+ await async_next_day (hass , freezer )
367+ assert _delay_count (caplog .text ) == i
0 commit comments