1 """Describe ZHA logbook events."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from typing
import TYPE_CHECKING
8 from zha.application.const
import ZHA_EVENT
15 from .const
import DOMAIN
as ZHA_DOMAIN
16 from .helpers
import async_get_zha_device_proxy
19 from zha.zigbee.device
import Device
25 async_describe_event: Callable[[str, str, Callable[[Event], dict[str, str]]],
None],
27 """Describe logbook events."""
28 device_registry = dr.async_get(hass)
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
41 device = device_registry.devices[event.data[ATTR_DEVICE_ID]]
43 device_name = device.name_by_user
or device.name
or "Unknown device"
45 hass, event.data[ATTR_DEVICE_ID]
47 except (KeyError, AttributeError):
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))
56 all_triggers = zha_device.device_automation_triggers
57 for etype_subtype
in etype_subtypes:
58 trigger = all_triggers[etype_subtype]
60 event_data.get(key) == value
for key, value
in trigger.items()
63 event_type, event_subtype = etype_subtype
66 if event_type
is None:
67 event_type = event_data.get(ATTR_COMMAND, ZHA_EVENT)
69 if event_subtype
is not None and event_subtype != event_type:
70 event_type = f
"{event_type} - {event_subtype}"
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"
77 message = f
"{event_type} event was fired"
79 if params := event_data.get(
"params"):
80 message = f
"{message} with parameters: {params}"
83 LOGBOOK_ENTRY_NAME: device_name,
84 LOGBOOK_ENTRY_MESSAGE: message,
87 async_describe_event(ZHA_DOMAIN, ZHA_EVENT, async_describe_zha_event)
ZHADeviceProxy async_get_zha_device_proxy(HomeAssistant hass, str device_id)
None async_describe_events(HomeAssistant hass, Callable[[str, str, Callable[[Event], dict[str, str]]], None] async_describe_event)