From 0d69adb4046d6487dabe07277b93e7af81205cfc Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Thu, 12 May 2022 03:07:11 -0400 Subject: [PATCH] Send initial message for certain zwave_js ws subscriptions (#71723) * Send initial message for certain zwave_js ws subscriptions * Be consistent * fix tests and bugs --- homeassistant/components/zwave_js/api.py | 39 ++++++++++++++++++++---- tests/components/zwave_js/test_api.py | 24 +++++++++++++-- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/zwave_js/api.py b/homeassistant/components/zwave_js/api.py index 5608679fe90..7d1c4622d50 100644 --- a/homeassistant/components/zwave_js/api.py +++ b/homeassistant/components/zwave_js/api.py @@ -1848,9 +1848,17 @@ async def websocket_subscribe_firmware_update_status( connection.subscriptions[msg["id"]] = async_cleanup progress = node.firmware_update_progress - connection.send_result( - msg[ID], _get_firmware_update_progress_dict(progress) if progress else None - ) + connection.send_result(msg[ID]) + if progress: + connection.send_message( + websocket_api.event_message( + msg[ID], + { + "event": "firmware update progress", + **_get_firmware_update_progress_dict(progress), + }, + ) + ) class FirmwareUploadView(HomeAssistantView): @@ -2011,8 +2019,16 @@ async def websocket_subscribe_controller_statistics( ] connection.subscriptions[msg["id"]] = async_cleanup - connection.send_result( - msg[ID], _get_controller_statistics_dict(controller.statistics) + connection.send_result(msg[ID]) + connection.send_message( + websocket_api.event_message( + msg[ID], + { + "event": "statistics updated", + "source": "controller", + **_get_controller_statistics_dict(controller.statistics), + }, + ) ) @@ -2069,7 +2085,18 @@ async def websocket_subscribe_node_statistics( msg[DATA_UNSUBSCRIBE] = unsubs = [node.on("statistics updated", forward_stats)] connection.subscriptions[msg["id"]] = async_cleanup - connection.send_result(msg[ID], _get_node_statistics_dict(node.statistics)) + connection.send_result(msg[ID]) + connection.send_message( + websocket_api.event_message( + msg[ID], + { + "event": "statistics updated", + "source": "node", + "nodeId": node.node_id, + **_get_node_statistics_dict(node.statistics), + }, + ) + ) @websocket_api.require_admin diff --git a/tests/components/zwave_js/test_api.py b/tests/components/zwave_js/test_api.py index f4ffa0876d9..d8a36fc6509 100644 --- a/tests/components/zwave_js/test_api.py +++ b/tests/components/zwave_js/test_api.py @@ -3479,7 +3479,14 @@ async def test_subscribe_firmware_update_status_initial_value( msg = await ws_client.receive_json() assert msg["success"] - assert msg["result"] == {"sent_fragments": 1, "total_fragments": 10} + assert msg["result"] is None + + msg = await ws_client.receive_json() + assert msg["event"] == { + "event": "firmware update progress", + "sent_fragments": 1, + "total_fragments": 10, + } async def test_subscribe_firmware_update_status_failures( @@ -3688,7 +3695,12 @@ async def test_subscribe_controller_statistics( msg = await ws_client.receive_json() assert msg["success"] - assert msg["result"] == { + assert msg["result"] is None + + msg = await ws_client.receive_json() + assert msg["event"] == { + "event": "statistics updated", + "source": "controller", "messages_tx": 0, "messages_rx": 0, "messages_dropped_tx": 0, @@ -3783,7 +3795,13 @@ async def test_subscribe_node_statistics( msg = await ws_client.receive_json() assert msg["success"] - assert msg["result"] == { + assert msg["result"] is None + + msg = await ws_client.receive_json() + assert msg["event"] == { + "source": "node", + "event": "statistics updated", + "nodeId": multisensor_6.node_id, "commands_tx": 0, "commands_rx": 0, "commands_dropped_tx": 0,