diff --git a/homeassistant/components/smhi/__init__.py b/homeassistant/components/smhi/__init__.py index e3f55904b77674a9e99ce0beee939589f4876ed4..98c8de870322ab268e47645aead992192219bc3f 100644 --- a/homeassistant/components/smhi/__init__.py +++ b/homeassistant/components/smhi/__init__.py @@ -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 diff --git a/homeassistant/components/smhi/config_flow.py b/homeassistant/components/smhi/config_flow.py index 8d338e3eb3e16fb2b86b729f56acd89769d91cb4..bfa38f317a95b213e10d4aaf8182421213614cce 100644 --- a/homeassistant/components/smhi/config_flow.py +++ b/homeassistant/components/smhi/config_flow.py @@ -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) diff --git a/homeassistant/components/smhi/weather.py b/homeassistant/components/smhi/weather.py index 5d1f3e3c87ee0fef6a57e4666e4ba9f53a6ab446..d7df54957c063b936b26d587428b2814a7448d7f 100644 --- a/homeassistant/components/smhi/weather.py +++ b/homeassistant/components/smhi/weather.py @@ -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) diff --git a/tests/components/smhi/test_config_flow.py b/tests/components/smhi/test_config_flow.py index ab3e36f81de14153eaa1b7436fa40a774b2670d0..f33849694c8fc7a0a72ae28a25795d96aaf5033a 100644 --- a/tests/components/smhi/test_config_flow.py +++ b/tests/components/smhi/test_config_flow.py @@ -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" diff --git a/tests/components/smhi/test_init.py b/tests/components/smhi/test_init.py index ea6d55fabf6337d19a4d737d9b2014c50d5fe8ca..ec6e4c417bb719b3f476c0659367f52316764c7d 100644 --- a/tests/components/smhi/test_init.py +++ b/tests/components/smhi/test_init.py @@ -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