Skip to content

Commit

Permalink
Fix unique ID when there is no _ in the old id
Browse files Browse the repository at this point in the history
  • Loading branch information
arjenbos committed Dec 11, 2024
1 parent 102927d commit 012e3cf
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions custom_components/postnl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
import urllib3
from aiohttp.client_exceptions import ClientError, ClientResponseError
from gql.transport.exceptions import TransportQueryError
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import (ConfigEntryAuthFailed,
ConfigEntryNotReady, HomeAssistantError)
from homeassistant.exceptions import (ConfigEntryNotReady, HomeAssistantError)
from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.config_entry_oauth2_flow import (
OAuth2Session, async_get_config_entry_implementation)

from .const import DOMAIN, PLATFORMS
from .graphql import PostNLGraphql
from .login_api import PostNLLoginAPI
from homeassistant.helpers import device_registry as dr, entity_registry as er

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,22 +58,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> True:
entity_registry = er.async_get(hass)

for device_entry in dr.async_entries_for_config_entry(
device_registry, entry.entry_id
device_registry, entry.entry_id
):
if (
device_entry.identifiers == {(DOMAIN, userinfo.get('account_id'))}
device_entry.identifiers == {(DOMAIN, userinfo.get('account_id'))}
):
_LOGGER.debug(
"Migrating entry %s"
"Migrating entry %s", device_entry.identifiers
)
for entity_entry in er.async_entries_for_device(
entity_registry, device_entry.id, True
entity_registry, device_entry.id, True
):
_LOGGER.debug('Migrating entity: %s', entity_entry.unique_id)
if entity_entry.unique_id.startswith(userinfo.get('account_id')):
continue

unique_id_parts = entity_entry.unique_id.split("_")
entity_new_unique_id = userinfo.get('account_id') + "_" + unique_id_parts[1]
entity_new_unique_id = userinfo.get('account_id') + "_" + (
unique_id_parts[1] if len(unique_id_parts) > 1 else unique_id_parts[0])
_LOGGER.debug('New unique ID for entity: %s', entity_new_unique_id)
entity_registry.async_update_entity(
entity_id=entity_entry.entity_id, new_unique_id=entity_new_unique_id
)
Expand Down

0 comments on commit 012e3cf

Please sign in to comment.