From bad5eaf329338b3f2524ca89d34236c9f0bfddae Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Fri, 21 Jun 2024 15:04:42 +0200 Subject: [PATCH] Add entity ids to grouped hue light (#113053) --- homeassistant/components/hue/v2/group.py | 30 ++++++++++++++++++++---- tests/components/hue/test_light_v2.py | 9 +++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/hue/v2/group.py b/homeassistant/components/hue/v2/group.py index db30800a333..34797b0e42c 100644 --- a/homeassistant/components/hue/v2/group.py +++ b/homeassistant/components/hue/v2/group.py @@ -26,6 +26,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback +import homeassistant.helpers.entity_registry as er from ..bridge import HueBridge from ..const import DOMAIN @@ -136,15 +137,18 @@ class GroupedHueLight(HueBaseEntity, LightEntity): scenes = { x.metadata.name for x in self.api.scenes if x.group.rid == self.group.id } - lights = { - self.controller.get_device(x.id).metadata.name - for x in self.controller.get_lights(self.resource.id) - } + light_resource_ids = tuple( + x.id for x in self.controller.get_lights(self.resource.id) + ) + light_names, light_entities = self._get_names_and_entity_ids_for_resource_ids( + light_resource_ids + ) return { "is_hue_group": True, "hue_scenes": scenes, "hue_type": self.group.type.value, - "lights": lights, + "lights": light_names, + "entity_id": light_entities, "dynamics": self._dynamic_mode_active, } @@ -278,3 +282,19 @@ class GroupedHueLight(HueBaseEntity, LightEntity): self._attr_color_mode = ColorMode.BRIGHTNESS else: self._attr_color_mode = ColorMode.ONOFF + + @callback + def _get_names_and_entity_ids_for_resource_ids( + self, resource_ids: tuple[str] + ) -> tuple[set[str], set[str]]: + """Return the names and entity ids for the given Hue (light) resource IDs.""" + ent_reg = er.async_get(self.hass) + light_names: set[str] = set() + light_entities: set[str] = set() + for resource_id in resource_ids: + light_names.add(self.controller.get_device(resource_id).metadata.name) + if entity_id := ent_reg.async_get_entity_id( + self.platform.domain, DOMAIN, resource_id + ): + light_entities.add(entity_id) + return light_names, light_entities diff --git a/tests/components/hue/test_light_v2.py b/tests/components/hue/test_light_v2.py index 1f25649fdaa..fca907eabb0 100644 --- a/tests/components/hue/test_light_v2.py +++ b/tests/components/hue/test_light_v2.py @@ -412,6 +412,11 @@ async def test_grouped_lights( "Hue light with color and color temperature gradient", "Hue light with color and color temperature 2", } + assert test_entity.attributes["entity_id"] == { + "light.hue_light_with_color_and_color_temperature_gradient", + "light.hue_light_with_color_and_color_temperature_2", + "light.hue_light_with_color_and_color_temperature_1", + } # test light created for hue room test_entity = hass.states.get("light.test_room") @@ -431,6 +436,10 @@ async def test_grouped_lights( "Hue on/off light", "Hue light with color temperature only", } + assert test_entity.attributes["entity_id"] == { + "light.hue_light_with_color_temperature_only", + "light.hue_on_off_light", + } # Test calling the turn on service on a grouped light test_light_id = "light.test_zone"