2021-04-12 14:18:38 -10:00
|
|
|
"""Helpers to check recorder."""
|
|
|
|
|
2022-07-22 15:11:34 +02:00
|
|
|
import asyncio
|
2022-08-28 22:18:35 -05:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from typing import Any
|
2021-04-12 14:18:38 -10:00
|
|
|
|
2022-07-22 15:11:34 +02:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-04-12 14:18:38 -10:00
|
|
|
|
2022-08-28 22:18:35 -05:00
|
|
|
DOMAIN = "recorder"
|
|
|
|
|
|
|
|
|
2023-04-11 13:58:28 -04:00
|
|
|
@dataclass(slots=True)
|
2022-08-28 22:18:35 -05:00
|
|
|
class RecorderData:
|
|
|
|
"""Recorder data stored in hass.data."""
|
|
|
|
|
|
|
|
recorder_platforms: dict[str, Any] = field(default_factory=dict)
|
|
|
|
db_connected: asyncio.Future = field(default_factory=asyncio.Future)
|
|
|
|
|
2021-04-12 14:18:38 -10:00
|
|
|
|
2021-11-05 10:40:56 +01:00
|
|
|
def async_migration_in_progress(hass: HomeAssistant) -> bool:
|
2021-04-12 14:18:38 -10:00
|
|
|
"""Check to see if a recorder migration is in progress."""
|
|
|
|
if "recorder" not in hass.config.components:
|
|
|
|
return False
|
2021-11-05 10:40:56 +01:00
|
|
|
# pylint: disable-next=import-outside-toplevel
|
|
|
|
from homeassistant.components import recorder
|
2021-04-12 14:18:38 -10:00
|
|
|
|
2021-11-05 10:40:56 +01:00
|
|
|
return recorder.util.async_migration_in_progress(hass)
|
2022-07-22 15:11:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_initialize_recorder(hass: HomeAssistant) -> None:
|
|
|
|
"""Initialize recorder data."""
|
2022-08-28 22:18:35 -05:00
|
|
|
hass.data[DOMAIN] = RecorderData()
|
2022-07-22 15:11:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def async_wait_recorder(hass: HomeAssistant) -> bool:
|
|
|
|
"""Wait for recorder to initialize and return connection status.
|
|
|
|
|
|
|
|
Returns False immediately if the recorder is not enabled.
|
|
|
|
"""
|
2022-08-28 22:18:35 -05:00
|
|
|
if DOMAIN not in hass.data:
|
2022-07-22 15:11:34 +02:00
|
|
|
return False
|
2022-08-28 22:18:35 -05:00
|
|
|
db_connected: asyncio.Future[bool] = hass.data[DOMAIN].db_connected
|
2022-07-22 15:11:34 +02:00
|
|
|
return await db_connected
|