diff --git a/homeassistant/components/zwave_js/api.py b/homeassistant/components/zwave_js/api.py index 2f23d06b38d..defb7ab0040 100644 --- a/homeassistant/components/zwave_js/api.py +++ b/homeassistant/components/zwave_js/api.py @@ -43,7 +43,10 @@ from zwave_js_server.model.node.firmware import ( NodeFirmwareUpdateProgress, NodeFirmwareUpdateResult, ) -from zwave_js_server.model.utils import async_parse_qr_code_string +from zwave_js_server.model.utils import ( + async_parse_qr_code_string, + async_try_parse_dsk_from_qr_code_string, +) from zwave_js_server.util.node import async_set_config_parameter from homeassistant.components import websocket_api @@ -396,6 +399,9 @@ def async_register_api(hass: HomeAssistant) -> None: websocket_api.async_register_command(hass, websocket_unprovision_smart_start_node) websocket_api.async_register_command(hass, websocket_get_provisioning_entries) websocket_api.async_register_command(hass, websocket_parse_qr_code_string) + websocket_api.async_register_command( + hass, websocket_try_parse_dsk_from_qr_code_string + ) websocket_api.async_register_command(hass, websocket_supports_feature) websocket_api.async_register_command(hass, websocket_stop_inclusion) websocket_api.async_register_command(hass, websocket_stop_exclusion) @@ -980,6 +986,32 @@ async def websocket_parse_qr_code_string( connection.send_result(msg[ID], dataclasses.asdict(qr_provisioning_information)) +@websocket_api.require_admin +@websocket_api.websocket_command( + { + vol.Required(TYPE): "zwave_js/try_parse_dsk_from_qr_code_string", + vol.Required(ENTRY_ID): str, + vol.Required(QR_CODE_STRING): str, + } +) +@websocket_api.async_response +@async_handle_failed_command +@async_get_entry +async def websocket_try_parse_dsk_from_qr_code_string( + hass: HomeAssistant, + connection: ActiveConnection, + msg: dict[str, Any], + entry: ConfigEntry, + client: Client, + driver: Driver, +) -> None: + """Try to parse a DSK string from a QR code.""" + connection.send_result( + msg[ID], + await async_try_parse_dsk_from_qr_code_string(client, msg[QR_CODE_STRING]), + ) + + @websocket_api.require_admin @websocket_api.websocket_command( { diff --git a/tests/components/zwave_js/test_api.py b/tests/components/zwave_js/test_api.py index 8b34b3548fe..8003b4524e4 100644 --- a/tests/components/zwave_js/test_api.py +++ b/tests/components/zwave_js/test_api.py @@ -1431,9 +1431,72 @@ async def test_parse_qr_code_string( assert msg["error"]["code"] == ERR_NOT_LOADED -async def test_supports_feature( - hass: HomeAssistant, integration, client, hass_ws_client: WebSocketGenerator -) -> None: +async def test_try_parse_dsk_from_qr_code_string( + hass, integration, client, hass_ws_client +): + """Test try_parse_dsk_from_qr_code_string websocket command.""" + entry = integration + ws_client = await hass_ws_client(hass) + + client.async_send_command.return_value = {"dsk": "a"} + + await ws_client.send_json( + { + ID: 1, + TYPE: "zwave_js/try_parse_dsk_from_qr_code_string", + ENTRY_ID: entry.entry_id, + QR_CODE_STRING: "90testtesttesttesttesttesttesttesttesttesttesttesttest", + } + ) + + msg = await ws_client.receive_json() + assert msg["success"] + assert msg["result"] == "a" + + assert len(client.async_send_command.call_args_list) == 1 + assert client.async_send_command.call_args[0][0] == { + "command": "utils.try_parse_dsk_from_qr_code_string", + "qr": "90testtesttesttesttesttesttesttesttesttesttesttesttest", + } + + # Test FailedZWaveCommand is caught + with patch( + "homeassistant.components.zwave_js.api.async_try_parse_dsk_from_qr_code_string", + side_effect=FailedZWaveCommand("failed_command", 1, "error message"), + ): + await ws_client.send_json( + { + ID: 6, + TYPE: "zwave_js/try_parse_dsk_from_qr_code_string", + ENTRY_ID: entry.entry_id, + QR_CODE_STRING: "90testtesttesttesttesttesttesttesttesttesttesttesttest", + } + ) + 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: 7, + TYPE: "zwave_js/try_parse_dsk_from_qr_code_string", + ENTRY_ID: entry.entry_id, + QR_CODE_STRING: "90testtesttesttesttesttesttesttesttesttesttesttesttest", + } + ) + msg = await ws_client.receive_json() + + assert not msg["success"] + assert msg["error"]["code"] == ERR_NOT_LOADED + + +async def test_supports_feature(hass, integration, client, hass_ws_client): """Test supports_feature websocket command.""" entry = integration ws_client = await hass_ws_client(hass)