hass-core/homeassistant/helpers/start.py
Erik Montnemery 6d57dbde68
Support passing callbacks to start.async_at_start (#63473)
* Support passing callbacks to start.async_at_start

* Update homeassistant/helpers/start.py

Co-authored-by: Franck Nijhof <git@frenck.dev>

* Fix imports

Co-authored-by: Franck Nijhof <git@frenck.dev>
2022-01-05 18:04:09 +01:00

27 lines
883 B
Python

"""Helpers to help during startup."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from homeassistant.const import EVENT_HOMEASSISTANT_START
from homeassistant.core import Event, HassJob, HomeAssistant, callback
@callback
def async_at_start(
hass: HomeAssistant, at_start_cb: Callable[[HomeAssistant], Awaitable[None] | None]
) -> None:
"""Execute something when Home Assistant is started.
Will execute it now if Home Assistant is already started.
"""
at_start_job = HassJob(at_start_cb)
if hass.is_running:
hass.async_run_hass_job(at_start_job, hass)
return
async def _matched_event(event: Event) -> None:
"""Call the callback when Home Assistant started."""
hass.async_run_hass_job(at_start_job, hass)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _matched_event)