1 """Describe Z-Wave JS logbook events."""
3 from __future__
import annotations
5 from collections.abc
import Callable
7 from zwave_js_server.const
import CommandClass
16 ATTR_COMMAND_CLASS_NAME,
24 ZWAVE_JS_NOTIFICATION_EVENT,
25 ZWAVE_JS_VALUE_NOTIFICATION_EVENT,
32 async_describe_event: Callable[[str, str, Callable[[Event], dict[str, str]]],
None],
34 """Describe logbook events."""
35 dev_reg = dr.async_get(hass)
38 def async_describe_zwave_js_notification_event(
41 """Describe Z-Wave JS notification event."""
42 device = dev_reg.devices[event.data[ATTR_DEVICE_ID]]
44 device_name = device.name_by_user
or device.name
47 command_class = event.data[ATTR_COMMAND_CLASS]
48 command_class_name = event.data[ATTR_COMMAND_CLASS_NAME]
50 data: dict[str, str] = {LOGBOOK_ENTRY_NAME: device_name}
51 prefix = f
"fired {command_class_name} CC 'notification' event"
53 if command_class == CommandClass.NOTIFICATION:
54 label = event.data[ATTR_LABEL]
55 event_label = event.data[ATTR_EVENT_LABEL]
58 LOGBOOK_ENTRY_MESSAGE: f
"{prefix} '{label}': '{event_label}'",
61 if command_class == CommandClass.ENTRY_CONTROL:
62 event_type = event.data[ATTR_EVENT_TYPE]
63 data_type = event.data[ATTR_DATA_TYPE]
66 LOGBOOK_ENTRY_MESSAGE: (
67 f
"{prefix} for event type '{event_type}' with data type "
72 if command_class == CommandClass.SWITCH_MULTILEVEL:
73 event_type = event.data[ATTR_EVENT_TYPE]
74 direction = event.data[ATTR_DIRECTION]
77 LOGBOOK_ENTRY_MESSAGE: (
78 f
"{prefix} for event type '{event_type}': '{direction}'"
82 return {**data, LOGBOOK_ENTRY_MESSAGE: prefix}
85 def async_describe_zwave_js_value_notification_event(
88 """Describe Z-Wave JS value notification event."""
89 device = dev_reg.devices[event.data[ATTR_DEVICE_ID]]
91 device_name = device.name_by_user
or device.name
94 command_class = event.data[ATTR_COMMAND_CLASS_NAME]
95 label = event.data[ATTR_LABEL]
96 value = event.data[ATTR_VALUE]
99 LOGBOOK_ENTRY_NAME: device_name,
100 LOGBOOK_ENTRY_MESSAGE: (
101 f
"fired {command_class} CC 'value notification' event for '{label}': "
106 async_describe_event(
107 DOMAIN, ZWAVE_JS_NOTIFICATION_EVENT, async_describe_zwave_js_notification_event
109 async_describe_event(
111 ZWAVE_JS_VALUE_NOTIFICATION_EVENT,
112 async_describe_zwave_js_value_notification_event,
None async_describe_events(HomeAssistant hass, Callable[[str, str, Callable[[Event], dict[str, str]]], None] async_describe_event)