diff --git a/homeassistant/components/tado/config_flow.py b/homeassistant/components/tado/config_flow.py index cd1fb307eb1e..eeb90e7ca56e 100644 --- a/homeassistant/components/tado/config_flow.py +++ b/homeassistant/components/tado/config_flow.py @@ -40,6 +40,8 @@ class TadoConfigFlow(ConfigFlow, domain=DOMAIN): login_task: asyncio.Task | None = None refresh_token: str | None = None tado: Tado | None = None + tado_device_url: str = "" + user_code: str = "" async def async_step_reauth( self, entry_data: Mapping[str, Any] @@ -69,8 +71,8 @@ class TadoConfigFlow(ConfigFlow, domain=DOMAIN): _LOGGER.exception("Error while initiating Tado") return self.async_abort(reason="cannot_connect") assert self.tado is not None - tado_device_url = self.tado.device_verification_url() - user_code = URL(tado_device_url).query["user_code"] + self.tado_device_url = self.tado.device_verification_url() + self.user_code = URL(self.tado_device_url).query["user_code"] async def _wait_for_login() -> None: """Wait for the user to login.""" @@ -119,8 +121,8 @@ class TadoConfigFlow(ConfigFlow, domain=DOMAIN): step_id="user", progress_action="wait_for_device", description_placeholders={ - "url": tado_device_url, - "code": user_code, + "url": self.tado_device_url, + "code": self.user_code, }, progress_task=self.login_task, ) diff --git a/tests/components/tado/test_config_flow.py b/tests/components/tado/test_config_flow.py index 6e7cb7dd928d..5367dcc43773 100644 --- a/tests/components/tado/test_config_flow.py +++ b/tests/components/tado/test_config_flow.py @@ -231,6 +231,43 @@ async def test_options_flow( assert result["data"] == {CONF_FALLBACK: CONST_OVERLAY_TADO_DEFAULT} +async def test_show_progress_polling( + hass: HomeAssistant, + mock_tado_api: MagicMock, + mock_setup_entry: AsyncMock, +) -> None: + """Test progress step re-entry while login task is still running.""" + + event = threading.Event() + + def mock_tado_api_device_activation() -> None: + event.wait(timeout=5) + + mock_tado_api.device_activation = mock_tado_api_device_activation + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.SHOW_PROGRESS + assert result["step_id"] == "user" + assert result["description_placeholders"]["url"] is not None + assert result["description_placeholders"]["code"] == "TEST" + + # Poll again while task is still running — this re-enters async_step_user + # with self.tado already set + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + assert result["type"] is FlowResultType.SHOW_PROGRESS + assert result["description_placeholders"]["url"] is not None + assert result["description_placeholders"]["code"] == "TEST" + + # Now complete the login + event.set() + await hass.async_block_till_done() + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + assert result["type"] is FlowResultType.CREATE_ENTRY + + async def test_homekit(hass: HomeAssistant, mock_tado_api: MagicMock) -> None: """Test that we abort from homekit if tado is already setup."""