Home Assistant Unofficial Reference 2024.12.1
logbook.py
Go to the documentation of this file.
1 """Describe homeassistant logbook events."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from typing import Any
7 
9  LOGBOOK_ENTRY_ICON,
10  LOGBOOK_ENTRY_MESSAGE,
11  LOGBOOK_ENTRY_NAME,
12 )
13 from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP
14 from homeassistant.core import Event, HomeAssistant, callback
15 from homeassistant.helpers.typing import NoEventData
16 from homeassistant.util.event_type import EventType
17 
18 from .const import DOMAIN
19 
20 EVENT_TO_NAME: dict[EventType[Any] | str, str] = {
21  EVENT_HOMEASSISTANT_STOP: "stopped",
22  EVENT_HOMEASSISTANT_START: "started",
23 }
24 
25 
26 @callback
28  hass: HomeAssistant,
29  async_describe_event: Callable[
30  [str, EventType[NoEventData] | str, Callable[[Event], dict[str, str]]], None
31  ],
32 ) -> None:
33  """Describe logbook events."""
34 
35  @callback
36  def async_describe_hass_event(event: Event[NoEventData]) -> dict[str, str]:
37  """Describe homeassistant logbook event."""
38  return {
39  LOGBOOK_ENTRY_NAME: "Home Assistant",
40  LOGBOOK_ENTRY_MESSAGE: EVENT_TO_NAME[event.event_type],
41  LOGBOOK_ENTRY_ICON: "mdi:home-assistant",
42  }
43 
44  async_describe_event(DOMAIN, EVENT_HOMEASSISTANT_STOP, async_describe_hass_event)
45  async_describe_event(DOMAIN, EVENT_HOMEASSISTANT_START, async_describe_hass_event)
None async_describe_events(HomeAssistant hass, Callable[[str, EventType[NoEventData]|str, Callable[[Event], dict[str, str]]], None] async_describe_event)
Definition: logbook.py:32