Add notification events to zwave_js integration (#45827)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Raman Gupta 2021-02-02 02:37:42 -06:00 committed by GitHub
parent 411c0a9685
commit 3ef7bd6b73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 1 deletions

View File

@ -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()

View File

@ -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"

View File

@ -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