Fix nmbs sensor unique_id (#135576)

This commit is contained in:
Erik Montnemery 2025-01-14 10:24:48 +01:00 committed by GitHub
parent 6d7e9f10d9
commit d333fa320f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 29 deletions

View File

@ -207,7 +207,10 @@ class NMBSLiveBoard(SensorEntity):
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return the unique ID.""" """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}" return f"nmbs_live_{unique_id}"
@property @property

View File

@ -243,15 +243,21 @@ async def test_invalid_station_name(
async def test_sensor_id_migration_standardname( async def test_sensor_id_migration_standardname(
hass: HomeAssistant, hass: HomeAssistant,
mock_nmbs_client: AsyncMock, mock_nmbs_client: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test migrating unique id.""" """Test migrating unique id."""
entity_registry.async_get_or_create( old_unique_id = (
SENSOR_DOMAIN, f"live_{DUMMY_DATA_IMPORT["STAT_BRUSSELS_NORTH"]}_"
DOMAIN, f"{DUMMY_DATA_IMPORT["STAT_BRUSSELS_NORTH"]}_"
f"live_{DUMMY_DATA_IMPORT["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA_IMPORT["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA_IMPORT["STAT_BRUSSELS_SOUTH"]}", f"{DUMMY_DATA_IMPORT["STAT_BRUSSELS_SOUTH"]}"
config_entry=mock_config_entry, )
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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -264,29 +270,34 @@ async def test_sensor_id_migration_standardname(
) )
assert result["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
config_entry_id = result["result"].entry_id
await hass.async_block_till_done() await hass.async_block_till_done()
entities = er.async_entries_for_config_entry( entities = er.async_entries_for_config_entry(entity_registry, config_entry_id)
entity_registry, mock_config_entry.entry_id assert len(entities) == 3
) entities_map = {entity.unique_id: entity for entity in entities}
assert len(entities) == 1 assert old_unique_id not in entities_map
assert ( assert new_unique_id in entities_map
entities[0].unique_id assert entities_map[new_unique_id].id == old_entry.id
== f"nmbs_live_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA["STAT_BRUSSELS_SOUTH"]}"
)
async def test_sensor_id_migration_localized_name( async def test_sensor_id_migration_localized_name(
hass: HomeAssistant, hass: HomeAssistant,
mock_nmbs_client: AsyncMock, mock_nmbs_client: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test migrating unique id.""" """Test migrating unique id."""
entity_registry.async_get_or_create( old_unique_id = (
SENSOR_DOMAIN, f"live_{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_NORTH"]}_"
DOMAIN, f"{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_NORTH"]}_"
f"live_{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_SOUTH"]}", f"{DUMMY_DATA_ALTERNATIVE_IMPORT["STAT_BRUSSELS_SOUTH"]}"
config_entry=mock_config_entry, )
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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -299,12 +310,11 @@ async def test_sensor_id_migration_localized_name(
) )
assert result["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
config_entry_id = result["result"].entry_id
await hass.async_block_till_done() await hass.async_block_till_done()
entities = er.async_entries_for_config_entry( entities = er.async_entries_for_config_entry(entity_registry, config_entry_id)
entity_registry, mock_config_entry.entry_id assert len(entities) == 3
) entities_map = {entity.unique_id: entity for entity in entities}
assert len(entities) == 1 assert old_unique_id not in entities_map
assert ( assert new_unique_id in entities_map
entities[0].unique_id assert entities_map[new_unique_id].id == old_entry.id
== f"nmbs_live_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA["STAT_BRUSSELS_NORTH"]}_{DUMMY_DATA["STAT_BRUSSELS_SOUTH"]}"
)