diff --git a/homeassistant/components/lidarr/config_flow.py b/homeassistant/components/lidarr/config_flow.py index 1b7f2b23c113fa053bf0e0d04b9e6610683b7c3d..a0b7395076675dc283ef64a5f9fcad67b2da66af 100644 --- a/homeassistant/components/lidarr/config_flow.py +++ b/homeassistant/components/lidarr/config_flow.py @@ -5,7 +5,7 @@ from collections.abc import Mapping from typing import Any from aiohttp import ClientConnectorError -from aiopyarr import SystemStatus, exceptions +from aiopyarr import exceptions from aiopyarr.lidarr_client import LidarrClient import voluptuous as vol @@ -54,15 +54,16 @@ class LidarrConfigFlow(ConfigFlow, domain=DOMAIN): else: try: - result = await validate_input(self.hass, user_input) - if isinstance(result, tuple): + if result := await validate_input(self.hass, user_input): user_input[CONF_API_KEY] = result[1] - elif isinstance(result, str): - errors = {"base": result} except exceptions.ArrAuthenticationException: errors = {"base": "invalid_auth"} except (ClientConnectorError, exceptions.ArrConnectionException): errors = {"base": "cannot_connect"} + except exceptions.ArrWrongAppException: + errors = {"base": "wrong_app"} + except exceptions.ArrZeroConfException: + errors = {"base": "zeroconf_failed"} except exceptions.ArrException: errors = {"base": "unknown"} if not errors: @@ -95,7 +96,7 @@ class LidarrConfigFlow(ConfigFlow, domain=DOMAIN): async def validate_input( hass: HomeAssistant, data: dict[str, Any] -) -> tuple[str, str, str] | str | SystemStatus: +) -> tuple[str, str, str] | None: """Validate the user input allows us to connect. Data has the keys from DATA_SCHEMA with values provided by the user. @@ -108,4 +109,5 @@ async def validate_input( ) if CONF_API_KEY not in data: return await lidarr.async_try_zeroconf() - return await lidarr.async_get_system_status() + await lidarr.async_get_system_status() + return None diff --git a/homeassistant/components/lidarr/manifest.json b/homeassistant/components/lidarr/manifest.json index 6f7ad875a462850e27b5c6e1a478923f68c60f22..7d4e9bcede76dcef795020ddb4ec51489044be5e 100644 --- a/homeassistant/components/lidarr/manifest.json +++ b/homeassistant/components/lidarr/manifest.json @@ -2,7 +2,7 @@ "domain": "lidarr", "name": "Lidarr", "documentation": "https://www.home-assistant.io/integrations/lidarr", - "requirements": ["aiopyarr==22.7.0"], + "requirements": ["aiopyarr==22.9.0"], "codeowners": ["@tkdrob"], "config_flow": true, "iot_class": "local_polling", diff --git a/homeassistant/components/radarr/config_flow.py b/homeassistant/components/radarr/config_flow.py index af74922402a99548592fbb4ae834f6361e504fea..c37eeba4969b3d55421077e918b86b10cef6cd91 100644 --- a/homeassistant/components/radarr/config_flow.py +++ b/homeassistant/components/radarr/config_flow.py @@ -62,15 +62,16 @@ class RadarrConfigFlow(ConfigFlow, domain=DOMAIN): else: try: - result = await validate_input(self.hass, user_input) - if isinstance(result, tuple): + if result := await validate_input(self.hass, user_input): user_input[CONF_API_KEY] = result[1] - elif isinstance(result, str): - errors = {"base": result} except exceptions.ArrAuthenticationException: errors = {"base": "invalid_auth"} except (ClientConnectorError, exceptions.ArrConnectionException): errors = {"base": "cannot_connect"} + except exceptions.ArrWrongAppException: + errors = {"base": "wrong_app"} + except exceptions.ArrZeroConfException: + errors = {"base": "zeroconf_failed"} except exceptions.ArrException: errors = {"base": "unknown"} if not errors: @@ -130,7 +131,7 @@ class RadarrConfigFlow(ConfigFlow, domain=DOMAIN): async def validate_input( hass: HomeAssistant, data: dict[str, Any] -) -> tuple[str, str, str] | str | None: +) -> tuple[str, str, str] | None: """Validate the user input allows us to connect.""" host_configuration = PyArrHostConfiguration( api_token=data.get(CONF_API_KEY, ""), diff --git a/homeassistant/components/radarr/manifest.json b/homeassistant/components/radarr/manifest.json index 3ecb4247d878a5d03ec3d131714fe1c07fc653aa..5bc15b24069cd65bebc5689e2c34e118239ee735 100644 --- a/homeassistant/components/radarr/manifest.json +++ b/homeassistant/components/radarr/manifest.json @@ -2,7 +2,7 @@ "domain": "radarr", "name": "Radarr", "documentation": "https://www.home-assistant.io/integrations/radarr", - "requirements": ["aiopyarr==22.7.0"], + "requirements": ["aiopyarr==22.9.0"], "codeowners": ["@tkdrob"], "config_flow": true, "iot_class": "local_polling", diff --git a/homeassistant/components/sonarr/manifest.json b/homeassistant/components/sonarr/manifest.json index 6b9b45b75ec652c19532e1ec263e20e04c5afa06..0c5b68a794976e86401dc358b1b77782049a4c2e 100644 --- a/homeassistant/components/sonarr/manifest.json +++ b/homeassistant/components/sonarr/manifest.json @@ -3,7 +3,7 @@ "name": "Sonarr", "documentation": "https://www.home-assistant.io/integrations/sonarr", "codeowners": ["@ctalkington"], - "requirements": ["aiopyarr==22.7.0"], + "requirements": ["aiopyarr==22.9.0"], "config_flow": true, "quality_scale": "silver", "iot_class": "local_polling", diff --git a/requirements_all.txt b/requirements_all.txt index 66385faa8fccc85b263ed63ce7b72351c4a9303c..4b16e8d8cb1774722e37f49357e8e5e02fefa218 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -237,7 +237,7 @@ aiopvpc==3.0.0 # homeassistant.components.lidarr # homeassistant.components.radarr # homeassistant.components.sonarr -aiopyarr==22.7.0 +aiopyarr==22.9.0 # homeassistant.components.qnap_qsw aioqsw==0.2.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 5af62e48bdcd09181ed3355b30bda819d5c5a123..7a347fcf059de3f05463a745d37444a069c2d1a8 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -212,7 +212,7 @@ aiopvpc==3.0.0 # homeassistant.components.lidarr # homeassistant.components.radarr # homeassistant.components.sonarr -aiopyarr==22.7.0 +aiopyarr==22.9.0 # homeassistant.components.qnap_qsw aioqsw==0.2.2 diff --git a/tests/components/lidarr/test_config_flow.py b/tests/components/lidarr/test_config_flow.py index 0ec48439012a7a1f45ec0a0e3e9a508e838e8496..b78593243403775fc00637432d44ae1aad3e98f2 100644 --- a/tests/components/lidarr/test_config_flow.py +++ b/tests/components/lidarr/test_config_flow.py @@ -1,7 +1,7 @@ """Test Lidarr config flow.""" from unittest.mock import patch -from aiopyarr import ArrAuthenticationException, ArrConnectionException, ArrException +from aiopyarr import exceptions from homeassistant import data_entry_flow from homeassistant.components.lidarr.const import DEFAULT_NAME, DOMAIN @@ -45,7 +45,7 @@ async def test_flow_user_form( async def test_flow_user_invalid_auth(hass: HomeAssistant) -> None: """Test invalid authentication.""" with _patch_client() as client: - client.side_effect = ArrAuthenticationException + client.side_effect = exceptions.ArrAuthenticationException result = await hass.config_entries.flow.async_init( DOMAIN, context={CONF_SOURCE: SOURCE_USER}, @@ -62,7 +62,7 @@ async def test_flow_user_invalid_auth(hass: HomeAssistant) -> None: async def test_flow_user_cannot_connect(hass: HomeAssistant) -> None: """Test connection error.""" with _patch_client() as client: - client.side_effect = ArrConnectionException + client.side_effect = exceptions.ArrConnectionException result = await hass.config_entries.flow.async_init( DOMAIN, context={CONF_SOURCE: SOURCE_USER}, @@ -76,27 +76,44 @@ async def test_flow_user_cannot_connect(hass: HomeAssistant) -> None: assert result["errors"]["base"] == "cannot_connect" -async def test_flow_user_unknown_error(hass: HomeAssistant) -> None: - """Test unknown error.""" - with _patch_client() as client: - client.side_effect = ArrException +async def test_wrong_app(hass: HomeAssistant) -> None: + """Test we show user form on wrong app.""" + with patch( + "homeassistant.components.lidarr.config_flow.LidarrClient.async_try_zeroconf", + side_effect=exceptions.ArrWrongAppException, + ): result = await hass.config_entries.flow.async_init( DOMAIN, context={CONF_SOURCE: SOURCE_USER}, + data=MOCK_USER_INPUT, ) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input=CONF_DATA, + + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == "user" + assert result["errors"]["base"] == "wrong_app" + + +async def test_zero_conf_failure(hass: HomeAssistant) -> None: + """Test we show user form on api key retrieval failure.""" + with patch( + "homeassistant.components.lidarr.config_flow.LidarrClient.async_try_zeroconf", + side_effect=exceptions.ArrZeroConfException, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={CONF_SOURCE: SOURCE_USER}, + data=MOCK_USER_INPUT, ) + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "user" - assert result["errors"]["base"] == "unknown" + assert result["errors"]["base"] == "zeroconf_failed" -async def test_flow_user_failed_zeroconf(hass: HomeAssistant) -> None: - """Test zero configuration failed.""" +async def test_flow_user_unknown_error(hass: HomeAssistant) -> None: + """Test unknown error.""" with _patch_client() as client: - client.return_value = "zeroconf_failed" + client.side_effect = exceptions.ArrException result = await hass.config_entries.flow.async_init( DOMAIN, context={CONF_SOURCE: SOURCE_USER}, @@ -107,7 +124,7 @@ async def test_flow_user_failed_zeroconf(hass: HomeAssistant) -> None: ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "user" - assert result["errors"]["base"] == "zeroconf_failed" + assert result["errors"]["base"] == "unknown" async def test_flow_reauth( diff --git a/tests/components/radarr/test_config_flow.py b/tests/components/radarr/test_config_flow.py index ffd6b5f575908d1e3e4b893288585db965f8160a..6aac4e369fefd2fa5e44708263acbb5beb00064c 100644 --- a/tests/components/radarr/test_config_flow.py +++ b/tests/components/radarr/test_config_flow.py @@ -1,7 +1,7 @@ """Test Radarr config flow.""" from unittest.mock import AsyncMock, patch -from aiopyarr import ArrException +from aiopyarr import exceptions from homeassistant import data_entry_flow from homeassistant.components.radarr.const import DEFAULT_NAME, DOMAIN @@ -115,7 +115,7 @@ async def test_wrong_app(hass: HomeAssistant) -> None: """Test we show user form on wrong app.""" with patch( "homeassistant.components.radarr.config_flow.RadarrClient.async_try_zeroconf", - return_value="wrong_app", + side_effect=exceptions.ArrWrongAppException, ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -125,14 +125,31 @@ async def test_wrong_app(hass: HomeAssistant) -> None: assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "user" - assert result["errors"] == {"base": "wrong_app"} + assert result["errors"]["base"] == "wrong_app" + + +async def test_zero_conf_failure(hass: HomeAssistant) -> None: + """Test we show user form on api key retrieval failure.""" + with patch( + "homeassistant.components.radarr.config_flow.RadarrClient.async_try_zeroconf", + side_effect=exceptions.ArrZeroConfException, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={CONF_SOURCE: SOURCE_USER}, + data={CONF_URL: URL, CONF_VERIFY_SSL: False}, + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == "user" + assert result["errors"]["base"] == "zeroconf_failed" async def test_unknown_error(hass: HomeAssistant) -> None: """Test we show user form on unknown error.""" with patch( "homeassistant.components.radarr.config_flow.RadarrClient.async_get_system_status", - side_effect=ArrException, + side_effect=exceptions.ArrException, ): result = await hass.config_entries.flow.async_init( DOMAIN,