Smhi reverse change of unique id change (#74176)

This commit is contained in:
G Johansson 2022-06-29 17:13:07 +02:00 committed by GitHub
parent 4e079c4417
commit 97dcfe4445
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 31 deletions

View file

@ -7,8 +7,7 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
Platform, Platform,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries
PLATFORMS = [Platform.WEATHER] PLATFORMS = [Platform.WEATHER]
@ -40,21 +39,10 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
CONF_LONGITUDE: entry.data[CONF_LONGITUDE], 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( if not hass.config_entries.async_update_entry(entry, data=new_data):
entry, data=new_data, unique_id=new_unique_id
):
return False return False
entry.version = 2 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 return True

View file

@ -57,7 +57,7 @@ class SmhiFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
HOME_LOCATION_NAME if name == HOME_LOCATION_NAME else DEFAULT_NAME 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() self._abort_if_unique_id_configured()
return self.async_create_entry(title=name, data=user_input) return self.async_create_entry(title=name, data=user_input)

View file

@ -136,7 +136,7 @@ class SmhiWeather(WeatherEntity):
"""Initialize the SMHI weather entity.""" """Initialize the SMHI weather entity."""
self._attr_name = name 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._forecasts: list[SmhiForecast] | None = None
self._fail_count = 0 self._fail_count = 0
self._smhi_api = Smhi(longitude, latitude, session=session) self._smhi_api = Smhi(longitude, latitude, session=session)

View file

@ -9,11 +9,7 @@ from homeassistant import config_entries
from homeassistant.components.smhi.const import DOMAIN from homeassistant.components.smhi.const import DOMAIN
from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import ( from homeassistant.data_entry_flow import FlowResultType
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -27,7 +23,7 @@ async def test_form(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
assert result["type"] == RESULT_TYPE_FORM assert result["type"] == FlowResultType.FORM
assert result["errors"] == {} assert result["errors"] == {}
with patch( with patch(
@ -48,7 +44,7 @@ async def test_form(hass: HomeAssistant) -> None:
) )
await hass.async_block_till_done() 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["title"] == "Home"
assert result2["data"] == { assert result2["data"] == {
"location": { "location": {
@ -81,7 +77,7 @@ async def test_form(hass: HomeAssistant) -> None:
) )
await hass.async_block_till_done() 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["title"] == "Weather 1.0 1.0"
assert result4["data"] == { assert result4["data"] == {
"location": { "location": {
@ -113,7 +109,7 @@ async def test_form_invalid_coordinates(hass: HomeAssistant) -> None:
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_FORM assert result2["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": "wrong_location"} assert result2["errors"] == {"base": "wrong_location"}
# Continue flow with new coordinates # Continue flow with new coordinates
@ -135,7 +131,7 @@ async def test_form_invalid_coordinates(hass: HomeAssistant) -> None:
) )
await hass.async_block_till_done() 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["title"] == "Weather 2.0 2.0"
assert result3["data"] == { assert result3["data"] == {
"location": { "location": {
@ -150,7 +146,7 @@ async def test_form_unique_id_exist(hass: HomeAssistant) -> None:
"""Test we handle unique id already exist.""" """Test we handle unique id already exist."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
unique_id="smhi-1.0-1.0", unique_id="1.0-1.0",
data={ data={
"location": { "location": {
"latitude": 1.0, "latitude": 1.0,
@ -179,5 +175,5 @@ async def test_form_unique_id_exist(hass: HomeAssistant) -> None:
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_ABORT assert result2["type"] == FlowResultType.ABORT
assert result2["reason"] == "already_configured" assert result2["reason"] == "already_configured"

View file

@ -1,4 +1,6 @@
"""Test SMHI component setup process.""" """Test SMHI component setup process."""
from unittest.mock import patch
from smhi.smhi_lib import APIURL_TEMPLATE from smhi.smhi_lib import APIURL_TEMPLATE
from homeassistant.components.smhi.const import DOMAIN from homeassistant.components.smhi.const import DOMAIN
@ -56,7 +58,7 @@ async def test_remove_entry(
async def test_migrate_entry( async def test_migrate_entry(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str
) -> None: ) -> None:
"""Test migrate entry and entities unique id.""" """Test migrate entry data."""
uri = APIURL_TEMPLATE.format( uri = APIURL_TEMPLATE.format(
TEST_CONFIG_MIGRATE["longitude"], TEST_CONFIG_MIGRATE["latitude"] TEST_CONFIG_MIGRATE["longitude"], TEST_CONFIG_MIGRATE["latitude"]
) )
@ -82,7 +84,29 @@ async def test_migrate_entry(
assert state assert state
assert entry.version == 2 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) 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