From 7714183118780faf8b6d41ceaeb6d34c596164dd Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Sun, 19 Jun 2022 10:09:26 -0400 Subject: [PATCH] Add `zwave_js/subscribe_node_status` WS API cmd (#73249) * Add zwave_js/subscribe_node_status WS API cmd * add ready to event --- homeassistant/components/zwave_js/api.py | 18 +++++++++++------ tests/components/zwave_js/test_api.py | 25 +++++++++++++++++++++--- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/zwave_js/api.py b/homeassistant/components/zwave_js/api.py index 6a4aaf0264e..b41c6bf2f92 100644 --- a/homeassistant/components/zwave_js/api.py +++ b/homeassistant/components/zwave_js/api.py @@ -378,6 +378,7 @@ def node_status(node: Node) -> dict[str, Any]: def async_register_api(hass: HomeAssistant) -> None: """Register all of our api endpoints.""" websocket_api.async_register_command(hass, websocket_network_status) + websocket_api.async_register_command(hass, websocket_subscribe_node_status) websocket_api.async_register_command(hass, websocket_node_status) websocket_api.async_register_command(hass, websocket_node_metadata) websocket_api.async_register_command(hass, websocket_node_comments) @@ -422,7 +423,6 @@ def async_register_api(hass: HomeAssistant) -> None: hass, websocket_subscribe_controller_statistics ) websocket_api.async_register_command(hass, websocket_subscribe_node_statistics) - websocket_api.async_register_command(hass, websocket_node_ready) hass.http.register_view(FirmwareUploadView()) @@ -497,25 +497,28 @@ async def websocket_network_status( @websocket_api.websocket_command( { - vol.Required(TYPE): "zwave_js/node_ready", + vol.Required(TYPE): "zwave_js/subscribe_node_status", vol.Required(DEVICE_ID): str, } ) @websocket_api.async_response @async_get_node -async def websocket_node_ready( +async def websocket_subscribe_node_status( hass: HomeAssistant, connection: ActiveConnection, msg: dict, node: Node, ) -> None: - """Subscribe to the node ready event of a Z-Wave JS node.""" + """Subscribe to node status update events of a Z-Wave JS node.""" @callback def forward_event(event: dict) -> None: """Forward the event.""" connection.send_message( - websocket_api.event_message(msg[ID], {"event": event["event"]}) + websocket_api.event_message( + msg[ID], + {"event": event["event"], "status": node.status, "ready": node.ready}, + ) ) @callback @@ -525,7 +528,10 @@ async def websocket_node_ready( unsub() connection.subscriptions[msg["id"]] = async_cleanup - msg[DATA_UNSUBSCRIBE] = unsubs = [node.on("ready", forward_event)] + msg[DATA_UNSUBSCRIBE] = unsubs = [ + node.on(evt, forward_event) + for evt in ("alive", "dead", "sleep", "wake up", "ready") + ] connection.send_result(msg[ID]) diff --git a/tests/components/zwave_js/test_api.py b/tests/components/zwave_js/test_api.py index 6c3dd796a7d..88fea684233 100644 --- a/tests/components/zwave_js/test_api.py +++ b/tests/components/zwave_js/test_api.py @@ -198,14 +198,14 @@ async def test_network_status(hass, multisensor_6, integration, hass_ws_client): assert msg["error"]["code"] == ERR_INVALID_FORMAT -async def test_node_ready( +async def test_subscribe_node_status( hass, multisensor_6_state, client, integration, hass_ws_client, ): - """Test the node ready websocket command.""" + """Test the subscribe node status websocket command.""" entry = integration ws_client = await hass_ws_client(hass) node_data = deepcopy(multisensor_6_state) # Copy to allow modification in tests. @@ -222,7 +222,7 @@ async def test_node_ready( await ws_client.send_json( { ID: 3, - TYPE: "zwave_js/node_ready", + TYPE: "zwave_js/subscribe_node_status", DEVICE_ID: device.id, } ) @@ -246,6 +246,25 @@ async def test_node_ready( msg = await ws_client.receive_json() assert msg["event"]["event"] == "ready" + assert msg["event"]["status"] == 1 + assert msg["event"]["ready"] + + event = Event( + "wake up", + { + "source": "node", + "event": "wake up", + "nodeId": node.node_id, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + + msg = await ws_client.receive_json() + + assert msg["event"]["event"] == "wake up" + assert msg["event"]["status"] == 2 + assert msg["event"]["ready"] async def test_node_status(hass, multisensor_6, integration, hass_ws_client):