Add entity ids to grouped hue light (#113053)

This commit is contained in:
Marcel van der Veldt 2024-06-21 15:04:42 +02:00 committed by GitHub
parent 7ba1e4446c
commit bad5eaf329
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 5 deletions

View file

@ -26,6 +26,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.helpers.entity_registry as er
from ..bridge import HueBridge from ..bridge import HueBridge
from ..const import DOMAIN from ..const import DOMAIN
@ -136,15 +137,18 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
scenes = { scenes = {
x.metadata.name for x in self.api.scenes if x.group.rid == self.group.id x.metadata.name for x in self.api.scenes if x.group.rid == self.group.id
} }
lights = { light_resource_ids = tuple(
self.controller.get_device(x.id).metadata.name x.id for x in self.controller.get_lights(self.resource.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 { return {
"is_hue_group": True, "is_hue_group": True,
"hue_scenes": scenes, "hue_scenes": scenes,
"hue_type": self.group.type.value, "hue_type": self.group.type.value,
"lights": lights, "lights": light_names,
"entity_id": light_entities,
"dynamics": self._dynamic_mode_active, "dynamics": self._dynamic_mode_active,
} }
@ -278,3 +282,19 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
self._attr_color_mode = ColorMode.BRIGHTNESS self._attr_color_mode = ColorMode.BRIGHTNESS
else: else:
self._attr_color_mode = ColorMode.ONOFF 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

View file

@ -412,6 +412,11 @@ async def test_grouped_lights(
"Hue light with color and color temperature gradient", "Hue light with color and color temperature gradient",
"Hue light with color and color temperature 2", "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 light created for hue room
test_entity = hass.states.get("light.test_room") test_entity = hass.states.get("light.test_room")
@ -431,6 +436,10 @@ async def test_grouped_lights(
"Hue on/off light", "Hue on/off light",
"Hue light with color temperature only", "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 calling the turn on service on a grouped light
test_light_id = "light.test_zone" test_light_id = "light.test_zone"