diff --git a/homeassistant/components/group/entity.py b/homeassistant/components/group/entity.py index f9d9a62a0ac..4b44de708b5 100644 --- a/homeassistant/components/group/entity.py +++ b/homeassistant/components/group/entity.py @@ -7,7 +7,13 @@ from collections.abc import Callable, Collection, Mapping import logging from typing import Any -from homeassistant.const import ATTR_ASSUMED_STATE, ATTR_ENTITY_ID, STATE_OFF, STATE_ON +from homeassistant.const import ( + ATTR_ASSUMED_STATE, + ATTR_ENTITY_ID, + ATTR_GROUP_ENTITIES, + STATE_OFF, + STATE_ON, +) from homeassistant.core import ( CALLBACK_TYPE, Event, @@ -35,7 +41,7 @@ _LOGGER = logging.getLogger(__name__) class GroupEntity(Entity): """Representation of a Group of entities.""" - _unrecorded_attributes = frozenset({ATTR_ENTITY_ID}) + _unrecorded_attributes = frozenset({ATTR_ENTITY_ID, ATTR_GROUP_ENTITIES}) _attr_should_poll = False _entity_ids: list[str] diff --git a/homeassistant/components/group/lock.py b/homeassistant/components/group/lock.py index 7b460aa4632..87e7474e03a 100644 --- a/homeassistant/components/group/lock.py +++ b/homeassistant/components/group/lock.py @@ -20,9 +20,6 @@ from homeassistant.const import ( CONF_ENTITIES, CONF_NAME, CONF_UNIQUE_ID, - SERVICE_LOCK, - SERVICE_OPEN, - SERVICE_UNLOCK, STATE_UNAVAILABLE, STATE_UNKNOWN, ) @@ -32,6 +29,7 @@ from homeassistant.helpers.entity_platform import ( AddConfigEntryEntitiesCallback, AddEntitiesCallback, ) +from homeassistant.helpers.group import GenericGroup from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from .entity import GroupEntity @@ -117,47 +115,13 @@ class LockGroup(GroupEntity, LockEntity): ) -> None: """Initialize a lock group.""" self._entity_ids = entity_ids + self.group = GenericGroup(self, entity_ids) self._attr_supported_features = LockEntityFeature.OPEN self._attr_name = name self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids} self._attr_unique_id = unique_id - async def async_lock(self, **kwargs: Any) -> None: - """Forward the lock command to all locks in the group.""" - data = {ATTR_ENTITY_ID: self._entity_ids} - _LOGGER.debug("Forwarded lock command: %s", data) - - await self.hass.services.async_call( - LOCK_DOMAIN, - SERVICE_LOCK, - data, - blocking=True, - context=self._context, - ) - - async def async_unlock(self, **kwargs: Any) -> None: - """Forward the unlock command to all locks in the group.""" - data = {ATTR_ENTITY_ID: self._entity_ids} - await self.hass.services.async_call( - LOCK_DOMAIN, - SERVICE_UNLOCK, - data, - blocking=True, - context=self._context, - ) - - async def async_open(self, **kwargs: Any) -> None: - """Forward the open command to all locks in the group.""" - data = {ATTR_ENTITY_ID: self._entity_ids} - await self.hass.services.async_call( - LOCK_DOMAIN, - SERVICE_OPEN, - data, - blocking=True, - context=self._context, - ) - @callback def async_update_group_state(self) -> None: """Query all members and determine the lock group state.""" diff --git a/homeassistant/components/zha/entity.py b/homeassistant/components/zha/entity.py index de09f420730..f3a0d0584c2 100644 --- a/homeassistant/components/zha/entity.py +++ b/homeassistant/components/zha/entity.py @@ -22,6 +22,7 @@ from homeassistant.core import State, callback from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE, DeviceInfo from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity +from homeassistant.helpers.group import IntegrationSpecificGroup from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.typing import UNDEFINED, UndefinedType @@ -51,6 +52,18 @@ class ZHAEntity(LogMixin, RestoreEntity, Entity): meta = self.entity_data.entity.info_object self._attr_unique_id = meta.unique_id + if self.entity_data.is_group_entity: + group_proxy = self.entity_data.group_proxy + assert group_proxy is not None + platform = self.entity_data.entity.PLATFORM + unique_ids = [ + entity.info_object.unique_id + for member in group_proxy.group.members + for entity in member.associated_entities + if platform == entity.PLATFORM + ] + self.group = IntegrationSpecificGroup(self, unique_ids) + if meta.entity_category is not None: self._attr_entity_category = EntityCategory(meta.entity_category) diff --git a/homeassistant/const.py b/homeassistant/const.py index 27a46355ca9..6c0a918eb1e 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -332,6 +332,9 @@ ATTR_NAME: Final = "name" # Contains one string or a list of strings, each being an entity id ATTR_ENTITY_ID: Final = "entity_id" +# Contains a list of entity ids that are members of a group +ATTR_GROUP_ENTITIES: Final = "group_entities" + # Contains one string, the config entry ID ATTR_CONFIG_ENTRY_ID: Final = "config_entry_id" diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index 30e9a040632..d1eb58dd7af 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -27,6 +27,7 @@ from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_ENTITY_PICTURE, ATTR_FRIENDLY_NAME, + ATTR_GROUP_ENTITIES, ATTR_ICON, ATTR_SUPPORTED_FEATURES, ATTR_UNIT_OF_MEASUREMENT, @@ -54,13 +55,15 @@ from homeassistant.loader import async_suggest_report_issue, bind_hass from homeassistant.util import ensure_unique_string, slugify from homeassistant.util.frozen_dataclass_compat import FrozenOrThawed -from . import device_registry as dr, entity_registry as er, singleton +from . import device_registry as dr, entity_registry as er from .device_registry import DeviceInfo, EventDeviceRegistryUpdatedData from .event import ( async_track_device_registry_updated_event, async_track_entity_registry_updated_event, ) -from .frame import report_non_thread_safe_operation +from .frame import report_non_thread_safe_operation, report_usage +from .group import Group +from .singleton import singleton from .typing import UNDEFINED, StateType, UndefinedType timer = time.time @@ -90,7 +93,7 @@ def async_setup(hass: HomeAssistant) -> None: @callback @bind_hass -@singleton.singleton(DATA_ENTITY_SOURCE) +@singleton(DATA_ENTITY_SOURCE) def entity_sources(hass: HomeAssistant) -> dict[str, EntityInfo]: """Get the entity sources. @@ -457,6 +460,15 @@ class Entity( # Only handled internally, never to be used by integrations. internal_integration_suggested_object_id: str | None + # A group information in case the entity represents a group + group: Group | None = None + # Internal copy of `group`. This prevents integration authors from + # mistakenly overwriting it during the entity's lifetime, which would + # break Group functionality. It also lets us check if `group` is + # actually a Group instance just once in `async_internal_added_to_hass`, + # rather than on every state write. + __group: Group | None = None + # If we reported if this entity was slow _slow_reported = False @@ -1064,6 +1076,10 @@ class Entity( entry = self.registry_entry capability_attr = self.capability_attributes + if self.__group is not None: + capability_attr = capability_attr.copy() if capability_attr else {} + capability_attr[ATTR_GROUP_ENTITIES] = self.__group.member_entity_ids.copy() + attr = capability_attr.copy() if capability_attr else {} available = self.available # only call self.available once per update cycle @@ -1503,6 +1519,17 @@ class Entity( ) self._async_subscribe_device_updates() + if self.group is not None: + if not isinstance(self.group, Group): + report_usage( # type: ignore[unreachable] + f"sets a `group` attribute on entity {self.entity_id} which is " + "not a `Group` instance", + breaks_in_ha_version="2027.2", + ) + else: + self.__group = self.group + self.__group.async_added_to_hass() + async def async_internal_will_remove_from_hass(self) -> None: """Run when entity will be removed from hass. @@ -1513,6 +1540,9 @@ class Entity( if self.platform: del entity_sources(self.hass)[self.entity_id] + if self.__group is not None: + self.__group.async_will_remove_from_hass() + @callback def _async_registry_updated( self, event: Event[er.EventEntityRegistryUpdatedData] diff --git a/homeassistant/helpers/group.py b/homeassistant/helpers/group.py index 7d4eeb6d133..939d1c1cafd 100644 --- a/homeassistant/helpers/group.py +++ b/homeassistant/helpers/group.py @@ -3,19 +3,167 @@ from __future__ import annotations from collections.abc import Iterable -from typing import Any +from typing import TYPE_CHECKING, Any + +from propcache.api import cached_property from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, ENTITY_MATCH_NONE -from homeassistant.core import HomeAssistant +from homeassistant.core import Event, HomeAssistant, callback +from . import entity_registry as er +from .singleton import singleton + +if TYPE_CHECKING: + from .entity import Entity + +DATA_GROUP_ENTITIES = "group_entities" ENTITY_PREFIX = "group." +class Group: + """Entity group base class.""" + + _entity: Entity + + def __init__(self, entity: Entity) -> None: + """Initialize the group.""" + self._entity = entity + + @property + def member_entity_ids(self) -> list[str]: + """Return the list of member entity IDs.""" + raise NotImplementedError + + @callback + def async_added_to_hass(self) -> None: + """Called when the entity is added to hass.""" + entity = self._entity + get_group_entities(entity.hass)[entity.entity_id] = entity + + @callback + def async_will_remove_from_hass(self) -> None: + """Called when the entity will be removed from hass.""" + entity = self._entity + del get_group_entities(entity.hass)[entity.entity_id] + + +class GenericGroup(Group): + """Generic entity group. + + Members can come from multiple integrations and are referenced by entity ID. + """ + + def __init__(self, entity: Entity, member_entity_ids: list[str]) -> None: + """Initialize the group.""" + super().__init__(entity) + self._member_entity_ids = member_entity_ids + + @cached_property + def member_entity_ids(self) -> list[str]: + """Return the list of member entity IDs.""" + return self._member_entity_ids + + +class IntegrationSpecificGroup(Group): + """Integration-specific entity group. + + Members come from a single integration and are referenced by unique ID. + Entity IDs are resolved via the entity registry. This group listens for + entity registry events to keep the resolved entity IDs up to date. + """ + + _member_entity_ids: list[str] | None = None + _member_unique_ids: list[str] + + def __init__(self, entity: Entity, member_unique_ids: list[str]) -> None: + """Initialize the group.""" + super().__init__(entity) + self._member_unique_ids = member_unique_ids + + @cached_property + def member_entity_ids(self) -> list[str]: + """Return the list of member entity IDs.""" + entity_registry = er.async_get(self._entity.hass) + self._member_entity_ids = [ + entity_id + for unique_id in self.member_unique_ids + if ( + entity_id := entity_registry.async_get_entity_id( + self._entity.platform.domain, + self._entity.platform.platform_name, + unique_id, + ) + ) + is not None + ] + return self._member_entity_ids + + @property + def member_unique_ids(self) -> list[str]: + """Return the list of member unique IDs.""" + return self._member_unique_ids + + @member_unique_ids.setter + def member_unique_ids(self, value: list[str]) -> None: + """Set the list of member unique IDs.""" + self._member_unique_ids = value + if self._member_entity_ids is not None: + self._member_entity_ids = None + del self.member_entity_ids + + @callback + def async_added_to_hass(self) -> None: + """Called when the entity is added to hass.""" + super().async_added_to_hass() + + entity = self._entity + entity_registry = er.async_get(entity.hass) + + @callback + def _handle_entity_registry_updated(event: Event[Any]) -> None: + """Handle registry create or update event.""" + if ( + event.data["action"] in {"create", "update"} + and (entry := entity_registry.async_get(event.data["entity_id"])) + and entry.domain == entity.platform.domain + and entry.platform == entity.platform.platform_name + and entry.unique_id in self.member_unique_ids + ) or ( + event.data["action"] == "remove" + and self._member_entity_ids is not None + and event.data["entity_id"] in self._member_entity_ids + ): + if self._member_entity_ids is not None: + self._member_entity_ids = None + del self.member_entity_ids + entity.async_write_ha_state() + + entity.async_on_remove( + entity.hass.bus.async_listen( + er.EVENT_ENTITY_REGISTRY_UPDATED, + _handle_entity_registry_updated, + ) + ) + + +@callback +@singleton(DATA_GROUP_ENTITIES) +def get_group_entities(hass: HomeAssistant) -> dict[str, Entity]: + """Get the group entities. + + Items are added to this dict by Group.async_added_to_hass and + removed by Group.async_will_remove_from_hass. + """ + return {} + + def expand_entity_ids(hass: HomeAssistant, entity_ids: Iterable[Any]) -> list[str]: """Return entity_ids with group entity ids replaced by their members. Async friendly. """ + group_entities = get_group_entities(hass) + found_ids: list[str] = [] for entity_id in entity_ids: if not isinstance(entity_id, str) or entity_id in ( @@ -25,8 +173,22 @@ def expand_entity_ids(hass: HomeAssistant, entity_ids: Iterable[Any]) -> list[st continue entity_id = entity_id.lower() + # If entity_id points at a group, expand it - if entity_id.startswith(ENTITY_PREFIX): + if (entity := group_entities.get(entity_id)) is not None and isinstance( + entity.group, GenericGroup + ): + child_entities = entity.group.member_entity_ids + if entity_id in child_entities: + child_entities = list(child_entities) + child_entities.remove(entity_id) + found_ids.extend( + ent_id + for ent_id in expand_entity_ids(hass, child_entities) + if ent_id not in found_ids + ) + # If entity_id points at an old-style group, expand it + elif entity_id.startswith(ENTITY_PREFIX): child_entities = get_entity_ids(hass, entity_id) if entity_id in child_entities: child_entities = list(child_entities) diff --git a/tests/helpers/test_group.py b/tests/helpers/test_group.py index 26f4ffda256..3679258e7b9 100644 --- a/tests/helpers/test_group.py +++ b/tests/helpers/test_group.py @@ -1,8 +1,15 @@ """Test the group helper.""" -from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON +from homeassistant.const import ATTR_ENTITY_ID, ATTR_GROUP_ENTITIES, STATE_OFF, STATE_ON from homeassistant.core import HomeAssistant -from homeassistant.helpers import group +from homeassistant.helpers import entity_registry as er, group +from homeassistant.helpers.group import ( + GenericGroup, + IntegrationSpecificGroup, + get_group_entities, +) + +from tests.common import MockEntity, MockEntityPlatform async def test_expand_entity_ids(hass: HomeAssistant) -> None: @@ -104,3 +111,375 @@ async def test_get_entity_ids_with_non_existing_group_name(hass: HomeAssistant) async def test_get_entity_ids_with_non_group_state(hass: HomeAssistant) -> None: """Test get_entity_ids with a non group state.""" assert group.get_entity_ids(hass, "switch.AC") == [] + + +async def test_get_group_entities(hass: HomeAssistant) -> None: + """Test get_group_entities returns registered group entities.""" + assert get_group_entities(hass) == {} + + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.test_group", unique_id="test_group") + ent.group = GenericGroup(ent, ["light.bulb1", "light.bulb2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + group_entities = get_group_entities(hass) + assert "light.test_group" in group_entities + assert group_entities["light.test_group"] is ent + + +async def test_group_entity_removed_from_registry(hass: HomeAssistant) -> None: + """Test group entity is removed from get_group_entities on removal.""" + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.test_group", unique_id="test_group") + ent.group = GenericGroup(ent, ["light.bulb1", "light.bulb2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + assert "light.test_group" in get_group_entities(hass) + + await platform.async_remove_entity(ent.entity_id) + await hass.async_block_till_done() + assert "light.test_group" not in get_group_entities(hass) + + +async def test_group_entity_id_changed_in_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test get_group_entities reflects new key when group entity ID is changed.""" + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.old_id", unique_id="test_group") + ent.group = GenericGroup(ent, ["light.bulb1", "light.bulb2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + assert "light.old_id" in get_group_entities(hass) + + entity_registry.async_update_entity("light.old_id", new_entity_id="light.new_id") + await hass.async_block_till_done() + + group_entities = get_group_entities(hass) + assert "light.old_id" not in group_entities + assert "light.new_id" in group_entities + + expanded = group.expand_entity_ids(hass, ["light.new_id"]) + assert sorted(expanded) == ["light.bulb1", "light.bulb2"] + + +async def test_multiple_group_entities(hass: HomeAssistant) -> None: + """Test multiple group entities can be registered and work independently.""" + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent1 = MockEntity(entity_id="light.group1", unique_id="multi_1") + ent1.group = GenericGroup(ent1, ["light.a", "light.b"]) + + ent2 = MockEntity(entity_id="light.group2", unique_id="multi_2") + ent2.group = GenericGroup(ent2, ["light.c", "light.d"]) + + await platform.async_add_entities([ent1, ent2]) + await hass.async_block_till_done() + + group_entities = get_group_entities(hass) + assert "light.group1" in group_entities + assert "light.group2" in group_entities + + expanded1 = group.expand_entity_ids(hass, ["light.group1"]) + expanded2 = group.expand_entity_ids(hass, ["light.group2"]) + + assert sorted(expanded1) == ["light.a", "light.b"] + assert sorted(expanded2) == ["light.c", "light.d"] + + +async def test_generic_group_member_entity_ids(hass: HomeAssistant) -> None: + """Test GenericGroup member_entity_ids property.""" + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.test_group") + ent.group = GenericGroup(ent, ["light.bulb1", "light.bulb2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + assert ent.group.member_entity_ids == ["light.bulb1", "light.bulb2"] + + +async def test_expand_entity_ids_with_generic_group(hass: HomeAssistant) -> None: + """Test expand_entity_ids with GenericGroup entities.""" + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.living_room_group", unique_id="living_room") + ent.group = GenericGroup(ent, ["light.lamp1", "light.lamp2", "light.lamp3"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + hass.states.async_set("light.lamp1", STATE_ON) + hass.states.async_set("light.lamp2", STATE_OFF) + hass.states.async_set("light.lamp3", STATE_ON) + + expanded = group.expand_entity_ids(hass, ["light.living_room_group"]) + assert sorted(expanded) == ["light.lamp1", "light.lamp2", "light.lamp3"] + + +async def test_expand_entity_ids_with_generic_group_recursive( + hass: HomeAssistant, +) -> None: + """Test expand_entity_ids with nested GenericGroup entities.""" + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + inner_group = MockEntity(entity_id="light.inner_group", unique_id="inner") + inner_group.group = GenericGroup(inner_group, ["light.lamp1", "light.lamp2"]) + + outer_group = MockEntity(entity_id="light.outer_group", unique_id="outer") + outer_group.group = GenericGroup(outer_group, ["light.inner_group", "light.lamp3"]) + + await platform.async_add_entities([inner_group, outer_group]) + await hass.async_block_till_done() + + expanded = group.expand_entity_ids(hass, ["light.outer_group"]) + assert sorted(expanded) == ["light.lamp1", "light.lamp2", "light.lamp3"] + + +async def test_expand_entity_ids_with_generic_group_self_reference( + hass: HomeAssistant, +) -> None: + """Test expand_entity_ids handles GenericGroup with self-reference.""" + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.self_ref_group", unique_id="self_ref") + ent.group = GenericGroup( + ent, ["light.self_ref_group", "light.bulb1", "light.bulb2"] + ) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + expanded = group.expand_entity_ids(hass, ["light.self_ref_group"]) + assert sorted(expanded) == ["light.bulb1", "light.bulb2"] + + +async def test_generic_group_attribute_in_state(hass: HomeAssistant) -> None: + """Test ATTR_GROUP_ENTITIES is included in GenericGroup state.""" + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.group_with_attrs", unique_id="attrs_test") + ent.group = GenericGroup(ent, ["light.lamp1", "light.lamp2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + state = hass.states.get("light.group_with_attrs") + assert state is not None + assert ATTR_GROUP_ENTITIES in state.attributes + assert state.attributes[ATTR_GROUP_ENTITIES] == ["light.lamp1", "light.lamp2"] + + +async def test_integration_specific_group_member_entity_ids( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test IntegrationSpecificGroup resolves entity IDs from unique IDs.""" + entity_registry.async_get_or_create( + "light", "test", "unique_1", suggested_object_id="member1" + ) + entity_registry.async_get_or_create( + "light", "test", "unique_2", suggested_object_id="member2" + ) + + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.integration_group", unique_id="int_group") + ent.group = IntegrationSpecificGroup(ent, ["unique_1", "unique_2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + assert sorted(ent.group.member_entity_ids) == ["light.member1", "light.member2"] + + +async def test_integration_specific_group_missing_entities( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test IntegrationSpecificGroup handles missing entities.""" + entity_registry.async_get_or_create( + "light", "test", "unique_1", suggested_object_id="member1" + ) + + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.partial_group", unique_id="partial") + ent.group = IntegrationSpecificGroup( + ent, ["unique_1", "unique_2", "unique_missing"] + ) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + assert ent.group.member_entity_ids == ["light.member1"] + + +async def test_integration_specific_group_member_unique_ids_setter( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test IntegrationSpecificGroup member_unique_ids setter clears cache.""" + entity_registry.async_get_or_create( + "light", "test", "unique_1", suggested_object_id="member1" + ) + entity_registry.async_get_or_create( + "light", "test", "unique_2", suggested_object_id="member2" + ) + entity_registry.async_get_or_create( + "light", "test", "unique_3", suggested_object_id="member3" + ) + + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.dynamic_group", unique_id="dynamic") + ent.group = IntegrationSpecificGroup(ent, ["unique_1"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + assert ent.group.member_entity_ids == ["light.member1"] + + ent.group.member_unique_ids = ["unique_2", "unique_3"] + + assert sorted(ent.group.member_entity_ids) == ["light.member2", "light.member3"] + + +async def test_integration_specific_group_member_added( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test IntegrationSpecificGroup updates when member is added to registry.""" + entity_registry.async_get_or_create( + "light", "test", "unique_1", suggested_object_id="member1" + ) + + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.registry_group", unique_id="reg_group") + ent.group = IntegrationSpecificGroup(ent, ["unique_1", "unique_2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + assert ent.group.member_entity_ids == ["light.member1"] + + entity_registry.async_get_or_create( + "light", "test", "unique_2", suggested_object_id="member2" + ) + await hass.async_block_till_done() + + assert sorted(ent.group.member_entity_ids) == ["light.member1", "light.member2"] + + +async def test_integration_specific_group_member_removed( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test IntegrationSpecificGroup updates when member is removed from registry.""" + entry1 = entity_registry.async_get_or_create( + "light", "test", "unique_1", suggested_object_id="member1" + ) + entity_registry.async_get_or_create( + "light", "test", "unique_2", suggested_object_id="member2" + ) + + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.remove_group", unique_id="rem_group") + ent.group = IntegrationSpecificGroup(ent, ["unique_1", "unique_2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + assert sorted(ent.group.member_entity_ids) == ["light.member1", "light.member2"] + + entity_registry.async_remove(entry1.entity_id) + await hass.async_block_till_done() + + assert ent.group.member_entity_ids == ["light.member2"] + + +async def test_integration_specific_group_member_renamed( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test IntegrationSpecificGroup updates when member entity_id is renamed.""" + entry = entity_registry.async_get_or_create( + "light", "test", "unique_1", suggested_object_id="original_name" + ) + + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.group", unique_id="grp") + ent.group = IntegrationSpecificGroup(ent, ["unique_1"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + assert ent.group.member_entity_ids == ["light.original_name"] + + entity_registry.async_update_entity(entry.entity_id, new_entity_id="light.new_id") + await hass.async_block_till_done() + + assert ent.group.member_entity_ids == ["light.new_id"] + + +async def test_integration_specific_group_attribute_in_state( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test ATTR_GROUP_ENTITIES is included in IntegrationSpecificGroup state.""" + entity_registry.async_get_or_create( + "light", "test", "unique_1", suggested_object_id="member1" + ) + entity_registry.async_get_or_create( + "light", "test", "unique_2", suggested_object_id="member2" + ) + + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.int_group_attrs", unique_id="int_attrs") + ent.group = IntegrationSpecificGroup(ent, ["unique_1", "unique_2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + state = hass.states.get("light.int_group_attrs") + assert state is not None + assert ATTR_GROUP_ENTITIES in state.attributes + assert sorted(state.attributes[ATTR_GROUP_ENTITIES]) == [ + "light.member1", + "light.member2", + ] + + +async def test_expand_entity_ids_integration_specific_group_not_expanded( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test expand_entity_ids doesn't expand IntegrationSpecificGroup.""" + entity_registry.async_get_or_create( + "light", "test", "unique_1", suggested_object_id="member1" + ) + entity_registry.async_get_or_create( + "light", "test", "unique_2", suggested_object_id="member2" + ) + + platform = MockEntityPlatform(hass, domain="light", platform_name="test") + + ent = MockEntity(entity_id="light.int_specific_group", unique_id="int_spec") + ent.group = IntegrationSpecificGroup(ent, ["unique_1", "unique_2"]) + + await platform.async_add_entities([ent]) + await hass.async_block_till_done() + + expanded = group.expand_entity_ids(hass, ["light.int_specific_group"]) + assert expanded == ["light.int_specific_group"]