2020-10-06 19:24:13 +00:00
|
|
|
"""Common test tools."""
|
2021-03-11 17:52:07 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-12 07:04:17 +00:00
|
|
|
from collections.abc import AsyncGenerator, Awaitable, Callable
|
|
|
|
from typing import cast
|
2021-05-20 11:05:15 +00:00
|
|
|
from unittest.mock import patch
|
2020-10-06 19:24:13 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2021-05-20 11:05:15 +00:00
|
|
|
from homeassistant.components import recorder
|
2021-03-11 17:52:07 +00:00
|
|
|
from homeassistant.components.recorder import Recorder
|
2020-10-06 19:24:13 +00:00
|
|
|
from homeassistant.components.recorder.const import DATA_INSTANCE
|
2021-04-22 17:58:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2021-03-11 17:52:07 +00:00
|
|
|
|
|
|
|
from .common import async_recorder_block_till_done
|
2020-10-06 19:24:13 +00:00
|
|
|
|
2021-05-20 11:05:15 +00:00
|
|
|
from tests.common import async_init_recorder_component
|
2021-03-11 17:52:07 +00:00
|
|
|
|
|
|
|
SetupRecorderInstanceT = Callable[..., Awaitable[Recorder]]
|
2020-10-06 19:24:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2021-05-20 11:05:15 +00:00
|
|
|
async def async_setup_recorder_instance(
|
|
|
|
enable_statistics,
|
|
|
|
) -> AsyncGenerator[SetupRecorderInstanceT, None]:
|
2021-03-11 17:52:07 +00:00
|
|
|
"""Yield callable to setup recorder instance."""
|
|
|
|
|
|
|
|
async def async_setup_recorder(
|
2021-04-22 17:58:02 +00:00
|
|
|
hass: HomeAssistant, config: ConfigType | None = None
|
2021-03-11 17:52:07 +00:00
|
|
|
) -> Recorder:
|
|
|
|
"""Setup and return recorder instance.""" # noqa: D401
|
2021-09-16 08:57:15 +00:00
|
|
|
stats = (
|
|
|
|
recorder.Recorder.async_periodic_statistics if enable_statistics else None
|
|
|
|
)
|
2021-05-20 11:05:15 +00:00
|
|
|
with patch(
|
2021-09-16 08:57:15 +00:00
|
|
|
"homeassistant.components.recorder.Recorder.async_periodic_statistics",
|
2021-05-20 11:05:15 +00:00
|
|
|
side_effect=stats,
|
|
|
|
autospec=True,
|
|
|
|
):
|
|
|
|
await async_init_recorder_component(hass, config)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
instance = cast(Recorder, hass.data[DATA_INSTANCE])
|
|
|
|
await async_recorder_block_till_done(hass, instance)
|
|
|
|
assert isinstance(instance, Recorder)
|
|
|
|
return instance
|
2021-03-11 17:52:07 +00:00
|
|
|
|
|
|
|
yield async_setup_recorder
|