Fix removal of non device-bound resources in Hue (#88897)

Fix removal of non device-bound resources (like entertainment areas)
This commit is contained in:
Marcel van der Veldt 2023-02-28 17:04:10 +01:00 committed by GitHub
parent 390daf1723
commit e74613f8be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,7 +55,13 @@ class HueBaseEntity(Entity):
self._attr_unique_id = resource.id self._attr_unique_id = resource.id
# device is precreated in main handler # device is precreated in main handler
# this attaches the entity to the precreated device # this attaches the entity to the precreated device
if self.device is not None: if self.device is None:
# attach all device-less entities to the bridge itself
# e.g. config based sensors like entertainment area
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, bridge.api.config.bridge.bridge_id)},
)
else:
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.device.id)}, identifiers={(DOMAIN, self.device.id)},
) )
@ -137,17 +143,14 @@ class HueBaseEntity(Entity):
def _handle_event(self, event_type: EventType, resource: HueResource) -> None: def _handle_event(self, event_type: EventType, resource: HueResource) -> None:
"""Handle status event for this resource (or it's parent).""" """Handle status event for this resource (or it's parent)."""
if event_type == EventType.RESOURCE_DELETED: if event_type == EventType.RESOURCE_DELETED:
# remove any services created for zones/rooms # handle removal of room and zone 'virtual' devices/services
# regular devices are removed automatically by the logic in device.py. # regular devices are removed automatically by the logic in device.py.
if resource.type in (ResourceTypes.ROOM, ResourceTypes.ZONE): if resource.type in (ResourceTypes.ROOM, ResourceTypes.ZONE):
dev_reg = async_get_device_registry(self.hass) dev_reg = async_get_device_registry(self.hass)
if device := dev_reg.async_get_device({(DOMAIN, resource.id)}): if device := dev_reg.async_get_device({(DOMAIN, resource.id)}):
dev_reg.async_remove_device(device.id) dev_reg.async_remove_device(device.id)
if resource.type in ( # cleanup entities that are not strictly device-bound and have the bridge as parent
ResourceTypes.GROUPED_LIGHT, if self.device is None:
ResourceTypes.SCENE,
ResourceTypes.SMART_SCENE,
):
ent_reg = async_get_entity_registry(self.hass) ent_reg = async_get_entity_registry(self.hass)
ent_reg.async_remove(self.entity_id) ent_reg.async_remove(self.entity_id)
return return