1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Remove lamps and groups from ha when removed from Hue (#26881)

* Remove light when removed from hue

* add remove_config_entry_id

* Review + bump aiohue

* lint

* Add tests
This commit is contained in:
Bram Kragten
2019-09-25 23:00:18 +02:00
committed by Paulus Schoutsen
parent f6995b8d17
commit b75639d9d1
7 changed files with 159 additions and 10 deletions

View File

@@ -8,6 +8,9 @@ import random
import aiohue
import async_timeout
from homeassistant.helpers.entity_registry import async_get_registry as get_ent_reg
from homeassistant.helpers.device_registry import async_get_registry as get_dev_reg
from homeassistant.components import hue
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
@@ -147,6 +150,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
tasks.append(
async_update_items(
hass,
config_entry,
bridge,
async_add_entities,
request_update,
@@ -160,6 +164,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
tasks.append(
async_update_items(
hass,
config_entry,
bridge,
async_add_entities,
request_update,
@@ -176,6 +181,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_update_items(
hass,
config_entry,
bridge,
async_add_entities,
request_bridge_update,
@@ -204,9 +210,9 @@ async def async_update_items(
_LOGGER.error("Unable to reach bridge %s (%s)", bridge.host, err)
bridge.available = False
for light_id, light in current.items():
if light_id not in progress_waiting:
light.async_schedule_update_ha_state()
for item_id, item in current.items():
if item_id not in progress_waiting:
item.async_schedule_update_ha_state()
return
@@ -219,7 +225,8 @@ async def async_update_items(
_LOGGER.info("Reconnected to bridge %s", bridge.host)
bridge.available = True
new_lights = []
new_items = []
removed_items = []
for item_id in api:
if item_id not in current:
@@ -227,12 +234,34 @@ async def async_update_items(
api[item_id], request_bridge_update, bridge, is_group
)
new_lights.append(current[item_id])
new_items.append(current[item_id])
elif item_id not in progress_waiting:
current[item_id].async_schedule_update_ha_state()
if new_lights:
async_add_entities(new_lights)
for item_id in current:
if item_id in api:
continue
# Device is removed from Hue, so we remove it from Home Assistant
entity = current[item_id]
removed_items.append(item_id)
await entity.async_remove()
ent_registry = await get_ent_reg(hass)
if entity.entity_id in ent_registry.entities:
ent_registry.async_remove(entity.entity_id)
dev_registry = await get_dev_reg(hass)
device = dev_registry.async_get_device(
identifiers={(hue.DOMAIN, entity.unique_id)}, connections=set()
)
dev_registry.async_update_device(
device.id, remove_config_entry_id=config_entry.entry_id
)
if new_items:
async_add_entities(new_items)
for item_id in removed_items:
del current[item_id]
class HueLight(Light):