From 768e53ac2d0509ff2f2c7af17820b4ef4772f019 Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Thu, 23 Jun 2022 20:13:37 -0400 Subject: [PATCH] Add zwave_js/get_any_firmware_update_progress WS cmd (#73905) --- homeassistant/components/zwave_js/api.py | 27 +++++++++ tests/components/zwave_js/test_api.py | 71 ++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/homeassistant/components/zwave_js/api.py b/homeassistant/components/zwave_js/api.py index ca25088a642..8b98c61c4b2 100644 --- a/homeassistant/components/zwave_js/api.py +++ b/homeassistant/components/zwave_js/api.py @@ -421,6 +421,9 @@ def async_register_api(hass: HomeAssistant) -> None: websocket_api.async_register_command( hass, websocket_get_firmware_update_capabilities ) + websocket_api.async_register_command( + hass, websocket_get_any_firmware_update_progress + ) websocket_api.async_register_command(hass, websocket_check_for_config_updates) websocket_api.async_register_command(hass, websocket_install_config_update) websocket_api.async_register_command( @@ -1989,6 +1992,30 @@ async def websocket_get_firmware_update_capabilities( connection.send_result(msg[ID], capabilities.to_dict()) +@websocket_api.require_admin +@websocket_api.websocket_command( + { + vol.Required(TYPE): "zwave_js/get_any_firmware_update_progress", + vol.Required(ENTRY_ID): str, + } +) +@websocket_api.async_response +@async_handle_failed_command +@async_get_entry +async def websocket_get_any_firmware_update_progress( + hass: HomeAssistant, + connection: ActiveConnection, + msg: dict, + entry: ConfigEntry, + client: Client, + driver: Driver, +) -> None: + """Get whether any firmware updates are in progress.""" + connection.send_result( + msg[ID], await driver.controller.async_get_any_firmware_update_progress() + ) + + class FirmwareUploadView(HomeAssistantView): """View to upload firmware.""" diff --git a/tests/components/zwave_js/test_api.py b/tests/components/zwave_js/test_api.py index 1ed125cc43a..3a2bff54e88 100644 --- a/tests/components/zwave_js/test_api.py +++ b/tests/components/zwave_js/test_api.py @@ -3723,6 +3723,77 @@ async def test_get_firmware_update_capabilities( assert msg["error"]["code"] == ERR_NOT_FOUND +async def test_get_any_firmware_update_progress( + hass, client, integration, hass_ws_client +): + """Test that the get_any_firmware_update_progress WS API call works.""" + entry = integration + ws_client = await hass_ws_client(hass) + + client.async_send_command.return_value = {"progress": True} + await ws_client.send_json( + { + ID: 1, + TYPE: "zwave_js/get_any_firmware_update_progress", + ENTRY_ID: entry.entry_id, + } + ) + msg = await ws_client.receive_json() + assert msg["success"] + assert msg["result"] + + assert len(client.async_send_command.call_args_list) == 1 + args = client.async_send_command.call_args[0][0] + assert args["command"] == "controller.get_any_firmware_update_progress" + + # Test FailedZWaveCommand is caught + with patch( + "zwave_js_server.model.controller.Controller.async_get_any_firmware_update_progress", + side_effect=FailedZWaveCommand("failed_command", 1, "error message"), + ): + await ws_client.send_json( + { + ID: 2, + TYPE: "zwave_js/get_any_firmware_update_progress", + ENTRY_ID: entry.entry_id, + } + ) + msg = await ws_client.receive_json() + + assert not msg["success"] + assert msg["error"]["code"] == "zwave_error" + assert msg["error"]["message"] == "Z-Wave error 1: error message" + + # Test sending command with not loaded entry fails + await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done() + + await ws_client.send_json( + { + ID: 3, + TYPE: "zwave_js/get_any_firmware_update_progress", + ENTRY_ID: entry.entry_id, + } + ) + msg = await ws_client.receive_json() + + assert not msg["success"] + assert msg["error"]["code"] == ERR_NOT_LOADED + + # Test sending command with improper device ID fails + await ws_client.send_json( + { + ID: 4, + TYPE: "zwave_js/get_any_firmware_update_progress", + ENTRY_ID: "invalid_entry", + } + ) + msg = await ws_client.receive_json() + + assert not msg["success"] + assert msg["error"]["code"] == ERR_NOT_FOUND + + async def test_check_for_config_updates(hass, client, integration, hass_ws_client): """Test that the check_for_config_updates WS API call works.""" entry = integration