From 05d3ca7e02b950ddfb6d3f3cb0f75fcf475fded5 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sat, 30 Apr 2022 00:34:33 +0200 Subject: [PATCH] Fix linking issue when deCONZ gateway is not unlocked (#71082) --- homeassistant/components/deconz/config_flow.py | 5 ++++- homeassistant/components/deconz/strings.json | 1 + .../components/deconz/translations/en.json | 1 + tests/components/deconz/test_config_flow.py | 16 +++++++++++++--- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/deconz/config_flow.py b/homeassistant/components/deconz/config_flow.py index 806ac72c973..af37ee96878 100644 --- a/homeassistant/components/deconz/config_flow.py +++ b/homeassistant/components/deconz/config_flow.py @@ -8,7 +8,7 @@ from typing import Any, cast from urllib.parse import urlparse import async_timeout -from pydeconz.errors import RequestError, ResponseError +from pydeconz.errors import LinkButtonNotPressed, RequestError, ResponseError from pydeconz.gateway import DeconzSession from pydeconz.utils import ( DiscoveredBridge, @@ -160,6 +160,9 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN): async with async_timeout.timeout(10): api_key = await deconz_session.get_api_key() + except LinkButtonNotPressed: + errors["base"] = "linking_not_possible" + except (ResponseError, RequestError, asyncio.TimeoutError): errors["base"] = "no_key" diff --git a/homeassistant/components/deconz/strings.json b/homeassistant/components/deconz/strings.json index 0fa929f9e63..4098951d714 100644 --- a/homeassistant/components/deconz/strings.json +++ b/homeassistant/components/deconz/strings.json @@ -23,6 +23,7 @@ } }, "error": { + "linking_not_possible": "Couldn't link with the gateway", "no_key": "Couldn't get an API key" }, "abort": { diff --git a/homeassistant/components/deconz/translations/en.json b/homeassistant/components/deconz/translations/en.json index a8e09fd20d7..16034e12414 100644 --- a/homeassistant/components/deconz/translations/en.json +++ b/homeassistant/components/deconz/translations/en.json @@ -9,6 +9,7 @@ "updated_instance": "Updated deCONZ instance with new host address" }, "error": { + "linking_not_possible": "Couldn't link with the gateway", "no_key": "Couldn't get an API key" }, "flow_title": "{host}", diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py index 8ed1e348fee..97b2f7ee164 100644 --- a/tests/components/deconz/test_config_flow.py +++ b/tests/components/deconz/test_config_flow.py @@ -4,6 +4,7 @@ import asyncio from unittest.mock import patch import pydeconz +import pytest from homeassistant.components import ssdp from homeassistant.components.deconz.config_flow import ( @@ -341,7 +342,16 @@ async def test_manual_configuration_timeout_get_bridge(hass, aioclient_mock): assert result["reason"] == "no_bridges" -async def test_link_get_api_key_ResponseError(hass, aioclient_mock): +@pytest.mark.parametrize( + "raised_error, error_string", + [ + (pydeconz.errors.LinkButtonNotPressed, "linking_not_possible"), + (asyncio.TimeoutError, "no_key"), + (pydeconz.errors.ResponseError, "no_key"), + (pydeconz.errors.RequestError, "no_key"), + ], +) +async def test_link_step_fails(hass, aioclient_mock, raised_error, error_string): """Test config flow should abort if no API key was possible to retrieve.""" aioclient_mock.get( pydeconz.utils.URL_DISCOVER, @@ -360,7 +370,7 @@ async def test_link_get_api_key_ResponseError(hass, aioclient_mock): assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" - aioclient_mock.post("http://1.2.3.4:80/api", exc=pydeconz.errors.ResponseError) + aioclient_mock.post("http://1.2.3.4:80/api", exc=raised_error) result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={} @@ -368,7 +378,7 @@ async def test_link_get_api_key_ResponseError(hass, aioclient_mock): assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" - assert result["errors"] == {"base": "no_key"} + assert result["errors"] == {"base": error_string} async def test_reauth_flow_update_configuration(hass, aioclient_mock):