From 3ef7bd6b73d518bae6eefe7cfddd7d483d336b5f Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Tue, 2 Feb 2021 02:37:42 -0600 Subject: [PATCH] Add notification events to zwave_js integration (#45827) Co-authored-by: Paulus Schoutsen --- homeassistant/components/zwave_js/__init__.py | 25 ++++++++++++++++- homeassistant/components/zwave_js/const.py | 1 + tests/components/zwave_js/test_events.py | 28 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/zwave_js/__init__.py b/homeassistant/components/zwave_js/__init__.py index 2b4b33e9b88..a4eb466fe87 100644 --- a/homeassistant/components/zwave_js/__init__.py +++ b/homeassistant/components/zwave_js/__init__.py @@ -5,6 +5,7 @@ import logging from async_timeout import timeout from zwave_js_server.client import Client as ZwaveClient from zwave_js_server.model.node import Node as ZwaveNode +from zwave_js_server.model.notification import Notification from zwave_js_server.model.value import ValueNotification from homeassistant.components.hassio.handler import HassioAPIError @@ -26,6 +27,7 @@ from .const import ( ATTR_HOME_ID, ATTR_LABEL, ATTR_NODE_ID, + ATTR_PARAMETERS, ATTR_PROPERTY_KEY_NAME, ATTR_PROPERTY_NAME, ATTR_TYPE, @@ -114,11 +116,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async_dispatcher_send( hass, f"{DOMAIN}_{entry.entry_id}_add_{disc_info.platform}", disc_info ) - # add listener for stateless node events (value notification) + # add listener for stateless node value notification events node.on( "value notification", lambda event: async_on_value_notification(event["value_notification"]), ) + # add listener for stateless node notification events + node.on( + "notification", lambda event: async_on_notification(event["notification"]) + ) @callback def async_on_node_added(node: ZwaveNode) -> None: @@ -172,6 +178,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: }, ) + @callback + def async_on_notification(notification: Notification) -> None: + """Relay stateless notification events from Z-Wave nodes to hass.""" + device = dev_reg.async_get_device({get_device_id(client, notification.node)}) + hass.bus.async_fire( + ZWAVE_JS_EVENT, + { + ATTR_TYPE: "notification", + ATTR_DOMAIN: DOMAIN, + ATTR_NODE_ID: notification.node.node_id, + ATTR_HOME_ID: client.driver.controller.home_id, + ATTR_DEVICE_ID: device.id, + ATTR_LABEL: notification.notification_label, + ATTR_PARAMETERS: notification.parameters, + }, + ) + async def handle_ha_shutdown(event: Event) -> None: """Handle HA shutdown.""" await client.disconnect() diff --git a/homeassistant/components/zwave_js/const.py b/homeassistant/components/zwave_js/const.py index 163f4fff9ac..dc2ffaeaa20 100644 --- a/homeassistant/components/zwave_js/const.py +++ b/homeassistant/components/zwave_js/const.py @@ -32,3 +32,4 @@ ATTR_DOMAIN = "domain" ATTR_DEVICE_ID = "device_id" ATTR_PROPERTY_NAME = "property_name" ATTR_PROPERTY_KEY_NAME = "property_key_name" +ATTR_PARAMETERS = "parameters" diff --git a/tests/components/zwave_js/test_events.py b/tests/components/zwave_js/test_events.py index c4280ebb50d..2a347f6afea 100644 --- a/tests/components/zwave_js/test_events.py +++ b/tests/components/zwave_js/test_events.py @@ -128,3 +128,31 @@ async def test_scenes(hass, hank_binary_switch, integration, client): assert events[2].data["command_class_name"] == "Central Scene" assert events[2].data["label"] == "Scene 001" assert events[2].data["value"] == "KeyPressed3x" + + +async def test_notifications(hass, hank_binary_switch, integration, client): + """Test notification events.""" + # just pick a random node to fake the value notification events + node = hank_binary_switch + events = async_capture_events(hass, "zwave_js_event") + + # Publish fake Basic Set value notification + event = Event( + type="notification", + data={ + "source": "node", + "event": "notification", + "nodeId": 23, + "notificationLabel": "Keypad lock operation", + "parameters": {"userId": 1}, + }, + ) + node.receive_event(event) + # wait for the event + await hass.async_block_till_done() + assert len(events) == 1 + assert events[0].data["type"] == "notification" + assert events[0].data["home_id"] == client.driver.controller.home_id + assert events[0].data["node_id"] == 32 + assert events[0].data["label"] == "Keypad lock operation" + assert events[0].data["parameters"]["userId"] == 1