Improve device registry for host and deCONZ service (#41786)

This commit is contained in:
Robert Svensson 2020-10-17 18:44:23 +02:00 committed by GitHub
parent 92669d33aa
commit c6c617ed31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 8 deletions

View File

@ -51,7 +51,6 @@ class DeconzGateway:
self.ignore_state_updates = False
self.deconz_ids = {}
self.device_id = None
self.entities = {}
self.events = []
self.listeners = []
@ -140,16 +139,23 @@ class DeconzGateway:
async def async_update_device_registry(self) -> None:
"""Update device registry."""
device_registry = await self.hass.helpers.device_registry.async_get_registry()
entry = device_registry.async_get_or_create(
# Host device
device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id,
connections={(CONNECTION_NETWORK_MAC, self.api.config.mac)},
)
# Gateway service
device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id,
identifiers={(DOMAIN, self.api.config.bridgeid)},
manufacturer="Dresden Elektronik",
model=self.api.config.modelid,
name=self.api.config.name,
sw_version=self.api.config.swversion,
via_device=(CONNECTION_NETWORK_MAC, self.api.config.mac),
)
self.device_id = entry.id
async def async_setup(self) -> bool:
"""Set up a deCONZ gateway."""

View File

@ -3,6 +3,7 @@ from pydeconz.utils import normalize_bridge_id
import voluptuous as vol
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.entity_registry import (
async_entries_for_config_entry,
async_entries_for_device,
@ -204,9 +205,20 @@ async def async_remove_orphaned_entries_service(hass, data):
if gateway.config_entry.entry_id in entry.config_entries
]
# Don't remove the Gateway device
if gateway.device_id in devices_to_be_removed:
devices_to_be_removed.remove(gateway.device_id)
# Don't remove the Gateway host entry
gateway_host = device_registry.async_get_device(
connections={(CONNECTION_NETWORK_MAC, gateway.api.config.mac)},
identifiers=set(),
)
if gateway_host.id in devices_to_be_removed:
devices_to_be_removed.remove(gateway_host.id)
# Don't remove the Gateway service entry
gateway_service = device_registry.async_get_device(
identifiers={(DOMAIN, gateway.api.config.bridgeid)}, connections=set()
)
if gateway_service.id in devices_to_be_removed:
devices_to_be_removed.remove(gateway_service.id)
# Don't remove devices belonging to available events
for event in gateway.events:

View File

@ -254,7 +254,7 @@ async def test_remove_orphaned_entries_service(hass):
if config_entry.entry_id in entry.config_entries
]
)
== 4 # Gateway, light, switch and orphan
== 5 # Host, gateway, light, switch and orphan
)
entity_registry = await hass.helpers.entity_registry.async_get_registry()
@ -287,7 +287,7 @@ async def test_remove_orphaned_entries_service(hass):
if config_entry.entry_id in entry.config_entries
]
)
== 3 # Gateway, light and switch
== 4 # Host, gateway, light and switch
)
assert (