From e079968ef4739d088eb23058f13f8ed4128de9af Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 21 Sep 2022 08:03:05 -1000 Subject: [PATCH] Handle timeout fetching bond token in config flow (#78896) --- homeassistant/components/bond/config_flow.py | 6 +++- tests/components/bond/test_config_flow.py | 38 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/bond/config_flow.py b/homeassistant/components/bond/config_flow.py index 09386c3587d..da8e6781bfa 100644 --- a/homeassistant/components/bond/config_flow.py +++ b/homeassistant/components/bond/config_flow.py @@ -1,6 +1,7 @@ """Config flow for Bond integration.""" from __future__ import annotations +import asyncio import contextlib from http import HTTPStatus import logging @@ -83,7 +84,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): instead ask them to manually enter the token. """ host = self._discovered[CONF_HOST] - if not (token := await async_get_token(self.hass, host)): + try: + if not (token := await async_get_token(self.hass, host)): + return + except asyncio.TimeoutError: return self._discovered[CONF_ACCESS_TOKEN] = token diff --git a/tests/components/bond/test_config_flow.py b/tests/components/bond/test_config_flow.py index 15aa643abaf..a54360283e6 100644 --- a/tests/components/bond/test_config_flow.py +++ b/tests/components/bond/test_config_flow.py @@ -1,6 +1,7 @@ """Test the Bond config flow.""" from __future__ import annotations +import asyncio from http import HTTPStatus from typing import Any from unittest.mock import MagicMock, Mock, patch @@ -268,6 +269,43 @@ async def test_zeroconf_form_token_unavailable(hass: core.HomeAssistant): assert len(mock_setup_entry.mock_calls) == 1 +async def test_zeroconf_form_token_times_out(hass: core.HomeAssistant): + """Test we get the discovery form and we handle the token request timeout.""" + + with patch_bond_version(), patch_bond_token(side_effect=asyncio.TimeoutError): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_ZEROCONF}, + data=zeroconf.ZeroconfServiceInfo( + host="test-host", + addresses=["test-host"], + hostname="mock_hostname", + name="ZXXX12345.some-other-tail-info", + port=None, + properties={}, + type="mock_type", + ), + ) + await hass.async_block_till_done() + assert result["type"] == "form" + assert result["errors"] == {} + + with patch_bond_version(), patch_bond_bridge(), patch_bond_device_ids(), _patch_async_setup_entry() as mock_setup_entry: + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_ACCESS_TOKEN: "test-token"}, + ) + await hass.async_block_till_done() + + assert result2["type"] == "create_entry" + assert result2["title"] == "bond-name" + assert result2["data"] == { + CONF_HOST: "test-host", + CONF_ACCESS_TOKEN: "test-token", + } + assert len(mock_setup_entry.mock_calls) == 1 + + async def test_zeroconf_form_with_token_available(hass: core.HomeAssistant): """Test we get the discovery form when we can get the token."""