From 6750614655f78561c852ad09ab428408e08ce039 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sat, 15 Jan 2022 20:01:48 +0100 Subject: [PATCH] Set unique id for config entry in smhi (#63547) --- homeassistant/components/smhi/__init__.py | 8 +++- homeassistant/components/smhi/config_flow.py | 4 ++ homeassistant/components/smhi/strings.json | 3 ++ .../components/smhi/translations/en.json | 3 ++ tests/components/smhi/test_config_flow.py | 41 +++++++++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/smhi/__init__.py b/homeassistant/components/smhi/__init__.py index 9625d15f92a..398932bf4d6 100644 --- a/homeassistant/components/smhi/__init__.py +++ b/homeassistant/components/smhi/__init__.py @@ -1,6 +1,6 @@ """Support for the Swedish weather institute weather service.""" from homeassistant.config_entries import ConfigEntry -from homeassistant.const import Platform +from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, Platform from homeassistant.core import HomeAssistant PLATFORMS = [Platform.WEATHER] @@ -8,6 +8,12 @@ PLATFORMS = [Platform.WEATHER] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up SMHI forecast as config entry.""" + + # Setting unique id where missing + if entry.unique_id is None: + unique_id = f"{entry.data[CONF_LATITUDE]}-{entry.data[CONF_LONGITUDE]}" + hass.config_entries.async_update_entry(entry, unique_id=unique_id) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True diff --git a/homeassistant/components/smhi/config_flow.py b/homeassistant/components/smhi/config_flow.py index 16be0c9fda4..5fb95b8c9e1 100644 --- a/homeassistant/components/smhi/config_flow.py +++ b/homeassistant/components/smhi/config_flow.py @@ -48,6 +48,10 @@ class SmhiFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): if is_ok: name = slugify(user_input[CONF_NAME]) if not self._name_in_configuration_exists(name): + latitude = user_input[CONF_LATITUDE] + longitude = user_input[CONF_LONGITUDE] + await self.async_set_unique_id(f"{latitude}-{longitude}") + self._abort_if_unique_id_configured() return self.async_create_entry( title=user_input[CONF_NAME], data=user_input ) diff --git a/homeassistant/components/smhi/strings.json b/homeassistant/components/smhi/strings.json index 0bba0c2ab48..7f19d4b2602 100644 --- a/homeassistant/components/smhi/strings.json +++ b/homeassistant/components/smhi/strings.json @@ -1,5 +1,8 @@ { "config": { + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_account%]" + }, "step": { "user": { "title": "Location in Sweden", diff --git a/homeassistant/components/smhi/translations/en.json b/homeassistant/components/smhi/translations/en.json index 904ade6ef0a..5400122be8a 100644 --- a/homeassistant/components/smhi/translations/en.json +++ b/homeassistant/components/smhi/translations/en.json @@ -1,5 +1,8 @@ { "config": { + "abort": { + "already_configured": "Account is already configured" + }, "error": { "name_exists": "Name already exists", "wrong_location": "Location Sweden only" diff --git a/tests/components/smhi/test_config_flow.py b/tests/components/smhi/test_config_flow.py index 3f189b52311..4f00d40cbef 100644 --- a/tests/components/smhi/test_config_flow.py +++ b/tests/components/smhi/test_config_flow.py @@ -4,7 +4,13 @@ from unittest.mock import Mock, patch from smhi.smhi_lib import Smhi as SmhiApi, SmhiForecastException from homeassistant.components.smhi import config_flow +from homeassistant.components.smhi.const import DOMAIN +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import RESULT_TYPE_ABORT + +from tests.common import MockConfigEntry # pylint: disable=protected-access @@ -200,6 +206,8 @@ async def test_flow_entry_created_from_user_input() -> None: return_value={"test": "something", "name_exist": "config"}, ), patch.object( flow, "_check_location", return_value=True + ), patch.object( + flow, "async_set_unique_id", return_value=None ): result = await flow.async_step_user(user_input=test_data) @@ -266,3 +274,36 @@ async def test_check_location_faulty() -> None: ), patch.object(SmhiApi, "async_get_forecast", side_effect=SmhiForecastException()): assert await flow._check_location("58", "17") is False + + +async def test_flow_unique_id_not_unique(hass: HomeAssistant) -> None: + """Test that unique id is unique.""" + + test_data = {"name": "home", CONF_LONGITUDE: "0", CONF_LATITUDE: "0"} + + MockConfigEntry( + domain=DOMAIN, + data={"name": "away", CONF_LONGITUDE: "0", CONF_LATITUDE: "0"}, + unique_id="0.0-0.0", + ).add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + with patch( + "homeassistant.components.smhi.async_setup_entry", + return_value=True, + ), patch( + "homeassistant.components.smhi.config_flow.Smhi.async_get_forecast", + return_value={"kalle": "anka"}, + ): + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + test_data, + ) + await hass.async_block_till_done() + + assert result2["type"] == RESULT_TYPE_ABORT + assert result2["reason"] == "already_configured"