mirror of
https://github.com/home-assistant/core.git
synced 2025-08-01 17:48:26 +00:00
Set unique id for config entry in smhi (#63547)
This commit is contained in:
parent
b7785eb188
commit
6750614655
@ -1,6 +1,6 @@
|
|||||||
"""Support for the Swedish weather institute weather service."""
|
"""Support for the Swedish weather institute weather service."""
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
PLATFORMS = [Platform.WEATHER]
|
PLATFORMS = [Platform.WEATHER]
|
||||||
@ -8,6 +8,12 @@ PLATFORMS = [Platform.WEATHER]
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up SMHI forecast as config entry."""
|
"""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)
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -48,6 +48,10 @@ class SmhiFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
if is_ok:
|
if is_ok:
|
||||||
name = slugify(user_input[CONF_NAME])
|
name = slugify(user_input[CONF_NAME])
|
||||||
if not self._name_in_configuration_exists(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(
|
return self.async_create_entry(
|
||||||
title=user_input[CONF_NAME], data=user_input
|
title=user_input[CONF_NAME], data=user_input
|
||||||
)
|
)
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
|
"abort": {
|
||||||
|
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]"
|
||||||
|
},
|
||||||
"step": {
|
"step": {
|
||||||
"user": {
|
"user": {
|
||||||
"title": "Location in Sweden",
|
"title": "Location in Sweden",
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
|
"abort": {
|
||||||
|
"already_configured": "Account is already configured"
|
||||||
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"name_exists": "Name already exists",
|
"name_exists": "Name already exists",
|
||||||
"wrong_location": "Location Sweden only"
|
"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 smhi.smhi_lib import Smhi as SmhiApi, SmhiForecastException
|
||||||
|
|
||||||
from homeassistant.components.smhi import config_flow
|
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.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
|
# 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"},
|
return_value={"test": "something", "name_exist": "config"},
|
||||||
), patch.object(
|
), patch.object(
|
||||||
flow, "_check_location", return_value=True
|
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)
|
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()):
|
), patch.object(SmhiApi, "async_get_forecast", side_effect=SmhiForecastException()):
|
||||||
|
|
||||||
assert await flow._check_location("58", "17") is False
|
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…
x
Reference in New Issue
Block a user