Handle timeout fetching bond token in config flow (#78896)

pull/78908/head
J. Nick Koston 2022-09-21 08:03:05 -10:00 committed by GitHub
parent 0a8a5b973a
commit e079968ef4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View File

@ -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

View File

@ -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."""