diff --git a/homeassistant/components/tile/__init__.py b/homeassistant/components/tile/__init__.py index 91e1567cd65..2f00dabbcb6 100644 --- a/homeassistant/components/tile/__init__.py +++ b/homeassistant/components/tile/__init__.py @@ -7,7 +7,7 @@ from pytile.errors import InvalidAuthError, SessionExpiredError, TileError from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers import aiohttp_client +from homeassistant.helpers import aiohttp_client, entity_registry from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.util.async_ import gather_with_concurrency @@ -33,6 +33,31 @@ async def async_setup_entry(hass, entry): hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id] = {} hass.data[DOMAIN][DATA_TILE][entry.entry_id] = {} + # The existence of shared Tiles across multiple accounts requires an entity ID + # change: + # + # Old: tile_{uuid} + # New: {username}_{uuid} + # + # Find any entities with the old format and update them: + ent_reg = entity_registry.async_get(hass) + for entity in [ + e + for e in ent_reg.entities.values() + if e.config_entry_id == entry.entry_id + and not e.unique_id.startswith(entry.data[CONF_USERNAME]) + ]: + new_unique_id = f"{entry.data[CONF_USERNAME]}_".join( + entity.unique_id.split(f"{DOMAIN}_") + ) + LOGGER.debug( + "Migrating entity %s from old unique ID '%s' to new unique ID '%s'", + entity.entity_id, + entity.unique_id, + new_unique_id, + ) + ent_reg.async_update_entity(entity.entity_id, new_unique_id=new_unique_id) + websession = aiohttp_client.async_get_clientsession(hass) try: diff --git a/homeassistant/components/tile/device_tracker.py b/homeassistant/components/tile/device_tracker.py index 7571e235ef1..add6e5f94a0 100644 --- a/homeassistant/components/tile/device_tracker.py +++ b/homeassistant/components/tile/device_tracker.py @@ -30,7 +30,9 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities( [ TileDeviceTracker( - hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id][tile_uuid], tile + entry, + hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id][tile_uuid], + tile, ) for tile_uuid, tile in hass.data[DOMAIN][DATA_TILE][entry.entry_id].items() ] @@ -61,10 +63,11 @@ async def async_setup_scanner(hass, config, async_see, discovery_info=None): class TileDeviceTracker(CoordinatorEntity, TrackerEntity): """Representation of a network infrastructure device.""" - def __init__(self, coordinator, tile): + def __init__(self, entry, coordinator, tile): """Initialize.""" super().__init__(coordinator) self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} + self._entry = entry self._tile = tile @property @@ -116,7 +119,7 @@ class TileDeviceTracker(CoordinatorEntity, TrackerEntity): @property def unique_id(self): """Return the unique ID of the entity.""" - return f"tile_{self._tile.uuid}" + return f"{self._entry.data[CONF_USERNAME]}_{self._tile.uuid}" @property def source_type(self): diff --git a/homeassistant/components/tile/manifest.json b/homeassistant/components/tile/manifest.json index a17c099509e..e8d386f4a88 100644 --- a/homeassistant/components/tile/manifest.json +++ b/homeassistant/components/tile/manifest.json @@ -3,7 +3,7 @@ "name": "Tile", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/tile", - "requirements": ["pytile==5.2.0"], + "requirements": ["pytile==5.2.2"], "codeowners": ["@bachya"], "iot_class": "cloud_polling" } diff --git a/requirements_all.txt b/requirements_all.txt index ee617b75b74..8e422d67a16 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1918,7 +1918,7 @@ python_opendata_transport==0.2.1 pythonegardia==1.0.40 # homeassistant.components.tile -pytile==5.2.0 +pytile==5.2.2 # homeassistant.components.touchline pytouchline==0.7 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 3a46d378017..22efc23a38b 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1061,7 +1061,7 @@ python-velbus==2.1.2 python_awair==0.2.1 # homeassistant.components.tile -pytile==5.2.0 +pytile==5.2.2 # homeassistant.components.traccar pytraccar==0.9.0