Fix linking issue when deCONZ gateway is not unlocked (#71082)

This commit is contained in:
Robert Svensson 2022-04-30 00:34:33 +02:00 committed by Paulus Schoutsen
parent b3e97577fd
commit 05d3ca7e02
4 changed files with 19 additions and 4 deletions

View File

@ -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"

View File

@ -23,6 +23,7 @@
}
},
"error": {
"linking_not_possible": "Couldn't link with the gateway",
"no_key": "Couldn't get an API key"
},
"abort": {

View File

@ -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}",

View File

@ -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):