Smhi reverse change of unique id change (#74176)
This commit is contained in:
parent
4e079c4417
commit
97dcfe4445
5 changed files with 39 additions and 31 deletions
|
@ -7,8 +7,7 @@ from homeassistant.const import (
|
|||
CONF_NAME,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
PLATFORMS = [Platform.WEATHER]
|
||||
|
||||
|
@ -40,21 +39,10 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
CONF_LONGITUDE: entry.data[CONF_LONGITUDE],
|
||||
},
|
||||
}
|
||||
new_unique_id = f"smhi-{entry.data[CONF_LATITUDE]}-{entry.data[CONF_LONGITUDE]}"
|
||||
|
||||
if not hass.config_entries.async_update_entry(
|
||||
entry, data=new_data, unique_id=new_unique_id
|
||||
):
|
||||
if not hass.config_entries.async_update_entry(entry, data=new_data):
|
||||
return False
|
||||
|
||||
entry.version = 2
|
||||
new_unique_id_entity = f"smhi-{entry.data[CONF_LOCATION][CONF_LATITUDE]}-{entry.data[CONF_LOCATION][CONF_LONGITUDE]}"
|
||||
|
||||
@callback
|
||||
def update_unique_id(entity_entry: RegistryEntry) -> dict[str, str]:
|
||||
"""Update unique ID of entity entry."""
|
||||
return {"new_unique_id": new_unique_id_entity}
|
||||
|
||||
await async_migrate_entries(hass, entry.entry_id, update_unique_id)
|
||||
|
||||
return True
|
||||
|
|
|
@ -57,7 +57,7 @@ class SmhiFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
HOME_LOCATION_NAME if name == HOME_LOCATION_NAME else DEFAULT_NAME
|
||||
)
|
||||
|
||||
await self.async_set_unique_id(f"smhi-{lat}-{lon}")
|
||||
await self.async_set_unique_id(f"{lat}-{lon}")
|
||||
self._abort_if_unique_id_configured()
|
||||
return self.async_create_entry(title=name, data=user_input)
|
||||
|
||||
|
|
|
@ -136,7 +136,7 @@ class SmhiWeather(WeatherEntity):
|
|||
"""Initialize the SMHI weather entity."""
|
||||
|
||||
self._attr_name = name
|
||||
self._attr_unique_id = f"smhi-{latitude}-{longitude}"
|
||||
self._attr_unique_id = f"{latitude}, {longitude}"
|
||||
self._forecasts: list[SmhiForecast] | None = None
|
||||
self._fail_count = 0
|
||||
self._smhi_api = Smhi(longitude, latitude, session=session)
|
||||
|
|
|
@ -9,11 +9,7 @@ from homeassistant import config_entries
|
|||
from homeassistant.components.smhi.const import DOMAIN
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import (
|
||||
RESULT_TYPE_ABORT,
|
||||
RESULT_TYPE_CREATE_ENTRY,
|
||||
RESULT_TYPE_FORM,
|
||||
)
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
@ -27,7 +23,7 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
|
@ -48,7 +44,7 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Home"
|
||||
assert result2["data"] == {
|
||||
"location": {
|
||||
|
@ -81,7 +77,7 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result4["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result4["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result4["title"] == "Weather 1.0 1.0"
|
||||
assert result4["data"] == {
|
||||
"location": {
|
||||
|
@ -113,7 +109,7 @@ async def test_form_invalid_coordinates(hass: HomeAssistant) -> None:
|
|||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == RESULT_TYPE_FORM
|
||||
assert result2["type"] == FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "wrong_location"}
|
||||
|
||||
# Continue flow with new coordinates
|
||||
|
@ -135,7 +131,7 @@ async def test_form_invalid_coordinates(hass: HomeAssistant) -> None:
|
|||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result3["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result3["title"] == "Weather 2.0 2.0"
|
||||
assert result3["data"] == {
|
||||
"location": {
|
||||
|
@ -150,7 +146,7 @@ async def test_form_unique_id_exist(hass: HomeAssistant) -> None:
|
|||
"""Test we handle unique id already exist."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="smhi-1.0-1.0",
|
||||
unique_id="1.0-1.0",
|
||||
data={
|
||||
"location": {
|
||||
"latitude": 1.0,
|
||||
|
@ -179,5 +175,5 @@ async def test_form_unique_id_exist(hass: HomeAssistant) -> None:
|
|||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == RESULT_TYPE_ABORT
|
||||
assert result2["type"] == FlowResultType.ABORT
|
||||
assert result2["reason"] == "already_configured"
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
"""Test SMHI component setup process."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from smhi.smhi_lib import APIURL_TEMPLATE
|
||||
|
||||
from homeassistant.components.smhi.const import DOMAIN
|
||||
|
@ -56,7 +58,7 @@ async def test_remove_entry(
|
|||
async def test_migrate_entry(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str
|
||||
) -> None:
|
||||
"""Test migrate entry and entities unique id."""
|
||||
"""Test migrate entry data."""
|
||||
uri = APIURL_TEMPLATE.format(
|
||||
TEST_CONFIG_MIGRATE["longitude"], TEST_CONFIG_MIGRATE["latitude"]
|
||||
)
|
||||
|
@ -82,7 +84,29 @@ async def test_migrate_entry(
|
|||
assert state
|
||||
|
||||
assert entry.version == 2
|
||||
assert entry.unique_id == "smhi-17.84197-17.84197"
|
||||
assert entry.unique_id == "17.84197-17.84197"
|
||||
|
||||
entity_get = entity_reg.async_get(entity.entity_id)
|
||||
assert entity_get.unique_id == "smhi-17.84197-17.84197"
|
||||
assert entity_get.unique_id == "17.84197, 17.84197"
|
||||
|
||||
|
||||
async def test_migrate_entry_failed(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str
|
||||
) -> None:
|
||||
"""Test migrate entry data that fails."""
|
||||
uri = APIURL_TEMPLATE.format(
|
||||
TEST_CONFIG_MIGRATE["longitude"], TEST_CONFIG_MIGRATE["latitude"]
|
||||
)
|
||||
aioclient_mock.get(uri, text=api_response)
|
||||
entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG_MIGRATE)
|
||||
entry.add_to_hass(hass)
|
||||
assert entry.version == 1
|
||||
|
||||
with patch(
|
||||
"homeassistant.config_entries.ConfigEntries.async_update_entry",
|
||||
return_value=False,
|
||||
):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.version == 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue