From e360a4fa9e40ace622e31f309af265fc4d6bbcfd Mon Sep 17 00:00:00 2001
From: Robert Hillis <tkdrob4390@yahoo.com>
Date: Wed, 28 Sep 2022 02:06:24 -0400
Subject: [PATCH] Bump aiopyarr to 22.9.0 (#79173)

---
 .../components/lidarr/config_flow.py          | 16 ++++---
 homeassistant/components/lidarr/manifest.json |  2 +-
 .../components/radarr/config_flow.py          | 11 +++--
 homeassistant/components/radarr/manifest.json |  2 +-
 homeassistant/components/sonarr/manifest.json |  2 +-
 requirements_all.txt                          |  2 +-
 requirements_test_all.txt                     |  2 +-
 tests/components/lidarr/test_config_flow.py   | 47 +++++++++++++------
 tests/components/radarr/test_config_flow.py   | 25 ++++++++--
 9 files changed, 73 insertions(+), 36 deletions(-)

diff --git a/homeassistant/components/lidarr/config_flow.py b/homeassistant/components/lidarr/config_flow.py
index 1b7f2b23c11..a0b73950766 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 6f7ad875a46..7d4e9bcede7 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 af74922402a..c37eeba4969 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 3ecb4247d87..5bc15b24069 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 6b9b45b75ec..0c5b68a7949 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 66385faa8fc..4b16e8d8cb1 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 5af62e48bdc..7a347fcf059 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 0ec48439012..b7859324340 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 ffd6b5f5759..6aac4e369fe 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,
-- 
GitLab