Skip to content

Commit 53c1f86

Browse files
committed
Wait for recorder migration
1 parent 72ebef9 commit 53c1f86

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

custom_components/patch/__init__.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from homeassistant.core import HomeAssistant, ServiceCall, callback
2424
from homeassistant.exceptions import IntegrationError
2525
from homeassistant.helpers import config_validation as cv
26-
from homeassistant.helpers import event
26+
from homeassistant.helpers import event, recorder
2727
from homeassistant.helpers import issue_registry as ir
2828

2929
from .const import (
@@ -116,7 +116,7 @@ async def async_reload(_: ServiceCall) -> None:
116116

117117
event.async_track_point_in_time(
118118
hass,
119-
Patch(hass, config[DOMAIN]).run,
119+
Patch(hass, config[DOMAIN]).run_after_migration,
120120
dt_util.now() + datetime.timedelta(seconds=config[DOMAIN][CONF_DELAY]),
121121
)
122122

@@ -132,7 +132,19 @@ def __init__(self, hass: HomeAssistant, config: ConfigType) -> None:
132132
self._config = config
133133

134134
@callback
135-
async def run(self, _: datetime.datetime | None = None) -> None:
135+
async def run_after_migration(self, _: datetime.datetime | None = None) -> None:
136+
"""Run if there is no migration in progress."""
137+
if recorder.async_migration_in_progress(self._hass):
138+
LOGGER.info("Recorder migration in progress. Checking again in a minute.")
139+
event.async_track_point_in_time(
140+
self._hass,
141+
self.run_after_migration,
142+
dt_util.now() + datetime.timedelta(minutes=1),
143+
)
144+
else:
145+
await self.run()
146+
147+
async def run(self) -> None:
136148
"""Execute."""
137149
updates = 0
138150
base_mismatch = []

tests/test_init.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import tempfile
88
from pathlib import Path
99
from typing import TYPE_CHECKING
10-
from unittest.mock import AsyncMock, patch
10+
from unittest.mock import AsyncMock, Mock, patch
1111

1212
import homeassistant.core as ha
1313
import 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+
6673
async 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")
8087
async 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

Comments
 (0)