Update Tile unique ID to include username (#52175)

This commit is contained in:
Aaron Bach 2021-06-28 14:36:18 -05:00 committed by GitHub
parent fbf85fd86b
commit f538e07902
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 7 deletions

View file

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

View file

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

View file

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

View file

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

View file

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