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:
"""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

View File

@ -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