Home Assistant Unofficial Reference 2024.12.1
start.py
Go to the documentation of this file.
1 """Helpers to help during startup."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from typing import Any
7 
8 from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STARTED
9 from homeassistant.core import (
10  CALLBACK_TYPE,
11  CoreState,
12  Event,
13  HassJob,
14  HomeAssistant,
15  callback,
16 )
17 from homeassistant.util.event_type import EventType
18 
19 from .typing import NoEventData
20 
21 
22 @callback
24  hass: HomeAssistant,
25  at_start_cb: Callable[[HomeAssistant], Coroutine[Any, Any, None] | None],
26  event_type: EventType[NoEventData],
27  check_state: Callable[[HomeAssistant], bool],
28 ) -> CALLBACK_TYPE:
29  """Execute a job at_start_cb when Home Assistant has the wanted state.
30 
31  The job is executed immediately if Home Assistant is in the wanted state.
32  Will wait for event specified by event_type if it isn't.
33  """
34  at_start_job = HassJob(at_start_cb)
35  if check_state(hass):
36  hass.async_run_hass_job(at_start_job, hass)
37  return lambda: None
38 
39  unsub: CALLBACK_TYPE | None = None
40 
41  @callback
42  def _matched_event(event: Event) -> None:
43  """Call the callback when Home Assistant started."""
44  hass.async_run_hass_job(at_start_job, hass)
45  nonlocal unsub
46  unsub = None
47 
48  @callback
49  def cancel() -> None:
50  if unsub:
51  unsub()
52 
53  unsub = hass.bus.async_listen_once(event_type, _matched_event)
54  return cancel
55 
56 
57 @callback
59  hass: HomeAssistant,
60  at_start_cb: Callable[[HomeAssistant], Coroutine[Any, Any, None] | None],
61 ) -> CALLBACK_TYPE:
62  """Execute a job at_start_cb when Home Assistant is starting.
63 
64  The job is executed immediately if Home Assistant is already starting or started.
65  Will wait for EVENT_HOMEASSISTANT_START if it isn't.
66  """
67 
68  def _is_running(hass: HomeAssistant) -> bool:
69  return hass.is_running
70 
71  return _async_at_core_state(
72  hass, at_start_cb, EVENT_HOMEASSISTANT_START, _is_running
73  )
74 
75 
76 @callback
78  hass: HomeAssistant,
79  at_start_cb: Callable[[HomeAssistant], Coroutine[Any, Any, None] | None],
80 ) -> CALLBACK_TYPE:
81  """Execute a job at_start_cb when Home Assistant has started.
82 
83  The job is executed immediately if Home Assistant is already started.
84  Will wait for EVENT_HOMEASSISTANT_STARTED if it isn't.
85  """
86 
87  def _is_started(hass: HomeAssistant) -> bool:
88  return hass.state is CoreState.running
89 
90  return _async_at_core_state(
91  hass, at_start_cb, EVENT_HOMEASSISTANT_STARTED, _is_started
92  )
CALLBACK_TYPE _async_at_core_state(HomeAssistant hass, Callable[[HomeAssistant], Coroutine[Any, Any, None]|None] at_start_cb, EventType[NoEventData] event_type, Callable[[HomeAssistant], bool] check_state)
Definition: start.py:28
CALLBACK_TYPE async_at_started(HomeAssistant hass, Callable[[HomeAssistant], Coroutine[Any, Any, None]|None] at_start_cb)
Definition: start.py:80
CALLBACK_TYPE async_at_start(HomeAssistant hass, Callable[[HomeAssistant], Coroutine[Any, Any, None]|None] at_start_cb)
Definition: start.py:61