Set unique id for config entry in smhi (#63547)
This commit is contained in:
parent
b7785eb188
commit
6750614655
5 changed files with 58 additions and 1 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
{
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]"
|
||||
},
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "Location in Sweden",
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
{
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "Account is already configured"
|
||||
},
|
||||
"error": {
|
||||
"name_exists": "Name already exists",
|
||||
"wrong_location": "Location Sweden only"
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Add table
Reference in a new issue