Home Assistant Unofficial Reference 2024.12.1
logbook.py
Go to the documentation of this file.
1 """Describe Z-Wave JS logbook events."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 
7 from zwave_js_server.const import CommandClass
8 
9 from homeassistant.components.logbook import LOGBOOK_ENTRY_MESSAGE, LOGBOOK_ENTRY_NAME
10 from homeassistant.const import ATTR_DEVICE_ID
11 from homeassistant.core import Event, HomeAssistant, callback
13 
14 from .const import (
15  ATTR_COMMAND_CLASS,
16  ATTR_COMMAND_CLASS_NAME,
17  ATTR_DATA_TYPE,
18  ATTR_DIRECTION,
19  ATTR_EVENT_LABEL,
20  ATTR_EVENT_TYPE,
21  ATTR_LABEL,
22  ATTR_VALUE,
23  DOMAIN,
24  ZWAVE_JS_NOTIFICATION_EVENT,
25  ZWAVE_JS_VALUE_NOTIFICATION_EVENT,
26 )
27 
28 
29 @callback
31  hass: HomeAssistant,
32  async_describe_event: Callable[[str, str, Callable[[Event], dict[str, str]]], None],
33 ) -> None:
34  """Describe logbook events."""
35  dev_reg = dr.async_get(hass)
36 
37  @callback
38  def async_describe_zwave_js_notification_event(
39  event: Event,
40  ) -> dict[str, str]:
41  """Describe Z-Wave JS notification event."""
42  device = dev_reg.devices[event.data[ATTR_DEVICE_ID]]
43  # Z-Wave JS devices always have a name
44  device_name = device.name_by_user or device.name
45  assert device_name
46 
47  command_class = event.data[ATTR_COMMAND_CLASS]
48  command_class_name = event.data[ATTR_COMMAND_CLASS_NAME]
49 
50  data: dict[str, str] = {LOGBOOK_ENTRY_NAME: device_name}
51  prefix = f"fired {command_class_name} CC 'notification' event"
52 
53  if command_class == CommandClass.NOTIFICATION:
54  label = event.data[ATTR_LABEL]
55  event_label = event.data[ATTR_EVENT_LABEL]
56  return {
57  **data,
58  LOGBOOK_ENTRY_MESSAGE: f"{prefix} '{label}': '{event_label}'",
59  }
60 
61  if command_class == CommandClass.ENTRY_CONTROL:
62  event_type = event.data[ATTR_EVENT_TYPE]
63  data_type = event.data[ATTR_DATA_TYPE]
64  return {
65  **data,
66  LOGBOOK_ENTRY_MESSAGE: (
67  f"{prefix} for event type '{event_type}' with data type "
68  f"'{data_type}'"
69  ),
70  }
71 
72  if command_class == CommandClass.SWITCH_MULTILEVEL:
73  event_type = event.data[ATTR_EVENT_TYPE]
74  direction = event.data[ATTR_DIRECTION]
75  return {
76  **data,
77  LOGBOOK_ENTRY_MESSAGE: (
78  f"{prefix} for event type '{event_type}': '{direction}'"
79  ),
80  }
81 
82  return {**data, LOGBOOK_ENTRY_MESSAGE: prefix}
83 
84  @callback
85  def async_describe_zwave_js_value_notification_event(
86  event: Event,
87  ) -> dict[str, str]:
88  """Describe Z-Wave JS value notification event."""
89  device = dev_reg.devices[event.data[ATTR_DEVICE_ID]]
90  # Z-Wave JS devices always have a name
91  device_name = device.name_by_user or device.name
92  assert device_name
93 
94  command_class = event.data[ATTR_COMMAND_CLASS_NAME]
95  label = event.data[ATTR_LABEL]
96  value = event.data[ATTR_VALUE]
97 
98  return {
99  LOGBOOK_ENTRY_NAME: device_name,
100  LOGBOOK_ENTRY_MESSAGE: (
101  f"fired {command_class} CC 'value notification' event for '{label}': "
102  f"'{value}'"
103  ),
104  }
105 
106  async_describe_event(
107  DOMAIN, ZWAVE_JS_NOTIFICATION_EVENT, async_describe_zwave_js_notification_event
108  )
109  async_describe_event(
110  DOMAIN,
111  ZWAVE_JS_VALUE_NOTIFICATION_EVENT,
112  async_describe_zwave_js_value_notification_event,
113  )
None async_describe_events(HomeAssistant hass, Callable[[str, str, Callable[[Event], dict[str, str]]], None] async_describe_event)
Definition: logbook.py:33