Fix zwave_js flow set add-on options (#49813)

* Fix zwave_js flow set add-on options

* Improve and deduplicate error messages

* Add more call assertions

* Add final missing call assertion
This commit is contained in:
Martin Hjelmare 2021-04-28 15:09:39 +02:00 committed by GitHub
parent ded8297d27
commit 9e1042d9e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 10 deletions

View File

@ -46,7 +46,7 @@ def api_error(error_message: str) -> Callable[[F], F]:
try:
return_value = await func(*args, **kwargs)
except HassioAPIError as err:
raise AddonError(error_message) from err
raise AddonError(f"{error_message}: {err}") from err
return return_value

View File

@ -212,7 +212,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
try:
await self.install_task
except AddonError as err:
_LOGGER.error("Failed to install Z-Wave JS add-on: %s", err)
_LOGGER.error(err)
return self.async_show_progress_done(next_step_id="install_failed")
self.integration_created_addon = True
@ -274,7 +274,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
try:
await self.start_task
except (CannotConnect, AddonError) as err:
_LOGGER.error("Failed to start Z-Wave JS add-on: %s", err)
_LOGGER.error(err)
return self.async_show_progress_done(next_step_id="start_failed")
return self.async_show_progress_done(next_step_id="finish_addon_setup")
@ -309,7 +309,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
else:
break
else:
raise CannotConnect("Failed to start add-on: timeout")
raise CannotConnect("Failed to start Z-Wave JS add-on: timeout")
finally:
# Continue the flow after show progress when the task is done.
self.hass.async_create_task(
@ -352,7 +352,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
try:
addon_info: dict = await addon_manager.async_get_addon_info()
except AddonError as err:
_LOGGER.error("Failed to get Z-Wave JS add-on info: %s", err)
_LOGGER.error(err)
raise AbortFlow("addon_info_failed") from err
return addon_info
@ -374,12 +374,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def _async_set_addon_config(self, config: dict) -> None:
"""Set Z-Wave JS add-on config."""
options = {"options": config}
addon_manager: AddonManager = get_addon_manager(self.hass)
try:
await addon_manager.async_set_addon_options(options)
await addon_manager.async_set_addon_options(config)
except AddonError as err:
_LOGGER.error("Failed to set Z-Wave JS add-on config: %s", err)
_LOGGER.error(err)
raise AbortFlow("addon_set_config_failed") from err
async def _async_install_addon(self) -> None:
@ -399,7 +398,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
try:
discovery_info_config = await addon_manager.async_get_addon_discovery_info()
except AddonError as err:
_LOGGER.error("Failed to get Z-Wave JS add-on discovery info: %s", err)
_LOGGER.error(err)
raise AbortFlow("addon_get_discovery_info_failed") from err
return discovery_info_config

View File

@ -1,6 +1,6 @@
"""Test the Z-Wave JS config flow."""
import asyncio
from unittest.mock import DEFAULT, patch
from unittest.mock import DEFAULT, call, patch
import pytest
from zwave_js_server.version import VersionInfo
@ -384,6 +384,10 @@ async def test_discovery_addon_not_running(
result["flow_id"], {"usb_path": "/test", "network_key": "abc123"}
)
assert set_addon_options.call_args == call(
hass, "core_zwave_js", {"options": {"device": "/test", "network_key": "abc123"}}
)
assert result["type"] == "progress"
assert result["step_id"] == "start_addon"
@ -397,6 +401,8 @@ async def test_discovery_addon_not_running(
result = await hass.config_entries.flow.async_configure(result["flow_id"])
await hass.async_block_till_done()
assert start_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "create_entry"
assert result["title"] == TITLE
assert result["data"] == {
@ -441,6 +447,8 @@ async def test_discovery_addon_not_installed(
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert install_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "form"
assert result["step_id"] == "configure_addon"
@ -448,6 +456,10 @@ async def test_discovery_addon_not_installed(
result["flow_id"], {"usb_path": "/test", "network_key": "abc123"}
)
assert set_addon_options.call_args == call(
hass, "core_zwave_js", {"options": {"device": "/test", "network_key": "abc123"}}
)
assert result["type"] == "progress"
assert result["step_id"] == "start_addon"
@ -461,6 +473,8 @@ async def test_discovery_addon_not_installed(
result = await hass.config_entries.flow.async_configure(result["flow_id"])
await hass.async_block_till_done()
assert start_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "create_entry"
assert result["title"] == TITLE
assert result["data"] == {
@ -694,6 +708,10 @@ async def test_addon_installed(
result["flow_id"], {"usb_path": "/test", "network_key": "abc123"}
)
assert set_addon_options.call_args == call(
hass, "core_zwave_js", {"options": {"device": "/test", "network_key": "abc123"}}
)
assert result["type"] == "progress"
assert result["step_id"] == "start_addon"
@ -707,6 +725,8 @@ async def test_addon_installed(
result = await hass.config_entries.flow.async_configure(result["flow_id"])
await hass.async_block_till_done()
assert start_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "create_entry"
assert result["title"] == TITLE
assert result["data"] == {
@ -754,12 +774,18 @@ async def test_addon_installed_start_failure(
result["flow_id"], {"usb_path": "/test", "network_key": "abc123"}
)
assert set_addon_options.call_args == call(
hass, "core_zwave_js", {"options": {"device": "/test", "network_key": "abc123"}}
)
assert result["type"] == "progress"
assert result["step_id"] == "start_addon"
await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert start_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "abort"
assert result["reason"] == "addon_start_failed"
@ -807,12 +833,18 @@ async def test_addon_installed_failures(
result["flow_id"], {"usb_path": "/test", "network_key": "abc123"}
)
assert set_addon_options.call_args == call(
hass, "core_zwave_js", {"options": {"device": "/test", "network_key": "abc123"}}
)
assert result["type"] == "progress"
assert result["step_id"] == "start_addon"
await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert start_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "abort"
assert result["reason"] == "addon_start_failed"
@ -851,9 +883,15 @@ async def test_addon_installed_set_options_failure(
result["flow_id"], {"usb_path": "/test", "network_key": "abc123"}
)
assert set_addon_options.call_args == call(
hass, "core_zwave_js", {"options": {"device": "/test", "network_key": "abc123"}}
)
assert result["type"] == "abort"
assert result["reason"] == "addon_set_config_failed"
assert start_addon.call_count == 0
@pytest.mark.parametrize("discovery_info", [{"config": ADDON_DISCOVERY_INFO}])
async def test_addon_installed_already_configured(
@ -897,12 +935,20 @@ async def test_addon_installed_already_configured(
result["flow_id"], {"usb_path": "/test_new", "network_key": "def456"}
)
assert set_addon_options.call_args == call(
hass,
"core_zwave_js",
{"options": {"device": "/test_new", "network_key": "def456"}},
)
assert result["type"] == "progress"
assert result["step_id"] == "start_addon"
await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert start_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "abort"
assert result["reason"] == "already_configured"
assert entry.data["url"] == "ws://host1:3001"
@ -944,6 +990,8 @@ async def test_addon_not_installed(
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert install_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "form"
assert result["step_id"] == "configure_addon"
@ -951,6 +999,10 @@ async def test_addon_not_installed(
result["flow_id"], {"usb_path": "/test", "network_key": "abc123"}
)
assert set_addon_options.call_args == call(
hass, "core_zwave_js", {"options": {"device": "/test", "network_key": "abc123"}}
)
assert result["type"] == "progress"
assert result["step_id"] == "start_addon"
@ -964,6 +1016,8 @@ async def test_addon_not_installed(
result = await hass.config_entries.flow.async_configure(result["flow_id"])
await hass.async_block_till_done()
assert start_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "create_entry"
assert result["title"] == TITLE
assert result["data"] == {
@ -1001,5 +1055,7 @@ async def test_install_addon_failure(hass, supervisor, addon_installed, install_
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert install_addon.call_args == call(hass, "core_zwave_js")
assert result["type"] == "abort"
assert result["reason"] == "addon_install_failed"