Skip to content

Commit

Permalink
Remove BackBox objects from device registry, special-case parent rela…
Browse files Browse the repository at this point in the history
…tionship
  • Loading branch information
loopj committed Jan 8, 2025
1 parent 9c60ff8 commit bdad37f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 4 additions & 0 deletions custom_components/vantage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .device import async_setup_devices
from .entity import async_cleanup_entities
from .events import async_setup_events
from .migrate import async_migrate_data
from .services import async_register_services

PLATFORMS: list[Platform] = [
Expand Down Expand Up @@ -110,6 +111,9 @@ async def handle_system_program_event(
# retrying set up later.
raise ConfigEntryNotReady from err

# Run any migrations
await async_migrate_data(hass, entry)

return True


Expand Down
7 changes: 5 additions & 2 deletions custom_components/vantage/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def handle_device_event(event_type: VantageEvent, obj: T, _data: Any) -> None:
entry.async_on_unload(controller.subscribe(handle_device_event))

# Register "parent" devices (controllers, modules, port devices, and stations)
register_items(vantage.back_boxes)
register_items(vantage.masters)
register_items(vantage.modules)
register_items(vantage.port_devices)
Expand Down Expand Up @@ -102,7 +101,11 @@ def vantage_device_info(client: Vantage, obj: SystemObject) -> DeviceInfo:

# Set up device relationships
if not isinstance(obj, Master):
if isinstance(obj, ChildObject) and obj.parent.id in client:
if (
isinstance(obj, ChildObject)
and obj.parent.id in client
and not client.back_boxes.get(obj.parent.id)
):
device_info["via_device"] = (DOMAIN, str(obj.parent.id))
else:
device_info["via_device"] = (DOMAIN, str(obj.master_id))
Expand Down
30 changes: 30 additions & 0 deletions custom_components/vantage/migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Migration functions for the Vantage integration."""

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr

from .const import LOGGER


async def async_migrate_data(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Run all Vantage data migrations."""

async_delete_back_boxes(hass, entry)


def async_delete_back_boxes(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Delete back boxes from the device registry."""
dev_reg = dr.async_get(hass)

back_box_devices = [
device
for device in dr.async_entries_for_config_entry(dev_reg, entry.entry_id)
if device.model == "BackBox"
]

if back_box_devices:
LOGGER.debug(f"Deleting {len(back_box_devices)} back boxes from the registry.")

for device in back_box_devices:
dev_reg.async_remove_device(device.id)

0 comments on commit bdad37f

Please sign in to comment.