From 2e2d303291486ed4e98d5cdc6e9139785397641a Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Sat, 16 Mar 2024 16:01:48 +0100 Subject: [PATCH] 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> --- homeassistant/components/brother/config_flow.py | 4 ++-- tests/components/brother/test_config_flow.py | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/brother/config_flow.py b/homeassistant/components/brother/config_flow.py index 55d47bb0c2c..3c60ccba5f0 100644 --- a/homeassistant/components/brother/config_flow.py +++ b/homeassistant/components/brother/config_flow.py @@ -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 diff --git a/tests/components/brother/test_config_flow.py b/tests/components/brother/test_config_flow.py index f83f882b8a0..3d83ecfcb7c 100644 --- a/tests/components/brother/test_config_flow.py +++ b/tests/components/brother/test_config_flow.py @@ -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,