Home Assistant Unofficial Reference 2024.12.1
logbook.py
Go to the documentation of this file.
1 """Describe ZHA logbook events."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from typing import TYPE_CHECKING
7 
8 from zha.application.const import ZHA_EVENT
9 
10 from homeassistant.components.logbook import LOGBOOK_ENTRY_MESSAGE, LOGBOOK_ENTRY_NAME
11 from homeassistant.const import ATTR_COMMAND, ATTR_DEVICE_ID
12 from homeassistant.core import Event, HomeAssistant, callback
14 
15 from .const import DOMAIN as ZHA_DOMAIN
16 from .helpers import async_get_zha_device_proxy
17 
18 if TYPE_CHECKING:
19  from zha.zigbee.device import Device
20 
21 
22 @callback
24  hass: HomeAssistant,
25  async_describe_event: Callable[[str, str, Callable[[Event], dict[str, str]]], None],
26 ) -> None:
27  """Describe logbook events."""
28  device_registry = dr.async_get(hass)
29 
30  @callback
31  def async_describe_zha_event(event: Event) -> dict[str, str]:
32  """Describe ZHA logbook event."""
33  device: dr.DeviceEntry | None = None
34  device_name: str = "Unknown device"
35  zha_device: Device | None = None
36  event_data = event.data
37  event_type: str | None = None
38  event_subtype: str | None = None
39 
40  try:
41  device = device_registry.devices[event.data[ATTR_DEVICE_ID]]
42  if device:
43  device_name = device.name_by_user or device.name or "Unknown device"
44  zha_device = async_get_zha_device_proxy(
45  hass, event.data[ATTR_DEVICE_ID]
46  ).device
47  except (KeyError, AttributeError):
48  pass
49 
50  if (
51  zha_device
52  and (command := event_data.get(ATTR_COMMAND))
53  and (command_to_etype_subtype := zha_device.device_automation_commands)
54  and (etype_subtypes := command_to_etype_subtype.get(command))
55  ):
56  all_triggers = zha_device.device_automation_triggers
57  for etype_subtype in etype_subtypes:
58  trigger = all_triggers[etype_subtype]
59  if not all(
60  event_data.get(key) == value for key, value in trigger.items()
61  ):
62  continue
63  event_type, event_subtype = etype_subtype
64  break
65 
66  if event_type is None:
67  event_type = event_data.get(ATTR_COMMAND, ZHA_EVENT)
68 
69  if event_subtype is not None and event_subtype != event_type:
70  event_type = f"{event_type} - {event_subtype}"
71 
72  if event_type is not None:
73  event_type = event_type.replace("_", " ").title()
74  if "event" in event_type.lower():
75  message = f"{event_type} was fired"
76  else:
77  message = f"{event_type} event was fired"
78 
79  if params := event_data.get("params"):
80  message = f"{message} with parameters: {params}"
81 
82  return {
83  LOGBOOK_ENTRY_NAME: device_name,
84  LOGBOOK_ENTRY_MESSAGE: message,
85  }
86 
87  async_describe_event(ZHA_DOMAIN, ZHA_EVENT, async_describe_zha_event)
ZHADeviceProxy async_get_zha_device_proxy(HomeAssistant hass, str device_id)
Definition: helpers.py:1053
None async_describe_events(HomeAssistant hass, Callable[[str, str, Callable[[Event], dict[str, str]]], None] async_describe_event)
Definition: logbook.py:26