Home Assistant Unofficial Reference 2024.12.1
logbook.py
Go to the documentation of this file.
1 """Describe assist_pipeline logbook events."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 
7 from homeassistant.components.logbook import LOGBOOK_ENTRY_MESSAGE, LOGBOOK_ENTRY_NAME
8 from homeassistant.const import ATTR_DEVICE_ID
9 from homeassistant.core import Event, HomeAssistant, callback
11 
12 from .const import DOMAIN, EVENT_RECORDING
13 
14 
15 @callback
17  hass: HomeAssistant,
18  async_describe_event: Callable[[str, str, Callable[[Event], dict[str, str]]], None],
19 ) -> None:
20  """Describe logbook events."""
21  device_registry = dr.async_get(hass)
22 
23  @callback
24  def async_describe_logbook_event(event: Event) -> dict[str, str]:
25  """Describe logbook event."""
26  device: dr.DeviceEntry | None = None
27  device_name: str = "Unknown device"
28 
29  device = device_registry.devices[event.data[ATTR_DEVICE_ID]]
30  if device:
31  device_name = device.name_by_user or device.name or "Unknown device"
32 
33  message = f"{device_name} captured an audio sample"
34 
35  return {
36  LOGBOOK_ENTRY_NAME: device_name,
37  LOGBOOK_ENTRY_MESSAGE: message,
38  }
39 
40  async_describe_event(DOMAIN, EVENT_RECORDING, async_describe_logbook_event)
None async_describe_events(HomeAssistant hass, Callable[[str, str, Callable[[Event], dict[str, str]]], None] async_describe_event)
Definition: logbook.py:19