2021-05-31 23:35:31 +00:00
|
|
|
"""Helpers to help during startup."""
|
2021-09-29 14:32:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Awaitable, Callable
|
2021-05-31 23:35:31 +00:00
|
|
|
|
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
2022-01-05 17:04:09 +00:00
|
|
|
from homeassistant.core import Event, HassJob, HomeAssistant, callback
|
2021-05-31 23:35:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_at_start(
|
2022-01-05 17:04:09 +00:00
|
|
|
hass: HomeAssistant, at_start_cb: Callable[[HomeAssistant], Awaitable[None] | None]
|
2021-05-31 23:35:31 +00:00
|
|
|
) -> None:
|
|
|
|
"""Execute something when Home Assistant is started.
|
|
|
|
|
|
|
|
Will execute it now if Home Assistant is already started.
|
|
|
|
"""
|
2022-01-05 17:04:09 +00:00
|
|
|
at_start_job = HassJob(at_start_cb)
|
2021-05-31 23:35:31 +00:00
|
|
|
if hass.is_running:
|
2022-01-05 17:04:09 +00:00
|
|
|
hass.async_run_hass_job(at_start_job, hass)
|
2021-05-31 23:35:31 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
async def _matched_event(event: Event) -> None:
|
|
|
|
"""Call the callback when Home Assistant started."""
|
2022-01-05 17:04:09 +00:00
|
|
|
hass.async_run_hass_job(at_start_job, hass)
|
2021-05-31 23:35:31 +00:00
|
|
|
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _matched_event)
|