Home Assistant Unofficial Reference 2024.12.1
logbook.py
Go to the documentation of this file.
1 """Describe logbook events."""
2 
3 from collections.abc import Callable
4 from typing import Any
5 
7  LOGBOOK_ENTRY_CONTEXT_ID,
8  LOGBOOK_ENTRY_ENTITY_ID,
9  LOGBOOK_ENTRY_MESSAGE,
10  LOGBOOK_ENTRY_NAME,
11  LOGBOOK_ENTRY_SOURCE,
12  LazyEventPartialState,
13 )
14 from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME
15 from homeassistant.core import HomeAssistant, callback
16 
17 from . import ATTR_SOURCE, EVENT_AUTOMATION_TRIGGERED
18 from .const import DOMAIN
19 
20 
21 @callback
23  hass: HomeAssistant,
24  async_describe_event: Callable[
25  [str, str, Callable[[LazyEventPartialState], dict[str, Any]]], None
26  ],
27 ) -> None:
28  """Describe logbook events."""
29 
30  @callback
31  def async_describe_logbook_event(event: LazyEventPartialState) -> dict[str, Any]:
32  """Describe a logbook event."""
33  data = event.data
34  message = "triggered"
35  if ATTR_SOURCE in data:
36  message = f"{message} by {data[ATTR_SOURCE]}"
37 
38  return {
39  LOGBOOK_ENTRY_NAME: data.get(ATTR_NAME),
40  LOGBOOK_ENTRY_MESSAGE: message,
41  LOGBOOK_ENTRY_SOURCE: data.get(ATTR_SOURCE),
42  LOGBOOK_ENTRY_ENTITY_ID: data.get(ATTR_ENTITY_ID),
43  LOGBOOK_ENTRY_CONTEXT_ID: event.context_id,
44  }
45 
46  async_describe_event(
47  DOMAIN, EVENT_AUTOMATION_TRIGGERED, async_describe_logbook_event
48  )
None async_describe_events(HomeAssistant hass, Callable[[str, str, Callable[[LazyEventPartialState], dict[str, Any]]], None] async_describe_event)
Definition: logbook.py:27