From bd1d6b747fa7df7f01d977dcd7f9e9b510f48c25 Mon Sep 17 00:00:00 2001 From: jbouwh Date: Sat, 14 Feb 2026 13:31:16 +0000 Subject: [PATCH] Allow updates of group members --- homeassistant/components/mqtt/entity.py | 7 ++-- tests/components/mqtt/test_light_json.py | 43 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/mqtt/entity.py b/homeassistant/components/mqtt/entity.py index 60514ffa974..9a2199dc85f 100644 --- a/homeassistant/components/mqtt/entity.py +++ b/homeassistant/components/mqtt/entity.py @@ -466,7 +466,7 @@ def async_setup_entity_entry_helper( class MqttAttributesMixin(Entity): - """Mixin used for platforms that support JSON attributes.""" + """Mixin used for platforms that support JSON attributes and group entities.""" _attributes_extra_blocked: frozenset[str] = frozenset() _attr_tpl: Callable[[ReceivePayloadType], ReceivePayloadType] | None = None @@ -474,9 +474,10 @@ class MqttAttributesMixin(Entity): [MessageCallbackType, set[str] | None, ReceiveMessage], None ] _process_update_extra_state_attributes: Callable[[dict[str, Any]], None] + group: IntegrationSpecificGroup def __init__(self, config: ConfigType) -> None: - """Initialize the JSON attributes mixin.""" + """Initialize the JSON attributes and handle group entities.""" self._attributes_sub_state: dict[str, EntitySubscription] = {} if CONF_GROUP in config: self.group = IntegrationSpecificGroup(self, config[CONF_GROUP]) @@ -490,6 +491,8 @@ class MqttAttributesMixin(Entity): def attributes_prepare_discovery_update(self, config: DiscoveryInfoType) -> None: """Handle updated discovery message.""" + if self.group is not None and CONF_GROUP in config: + self.group.included_unique_ids = config[CONF_GROUP] self._attributes_config = config self._attributes_prepare_subscribe_topics() diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 353e54b7480..82ab35fe887 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -173,6 +173,7 @@ COLOR_MODES_CONFIG = { GROUP_MEMBER_1_TOPIC = "homeassistant/light/member_1/config" GROUP_MEMBER_2_TOPIC = "homeassistant/light/member_2/config" +GROUP_MEMBER_3_TOPIC = "homeassistant/light/member_3/config" GROUP_TOPIC = "homeassistant/light/group/config" GROUP_DISCOVERY_MEMBER_1_CONFIG = json.dumps( { @@ -192,6 +193,15 @@ GROUP_DISCOVERY_MEMBER_2_CONFIG = json.dumps( "default_entity_id": "light.member2", } ) +GROUP_DISCOVERY_MEMBER_3_CONFIG = json.dumps( + { + "schema": "json", + "command_topic": "test-command-topic-member3", + "unique_id": "very_unique_member3", + "name": "member3", + "default_entity_id": "light.member3", + } +) GROUP_DISCOVERY_LIGHT_GROUP_CONFIG = json.dumps( { "schema": "json", @@ -203,6 +213,17 @@ GROUP_DISCOVERY_LIGHT_GROUP_CONFIG = json.dumps( "group": ["very_unique_member1", "very_unique_member2"], } ) +GROUP_DISCOVERY_LIGHT_GROUP_CONFIG_EXPANDED = json.dumps( + { + "schema": "json", + "command_topic": "test-command-topic-group", + "state_topic": "test-state-topic-group", + "unique_id": "very_unique_group", + "name": "group", + "default_entity_id": "light.group", + "group": ["very_unique_member1", "very_unique_member2", "very_unique_member3"], + } +) class JsonValidator: @@ -1921,6 +1942,28 @@ async def test_light_group_discovery_members_before_group( "light.member2", ] + # Now create and discover a new member + async_fire_mqtt_message(hass, GROUP_MEMBER_3_TOPIC, GROUP_DISCOVERY_MEMBER_3_CONFIG) + await hass.async_block_till_done() + + # Update the group discovery + async_fire_mqtt_message( + hass, GROUP_TOPIC, GROUP_DISCOVERY_LIGHT_GROUP_CONFIG_EXPANDED + ) + + await hass.async_block_till_done() + + assert hass.states.get("light.member1") is not None + assert hass.states.get("light.member2") is not None + assert hass.states.get("light.member3") is not None + group_state = hass.states.get("light.group") + assert group_state is not None + assert group_state.attributes.get("group_entities") == [ + "light.member1", + "light.member2", + "light.member3", + ] + async def test_light_group_discovery_group_before_members( hass: HomeAssistant,