1 """Helpers to check recorder."""
3 from __future__
import annotations
6 from collections.abc
import Callable, Generator
7 from contextlib
import contextmanager
8 from dataclasses
import dataclass, field
11 from typing
import TYPE_CHECKING, Any
17 from sqlalchemy.orm.session
import Session
21 _LOGGER = logging.getLogger(__name__)
23 DOMAIN: HassKey[RecorderData] =
HassKey(
"recorder")
24 DATA_INSTANCE: HassKey[Recorder] =
HassKey(
"recorder_instance")
27 @dataclass(slots=True)
29 """Recorder data stored in hass.data."""
31 recorder_platforms: dict[str, Any] = field(default_factory=dict)
32 db_connected: asyncio.Future[bool] = field(default_factory=asyncio.Future)
37 """Check to see if a recorder migration is in progress."""
41 return recorder.util.async_migration_in_progress(hass)
46 """Check to see if a recorder migration is live."""
50 return recorder.util.async_migration_is_live(hass)
55 """Initialize recorder data."""
64 """Wait for recorder to initialize and return connection status.
66 Returns False immediately if the recorder is not enabled.
68 if DOMAIN
not in hass.data:
70 return await hass.data[DOMAIN].db_connected
73 @functools.lru_cache(maxsize=1)
75 """Get the recorder instance."""
76 return hass.data[DATA_INSTANCE]
82 hass: HomeAssistant |
None =
None,
83 session: Session |
None =
None,
84 exception_filter: Callable[[Exception], bool] |
None =
None,
85 read_only: bool =
False,
86 ) -> Generator[Session]:
87 """Provide a transactional scope around a series of operations.
89 read_only is used to indicate that the session is only used for reading
90 data and that no commit is required. It does not prevent the session
91 from writing and is not a security measure.
93 if session
is None and hass
is not None:
97 raise RuntimeError(
"Session required")
102 if not read_only
and session.get_transaction():
105 except Exception
as err:
106 _LOGGER.exception(
"Error executing query")
109 if not exception_filter
or not exception_filter(err):
bool async_setup(HomeAssistant hass, ConfigType config)
Recorder get_instance(HomeAssistant hass)
bool async_migration_is_live(HomeAssistant hass)
None async_initialize_recorder(HomeAssistant hass)
bool async_wait_recorder(HomeAssistant hass)
Generator[Session] session_scope(*HomeAssistant|None hass=None, Session|None session=None, Callable[[Exception], bool]|None exception_filter=None, bool read_only=False)
bool async_migration_in_progress(HomeAssistant hass)