Add support for firmware target in zwave_js FirmwareUploadView (#88523)

* Add support for firmware target in zwave_js FirmwareUploadView

fix

* Update tests/components/zwave_js/test_api.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update tests/components/zwave_js/test_api.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update tests/components/zwave_js/test_api.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update tests/components/zwave_js/test_api.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* fix types

* Switch back to using Any

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/88679/head
Raman Gupta 2023-02-23 13:24:55 -05:00 committed by GitHub
parent 6112793b19
commit 5739782877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 8 deletions

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
import dataclasses import dataclasses
from functools import partial, wraps from functools import partial, wraps
from typing import Any, Literal from typing import Any, Literal, cast
from aiohttp import web, web_exceptions, web_request from aiohttp import web, web_exceptions, web_request
import voluptuous as vol import voluptuous as vol
@ -2186,6 +2186,9 @@ class FirmwareUploadView(HomeAssistantView):
additional_user_agent_components=USER_AGENT, additional_user_agent_components=USER_AGENT,
) )
else: else:
firmware_target: int | None = None
if "target" in data:
firmware_target = int(cast(str, data["target"]))
await update_firmware( await update_firmware(
node.client.ws_server_url, node.client.ws_server_url,
node, node,
@ -2193,6 +2196,7 @@ class FirmwareUploadView(HomeAssistantView):
NodeFirmwareUpdateData( NodeFirmwareUpdateData(
uploaded_file.filename, uploaded_file.filename,
await hass.async_add_executor_job(uploaded_file.file.read), await hass.async_add_executor_job(uploaded_file.file.read),
firmware_target=firmware_target,
) )
], ],
async_get_clientsession(hass), async_get_clientsession(hass),

View File

@ -2,6 +2,7 @@
from copy import deepcopy from copy import deepcopy
from http import HTTPStatus from http import HTTPStatus
import json import json
from typing import Any
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -2983,12 +2984,18 @@ async def test_get_config_parameters(
assert msg["error"]["code"] == ERR_NOT_LOADED assert msg["error"]["code"] == ERR_NOT_LOADED
@pytest.mark.parametrize(
("firmware_data", "expected_data"),
[({"target": "1"}, {"firmware_target": 1}), ({}, {})],
)
async def test_firmware_upload_view( async def test_firmware_upload_view(
hass: HomeAssistant, hass: HomeAssistant,
multisensor_6, multisensor_6,
integration, integration,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
firmware_file, firmware_file,
firmware_data: dict[str, Any],
expected_data: dict[str, Any],
) -> None: ) -> None:
"""Test the HTTP firmware upload view.""" """Test the HTTP firmware upload view."""
client = await hass_client() client = await hass_client()
@ -3001,15 +3008,19 @@ async def test_firmware_upload_view(
"homeassistant.components.zwave_js.api.USER_AGENT", "homeassistant.components.zwave_js.api.USER_AGENT",
{"HomeAssistant": "0.0.0"}, {"HomeAssistant": "0.0.0"},
): ):
data = {"file": firmware_file}
data.update(firmware_data)
resp = await client.post( resp = await client.post(
f"/api/zwave_js/firmware/upload/{device.id}", f"/api/zwave_js/firmware/upload/{device.id}", data=data
data={"file": firmware_file},
) )
update_data = NodeFirmwareUpdateData("file", bytes(10))
for attr, value in expected_data.items():
setattr(update_data, attr, value)
mock_controller_cmd.assert_not_called() mock_controller_cmd.assert_not_called()
assert mock_node_cmd.call_args[0][1:3] == ( assert mock_node_cmd.call_args[0][1:3] == (multisensor_6, [update_data])
multisensor_6,
[NodeFirmwareUpdateData("file", bytes(10))],
)
assert mock_node_cmd.call_args[1] == { assert mock_node_cmd.call_args[1] == {
"additional_user_agent_components": {"HomeAssistant": "0.0.0"}, "additional_user_agent_components": {"HomeAssistant": "0.0.0"},
} }
@ -3017,7 +3028,11 @@ async def test_firmware_upload_view(
async def test_firmware_upload_view_controller( async def test_firmware_upload_view_controller(
hass, client, integration, hass_client: ClientSessionGenerator, firmware_file hass: HomeAssistant,
client,
integration,
hass_client: ClientSessionGenerator,
firmware_file,
) -> None: ) -> None:
"""Test the HTTP firmware upload view for a controller.""" """Test the HTTP firmware upload view for a controller."""
hass_client = await hass_client() hass_client = await hass_client()