Skip to content

Commit

Permalink
Stabilize entity unique ids
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaradclimber committed Aug 26, 2023
1 parent 543f0fa commit 79c77ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion custom_components/vigieau/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
_LOGGER = logging.getLogger(__name__)

MIGRATED_FROM_VERSION_1 = "migrated_from_version_1"
MIGRATED_FROM_VERSION_3 = "migrated_from_version_3"


async def async_migrate_entry(hass, config_entry: ConfigEntry):
Expand Down Expand Up @@ -77,6 +78,15 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
config_entry.version = 3
hass.config_entries.async_update_entry(config_entry, data=new)

if config_entry.version == 3:
_LOGGER.warn("config entry version is 3, migrating to version 4")
new = {**config_entry.data}
insee_code, city_name, lat, lon = await get_insee_code_fromcoord(hass)
new[MIGRATED_FROM_VERSION_3] = True
config_entry.version = 4
hass.config_entries.async_update_entry(config_entry, data=new)


return True


Expand Down Expand Up @@ -274,8 +284,10 @@ def __init__(
self._config = description
if MIGRATED_FROM_VERSION_1 in config_entry.data:
self._attr_unique_id = f"sensor-vigieau-{self._config.key}"
else:
elif MIGRATED_FROM_VERSION_3 in config_entry.data:
self._attr_unique_id = f"sensor-vigieau-{self._attr_name}-{config_entry.data.get(CONF_INSEE_CODE)}-{config_entry.data.get(CONF_LATITUDE)}-{config_entry.data.get(CONF_LONGITUDE)}"
else:
self._attr_unique_id = f"sensor-vigieau-{self._config.key}-{config_entry.data.get(CONF_INSEE_CODE)}-{config_entry.data.get(CONF_LATITUDE)}-{config_entry.data.get(CONF_LONGITUDE)}"
self._attr_device_info = DeviceInfo(
name=f"{NAME} {config_entry.data.get(CONF_CITY)}",
entry_type=DeviceEntryType.SERVICE,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/vigieau/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _build_place_key(city) -> str:


class SetupConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 3
VERSION = 4

def __init__(self):
"""Initialize"""
Expand Down

0 comments on commit 79c77ce

Please sign in to comment.