From 59cea56e170d9e55e3c77b0be5dd47ded75919ce Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 13 Jan 2022 00:08:04 +0100 Subject: [PATCH] Fix reconnect rather than reauth when both HA and UniFi controller restarts at the same time (#63994) --- homeassistant/components/unifi/controller.py | 1 + tests/components/unifi/test_controller.py | 59 ++++++-------------- 2 files changed, 17 insertions(+), 43 deletions(-) diff --git a/homeassistant/components/unifi/controller.py b/homeassistant/components/unifi/controller.py index add1b7cdd45..7b8856e3aaf 100644 --- a/homeassistant/components/unifi/controller.py +++ b/homeassistant/components/unifi/controller.py @@ -500,6 +500,7 @@ async def get_controller( aiounifi.BadGateway, aiounifi.ServiceUnavailable, aiounifi.RequestError, + aiounifi.ResponseError, ) as err: LOGGER.error("Error connecting to the UniFi Network at %s: %s", host, err) raise CannotConnect from err diff --git a/tests/components/unifi/test_controller.py b/tests/components/unifi/test_controller.py index 81990c42231..8a41ada9b62 100644 --- a/tests/components/unifi/test_controller.py +++ b/tests/components/unifi/test_controller.py @@ -473,49 +473,22 @@ async def test_get_controller_verify_ssl_false(hass): assert await get_controller(hass, **controller_data) -async def test_get_controller_login_failed(hass): - """Check that get_controller can handle a failed login.""" - with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch( - "aiounifi.Controller.login", side_effect=aiounifi.Unauthorized - ), pytest.raises(AuthenticationRequired): - await get_controller(hass, **CONTROLLER_DATA) - - -async def test_get_controller_controller_bad_gateway(hass): +@pytest.mark.parametrize( + "side_effect,raised_exception", + [ + (asyncio.TimeoutError, CannotConnect), + (aiounifi.BadGateway, CannotConnect), + (aiounifi.ServiceUnavailable, CannotConnect), + (aiounifi.RequestError, CannotConnect), + (aiounifi.ResponseError, CannotConnect), + (aiounifi.Unauthorized, AuthenticationRequired), + (aiounifi.LoginRequired, AuthenticationRequired), + (aiounifi.AiounifiException, AuthenticationRequired), + ], +) +async def test_get_controller_fails_to_connect(hass, side_effect, raised_exception): """Check that get_controller can handle controller being unavailable.""" with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch( - "aiounifi.Controller.login", side_effect=aiounifi.BadGateway - ), pytest.raises(CannotConnect): - await get_controller(hass, **CONTROLLER_DATA) - - -async def test_get_controller_controller_service_unavailable(hass): - """Check that get_controller can handle controller being unavailable.""" - with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch( - "aiounifi.Controller.login", side_effect=aiounifi.ServiceUnavailable - ), pytest.raises(CannotConnect): - await get_controller(hass, **CONTROLLER_DATA) - - -async def test_get_controller_controller_unavailable(hass): - """Check that get_controller can handle controller being unavailable.""" - with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch( - "aiounifi.Controller.login", side_effect=aiounifi.RequestError - ), pytest.raises(CannotConnect): - await get_controller(hass, **CONTROLLER_DATA) - - -async def test_get_controller_login_required(hass): - """Check that get_controller can handle unknown errors.""" - with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch( - "aiounifi.Controller.login", side_effect=aiounifi.LoginRequired - ), pytest.raises(AuthenticationRequired): - await get_controller(hass, **CONTROLLER_DATA) - - -async def test_get_controller_unknown_error(hass): - """Check that get_controller can handle unknown errors.""" - with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch( - "aiounifi.Controller.login", side_effect=aiounifi.AiounifiException - ), pytest.raises(AuthenticationRequired): + "aiounifi.Controller.login", side_effect=side_effect + ), pytest.raises(raised_exception): await get_controller(hass, **CONTROLLER_DATA)