diff --git a/homeassistant/components/nmbs/sensor.py b/homeassistant/components/nmbs/sensor.py index ffb539a39a0..eea1745fcc1 100644 --- a/homeassistant/components/nmbs/sensor.py +++ b/homeassistant/components/nmbs/sensor.py @@ -207,7 +207,10 @@ class NMBSLiveBoard(SensorEntity): def unique_id(self) -> str: """Return the unique ID.""" - unique_id = f"{self._station}_{self._station_from}_{self._station_to}" + unique_id = ( + f"{self._station["id"]}_{self._station_from["id"]}_" + f"{self._station_to["id"]}" + ) return f"nmbs_live_{unique_id}" @property diff --git a/tests/components/nmbs/test_config_flow.py b/tests/components/nmbs/test_config_flow.py index 08ecfbfd136..448bcdfbc51 100644 --- a/tests/components/nmbs/test_config_flow.py +++ b/tests/components/nmbs/test_config_flow.py @@ -243,15 +243,21 @@ async def test_invalid_station_name( async def test_sensor_id_migration_standardname( hass: HomeAssistant, mock_nmbs_client: AsyncMock, - mock_config_entry: MockConfigEntry, entity_registry: er.EntityRegistry, ) -> None: """Test migrating unique id.""" - entity_registry.async_get_or_create( - SENSOR_DOMAIN, - DOMAIN, - f"live_{DUMMY_DATA_IMPORT["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA_IMPORT["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA_IMPORT["STAT_BRUSSELS_SOUTH"]}", - config_entry=mock_config_entry, + old_unique_id = ( + f"live_{DUMMY_DATA_IMPORT["STAT_BRUSSELS_NORTH"]}_" + f"{DUMMY_DATA_IMPORT["STAT_BRUSSELS_NORTH"]}_" + f"{DUMMY_DATA_IMPORT["STAT_BRUSSELS_SOUTH"]}" + ) + new_unique_id = ( + f"nmbs_live_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_" + f"{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_" + f"{DUMMY_DATA["STAT_BRUSSELS_SOUTH"]}" + ) + old_entry = entity_registry.async_get_or_create( + SENSOR_DOMAIN, DOMAIN, old_unique_id ) result = await hass.config_entries.flow.async_init( DOMAIN, @@ -264,29 +270,34 @@ async def test_sensor_id_migration_standardname( ) assert result["type"] is FlowResultType.CREATE_ENTRY + config_entry_id = result["result"].entry_id await hass.async_block_till_done() - entities = er.async_entries_for_config_entry( - entity_registry, mock_config_entry.entry_id - ) - assert len(entities) == 1 - assert ( - entities[0].unique_id - == f"nmbs_live_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA["STAT_BRUSSELS_SOUTH"]}" - ) + entities = er.async_entries_for_config_entry(entity_registry, config_entry_id) + assert len(entities) == 3 + entities_map = {entity.unique_id: entity for entity in entities} + assert old_unique_id not in entities_map + assert new_unique_id in entities_map + assert entities_map[new_unique_id].id == old_entry.id async def test_sensor_id_migration_localized_name( hass: HomeAssistant, mock_nmbs_client: AsyncMock, - mock_config_entry: MockConfigEntry, entity_registry: er.EntityRegistry, ) -> None: """Test migrating unique id.""" - entity_registry.async_get_or_create( - SENSOR_DOMAIN, - DOMAIN, - f"live_{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_SOUTH"]}", - config_entry=mock_config_entry, + old_unique_id = ( + f"live_{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_NORTH"]}_" + f"{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_NORTH"]}_" + f"{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_SOUTH"]}" + ) + new_unique_id = ( + f"nmbs_live_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_" + f"{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_" + f"{DUMMY_DATA["STAT_BRUSSELS_SOUTH"]}" + ) + old_entry = entity_registry.async_get_or_create( + SENSOR_DOMAIN, DOMAIN, old_unique_id ) result = await hass.config_entries.flow.async_init( DOMAIN, @@ -299,12 +310,11 @@ async def test_sensor_id_migration_localized_name( ) assert result["type"] is FlowResultType.CREATE_ENTRY + config_entry_id = result["result"].entry_id await hass.async_block_till_done() - entities = er.async_entries_for_config_entry( - entity_registry, mock_config_entry.entry_id - ) - assert len(entities) == 1 - assert ( - entities[0].unique_id - == f"nmbs_live_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA["STAT_BRUSSELS_SOUTH"]}" - ) + entities = er.async_entries_for_config_entry(entity_registry, config_entry_id) + assert len(entities) == 3 + entities_map = {entity.unique_id: entity for entity in entities} + assert old_unique_id not in entities_map + assert new_unique_id in entities_map + assert entities_map[new_unique_id].id == old_entry.id