From 8d7744a74f6f28657941dc095fb5a197e726ffff Mon Sep 17 00:00:00 2001 From: jan iversen Date: Wed, 13 Oct 2021 07:16:55 +0200 Subject: [PATCH] Warn user if Gateway is already paired (#57530) * Warn user if Gateway is already paired. Co-authored-by: Martin Hjelmare --- .../components/tradfri/config_flow.py | 3 ++- homeassistant/components/tradfri/strings.json | 3 ++- tests/components/tradfri/test_config_flow.py | 22 ++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/tradfri/config_flow.py b/homeassistant/components/tradfri/config_flow.py index e45bd36753f..11a56200eda 100644 --- a/homeassistant/components/tradfri/config_flow.py +++ b/homeassistant/components/tradfri/config_flow.py @@ -182,7 +182,8 @@ async def authenticate( raise AuthError("timeout") from err finally: await api_factory.shutdown() - + if key is None: + raise AuthError("cannot_authenticate") return await get_gateway_info(hass, host, identity, key) diff --git a/homeassistant/components/tradfri/strings.json b/homeassistant/components/tradfri/strings.json index 45850fd639a..34d7e89929a 100644 --- a/homeassistant/components/tradfri/strings.json +++ b/homeassistant/components/tradfri/strings.json @@ -13,7 +13,8 @@ "error": { "invalid_key": "Failed to register with provided key. If this keeps happening, try restarting the gateway.", "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", - "timeout": "Timeout validating the code." + "timeout": "Timeout validating the code.", + "cannot_authenticate": "Cannot authenticate, is Gateway paired with another server like e.g. Homekit?" }, "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", diff --git a/tests/components/tradfri/test_config_flow.py b/tests/components/tradfri/test_config_flow.py index ca6380a9310..c8c5323d6e4 100644 --- a/tests/components/tradfri/test_config_flow.py +++ b/tests/components/tradfri/test_config_flow.py @@ -1,5 +1,5 @@ """Test the Tradfri config flow.""" -from unittest.mock import patch +from unittest.mock import AsyncMock, patch import pytest @@ -18,6 +18,26 @@ def mock_auth(): yield mock_auth +async def test_already_paired(hass, mock_entry_setup): + """Test Gateway already paired.""" + with patch( + "homeassistant.components.tradfri.config_flow.APIFactory", + autospec=True, + ) as mock_lib: + mx = AsyncMock() + mx.generate_psk.return_value = None + mock_lib.init.return_value = mx + result = await hass.config_entries.flow.async_init( + "tradfri", context={"source": config_entries.SOURCE_USER} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"host": "123.123.123.123", "security_code": "abcd"} + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["errors"] == {"base": "cannot_authenticate"} + + async def test_user_connection_successful(hass, mock_auth, mock_entry_setup): """Test a successful connection.""" mock_auth.side_effect = lambda hass, host, code: {"host": host, "gateway_id": "bla"}