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 Paulus Schoutsen
parent 0a64ae2f7a
commit 2e2d303291
2 changed files with 9 additions and 7 deletions

View File

@ -58,7 +58,7 @@ class BrotherConfigFlow(config_entries.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"
@ -88,7 +88,7 @@ class BrotherConfigFlow(config_entries.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

@ -93,10 +93,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
@ -147,10 +148,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,