Skip to content

Commit

Permalink
Move Aladdin stale device removal to init module (home-assistant#118784)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Jun 4, 2024
1 parent c0912a0 commit fce5f2a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
31 changes: 31 additions & 0 deletions homeassistant/components/aladdin_connect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.config_entry_oauth2_flow import (
OAuth2Session,
async_get_config_entry_implementation,
)

from .api import AsyncConfigEntryAuth
from .const import DOMAIN
from .coordinator import AladdinConnectCoordinator

PLATFORMS: list[Platform] = [Platform.COVER, Platform.SENSOR]
Expand All @@ -38,6 +40,8 @@ async def async_setup_entry(

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

async_remove_stale_devices(hass, entry)

return True


Expand All @@ -61,3 +65,30 @@ async def async_migrate_entry(
)

return True


def async_remove_stale_devices(
hass: HomeAssistant, config_entry: AladdinConnectConfigEntry
) -> None:
"""Remove stale devices from device registry."""
device_registry = dr.async_get(hass)
device_entries = dr.async_entries_for_config_entry(
device_registry, config_entry.entry_id
)
all_device_ids = {door.unique_id for door in config_entry.runtime_data.doors}

for device_entry in device_entries:
device_id: str | None = None

for identifier in device_entry.identifiers:
if identifier[0] == DOMAIN:
device_id = identifier[1]
break

if device_id is None or device_id not in all_device_ids:
# If device_id is None an invalid device entry was found for this config entry.
# If the device_id is not in existing device ids it's a stale device entry.
# Remove config entry from this device entry in either case.
device_registry.async_update_device(
device_entry.id, remove_config_entry_id=config_entry.entry_id
)
30 changes: 0 additions & 30 deletions homeassistant/components/aladdin_connect/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
CoverEntityFeature,
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.device_registry as dr
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import AladdinConnectConfigEntry, AladdinConnectCoordinator
from .const import DOMAIN
from .entity import AladdinConnectEntity


Expand All @@ -27,34 +25,6 @@ async def async_setup_entry(
coordinator = config_entry.runtime_data

async_add_entities(AladdinDevice(coordinator, door) for door in coordinator.doors)
remove_stale_devices(hass, config_entry)


def remove_stale_devices(
hass: HomeAssistant, config_entry: AladdinConnectConfigEntry
) -> None:
"""Remove stale devices from device registry."""
device_registry = dr.async_get(hass)
device_entries = dr.async_entries_for_config_entry(
device_registry, config_entry.entry_id
)
all_device_ids = {door.unique_id for door in config_entry.runtime_data.doors}

for device_entry in device_entries:
device_id: str | None = None

for identifier in device_entry.identifiers:
if identifier[0] == DOMAIN:
device_id = identifier[1]
break

if device_id is None or device_id not in all_device_ids:
# If device_id is None an invalid device entry was found for this config entry.
# If the device_id is not in existing device ids it's a stale device entry.
# Remove config entry from this device entry in either case.
device_registry.async_update_device(
device_entry.id, remove_config_entry_id=config_entry.entry_id
)


class AladdinDevice(AladdinConnectEntity, CoverEntity):
Expand Down

0 comments on commit fce5f2a

Please sign in to comment.