diff --git a/homeassistant/components/deconz/gateway.py b/homeassistant/components/deconz/gateway.py index 2a853d097f3..475b3c48525 100644 --- a/homeassistant/components/deconz/gateway.py +++ b/homeassistant/components/deconz/gateway.py @@ -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.""" diff --git a/homeassistant/components/deconz/services.py b/homeassistant/components/deconz/services.py index bf503376321..7a00029b911 100644 --- a/homeassistant/components/deconz/services.py +++ b/homeassistant/components/deconz/services.py @@ -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: diff --git a/tests/components/deconz/test_services.py b/tests/components/deconz/test_services.py index 5b6a0e9520b..26524553dbe 100644 --- a/tests/components/deconz/test_services.py +++ b/tests/components/deconz/test_services.py @@ -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 (