Recorder improvements (#47739)

This commit is contained in:
Marc Mueller 2021-03-11 18:52:07 +01:00 committed by GitHub
parent af4d06b12e
commit 10848b9bdf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 207 additions and 77 deletions

View file

@ -1,14 +1,15 @@
"""Common test utils for working with recorder."""
from datetime import timedelta
from homeassistant import core as ha
from homeassistant.components import recorder
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.util import dt as dt_util
from tests.common import fire_time_changed
from tests.common import async_fire_time_changed, fire_time_changed
def wait_recording_done(hass):
def wait_recording_done(hass: HomeAssistantType) -> None:
"""Block till recording is done."""
hass.block_till_done()
trigger_db_commit(hass)
@ -17,18 +18,45 @@ def wait_recording_done(hass):
hass.block_till_done()
async def async_wait_recording_done(hass):
async def async_wait_recording_done_without_instance(hass: HomeAssistantType) -> None:
"""Block till recording is done."""
await hass.loop.run_in_executor(None, wait_recording_done, hass)
def trigger_db_commit(hass):
def trigger_db_commit(hass: HomeAssistantType) -> None:
"""Force the recorder to commit."""
for _ in range(recorder.DEFAULT_COMMIT_INTERVAL):
# We only commit on time change
fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1))
async def async_wait_recording_done(
hass: HomeAssistantType,
instance: recorder.Recorder,
) -> None:
"""Async wait until recording is done."""
await hass.async_block_till_done()
async_trigger_db_commit(hass)
await hass.async_block_till_done()
await async_recorder_block_till_done(hass, instance)
await hass.async_block_till_done()
@ha.callback
def async_trigger_db_commit(hass: HomeAssistantType) -> None:
"""Fore the recorder to commit. Async friendly."""
for _ in range(recorder.DEFAULT_COMMIT_INTERVAL):
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1))
async def async_recorder_block_till_done(
hass: HomeAssistantType,
instance: recorder.Recorder,
) -> None:
"""Non blocking version of recorder.block_till_done()."""
await hass.async_add_executor_job(instance.block_till_done)
def corrupt_db_file(test_db_file):
"""Corrupt an sqlite3 database file."""
with open(test_db_file, "w+") as fhandle: