From 3f5d7e85c3dee76c0a5ebb02b478f40feff7ba5d Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Mon, 30 Nov 2020 18:59:15 +0100 Subject: [PATCH] Add show progress to ozw config flow (#43310) --- homeassistant/components/ozw/config_flow.py | 30 ++++++++++++++++--- homeassistant/components/ozw/strings.json | 6 ++++ .../components/ozw/translations/en.json | 6 ++++ tests/components/ozw/test_config_flow.py | 17 +++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/ozw/config_flow.py b/homeassistant/components/ozw/config_flow.py index 1c0ccdefa70..9ecbcbe76bb 100644 --- a/homeassistant/components/ozw/config_flow.py +++ b/homeassistant/components/ozw/config_flow.py @@ -36,6 +36,7 @@ class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self.use_addon = False # If we install the add-on we should uninstall it on entry remove. self.integration_created_addon = False + self.install_task = None async def async_step_user(self, user_input=None): """Handle the initial step.""" @@ -93,16 +94,27 @@ class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return await self.async_step_install_addon() - async def async_step_install_addon(self): + async def async_step_install_addon(self, user_input=None): """Install OpenZWave add-on.""" + if not self.install_task: + self.install_task = self.hass.async_create_task(self._async_install_addon()) + return self.async_show_progress( + step_id="install_addon", progress_action="install_addon" + ) + try: - await self.hass.components.hassio.async_install_addon("core_zwave") + await self.install_task except self.hass.components.hassio.HassioAPIError as err: _LOGGER.error("Failed to install OpenZWave add-on: %s", err) - return self.async_abort(reason="addon_install_failed") + return self.async_show_progress_done(next_step_id="install_failed") + self.integration_created_addon = True - return await self.async_step_start_addon() + return self.async_show_progress_done(next_step_id="start_addon") + + async def async_step_install_failed(self, user_input=None): + """Add-on installation failed.""" + return self.async_abort(reason="addon_install_failed") async def async_step_start_addon(self, user_input=None): """Ask for config and start OpenZWave add-on.""" @@ -181,3 +193,13 @@ class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): except self.hass.components.hassio.HassioAPIError as err: _LOGGER.error("Failed to set OpenZWave add-on config: %s", err) raise AbortFlow("addon_set_config_failed") from err + + async def _async_install_addon(self): + """Install the OpenZWave add-on.""" + try: + await self.hass.components.hassio.async_install_addon("core_zwave") + finally: + # Continue the flow after show progress when the task is done. + self.hass.async_create_task( + self.hass.config_entries.flow.async_configure(flow_id=self.flow_id) + ) diff --git a/homeassistant/components/ozw/strings.json b/homeassistant/components/ozw/strings.json index 52317b3d6a8..f006f0663f0 100644 --- a/homeassistant/components/ozw/strings.json +++ b/homeassistant/components/ozw/strings.json @@ -6,6 +6,9 @@ "description": "Do you want to use the OpenZWave Supervisor add-on?", "data": {"use_addon": "Use the OpenZWave Supervisor add-on"} }, + "install_addon": { + "title": "The OpenZWave add-on installation has started" + }, "start_addon": { "title": "Enter the OpenZWave add-on configuration", "data": {"usb_path": "[%key:common::config_flow::data::usb_path%]", "network_key": "Network Key"} @@ -20,6 +23,9 @@ }, "error": { "addon_start_failed": "Failed to start the OpenZWave add-on. Check the configuration." + }, + "progress": { + "install_addon": "Please wait while the OpenZWave add-on installation finishes. This can take several minutes." } } } diff --git a/homeassistant/components/ozw/translations/en.json b/homeassistant/components/ozw/translations/en.json index e028e1923ae..e21b1819883 100644 --- a/homeassistant/components/ozw/translations/en.json +++ b/homeassistant/components/ozw/translations/en.json @@ -10,7 +10,13 @@ "error": { "addon_start_failed": "Failed to start the OpenZWave add-on. Check the configuration." }, + "progress": { + "install_addon": "Please wait while the OpenZWave add-on installation finishes. This can take several minutes." + }, "step": { + "install_addon": { + "title": "The OpenZWave add-on installation has started" + }, "on_supervisor": { "data": { "use_addon": "Use the OpenZWave Supervisor add-on" diff --git a/tests/components/ozw/test_config_flow.py b/tests/components/ozw/test_config_flow.py index 7561244999d..7a2b89967c5 100644 --- a/tests/components/ozw/test_config_flow.py +++ b/tests/components/ozw/test_config_flow.py @@ -305,6 +305,16 @@ async def test_addon_not_installed( result["flow_id"], {"use_addon": True} ) + assert result["type"] == "progress" + + # Make sure the flow continues when the progress task is done. + await hass.async_block_till_done() + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert result["type"] == "form" + assert result["step_id"] == "start_addon" + with patch( "homeassistant.components.ozw.async_setup", return_value=True ) as mock_setup, patch( @@ -342,5 +352,12 @@ async def test_install_addon_failure(hass, supervisor, addon_installed, install_ result["flow_id"], {"use_addon": True} ) + assert result["type"] == "progress" + + # Make sure the flow continues when the progress task is done. + await hass.async_block_till_done() + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + assert result["type"] == "abort" assert result["reason"] == "addon_install_failed"