2019-10-18 22:41:11 +00:00
|
|
|
"""Helper functions for Philips Hue."""
|
2021-11-16 19:59:17 +00:00
|
|
|
|
2022-05-18 11:42:49 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
2019-10-18 22:41:11 +00:00
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
from ..const import DOMAIN
|
2019-10-18 22:41:11 +00:00
|
|
|
|
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
async def remove_devices(bridge, api_ids, current):
|
2019-10-18 22:41:11 +00:00
|
|
|
"""Get items that are removed from api."""
|
|
|
|
removed_items = []
|
|
|
|
|
|
|
|
for item_id in current:
|
|
|
|
if item_id in api_ids:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Device is removed from Hue, so we remove it from Home Assistant
|
|
|
|
entity = current[item_id]
|
|
|
|
removed_items.append(item_id)
|
2021-02-08 09:45:46 +00:00
|
|
|
await entity.async_remove(force_remove=True)
|
2022-05-18 11:42:49 +00:00
|
|
|
ent_registry = er.async_get(bridge.hass)
|
2019-10-18 22:41:11 +00:00
|
|
|
if entity.entity_id in ent_registry.entities:
|
|
|
|
ent_registry.async_remove(entity.entity_id)
|
2022-05-18 11:42:49 +00:00
|
|
|
dev_registry = dr.async_get(bridge.hass)
|
2021-01-07 12:49:45 +00:00
|
|
|
device = dev_registry.async_get_device(identifiers={(DOMAIN, entity.device_id)})
|
2019-10-18 22:41:11 +00:00
|
|
|
if device is not None:
|
|
|
|
dev_registry.async_update_device(
|
2020-01-31 22:47:40 +00:00
|
|
|
device.id, remove_config_entry_id=bridge.config_entry.entry_id
|
2019-10-18 22:41:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for item_id in removed_items:
|
|
|
|
del current[item_id]
|