Catch TimeoutError in Brother config flow (#113593)

* Catch TimeoutError in Brother config flow

* Update tests

* Remove unnecessary parentheses

---------

Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
This commit is contained in:
Maciej Bieniek 2024-03-16 16:01:48 +01:00 committed by GitHub
parent ef0c17749f
commit 6191b25563
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 7 deletions

View File

@ -59,7 +59,7 @@ class BrotherConfigFlow(ConfigFlow, domain=DOMAIN):
return self.async_create_entry(title=title, data=user_input)
except InvalidHost:
errors[CONF_HOST] = "wrong_host"
except ConnectionError:
except (ConnectionError, TimeoutError):
errors["base"] = "cannot_connect"
except SnmpError:
errors["base"] = "snmp_error"
@ -89,7 +89,7 @@ class BrotherConfigFlow(ConfigFlow, domain=DOMAIN):
await self.brother.async_update()
except UnsupportedModelError:
return self.async_abort(reason="unsupported_model")
except (ConnectionError, SnmpError):
except (ConnectionError, SnmpError, TimeoutError):
return self.async_abort(reason="cannot_connect")
# Check if already configured

View File

@ -94,10 +94,11 @@ async def test_invalid_hostname(hass: HomeAssistant) -> None:
assert result["errors"] == {CONF_HOST: "wrong_host"}
async def test_connection_error(hass: HomeAssistant) -> None:
@pytest.mark.parametrize("exc", [ConnectionError, TimeoutError])
async def test_connection_error(hass: HomeAssistant, exc: Exception) -> None:
"""Test connection to host error."""
with patch("brother.Brother.initialize"), patch(
"brother.Brother._get_data", side_effect=ConnectionError()
"brother.Brother._get_data", side_effect=exc
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
@ -148,10 +149,11 @@ async def test_device_exists_abort(hass: HomeAssistant) -> None:
assert result["reason"] == "already_configured"
async def test_zeroconf_snmp_error(hass: HomeAssistant) -> None:
"""Test we abort zeroconf flow on SNMP error."""
@pytest.mark.parametrize("exc", [ConnectionError, TimeoutError, SnmpError("error")])
async def test_zeroconf_exception(hass: HomeAssistant, exc: Exception) -> None:
"""Test we abort zeroconf flow on exception."""
with patch("brother.Brother.initialize"), patch(
"brother.Brother._get_data", side_effect=SnmpError("error")
"brother.Brother._get_data", side_effect=exc
):
result = await hass.config_entries.flow.async_init(
DOMAIN,