Remove media player group state listener when entity is removed (#183273)

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Franck Nijhof
2026-09-27 13:16:16 +02:00
committed by GitHub
co-authored by Claude Copilot Autofix powered by AI
parent 54d185baee
commit 024793d9bd
2 changed files with 28 additions and 2 deletions
@@ -259,8 +259,10 @@ class MediaPlayerGroup(MediaPlayerEntity):
for entity_id in self._entities:
new_state = self.hass.states.get(entity_id)
self.async_update_supported_features(entity_id, new_state)
async_track_state_change_event(
self.hass, self._entities, self.async_on_state_change
self.async_on_remove(
async_track_state_change_event(
self.hass, self._entities, self.async_on_state_change
)
)
self.async_update_group_state()
self.async_write_ha_state()
@@ -657,3 +657,27 @@ async def test_service_play_media_kwargs(hass: HomeAssistant) -> None:
mp_living_room.play_media.assert_called_with(
"some_type", "some_id", announce=True, extra={"volume": 20}
)
async def test_state_listener_removed_with_entity(hass: HomeAssistant) -> None:
"""Test the member state listener is removed when the group is removed."""
await async_setup_component(
hass,
MEDIA_DOMAIN,
{
MEDIA_DOMAIN: {
"platform": DOMAIN,
"entities": ["media_player.player_1", "media_player.player_2"],
}
},
)
await hass.async_block_till_done()
entity = hass.data[MEDIA_DOMAIN].get_entity("media_player.media_group")
await entity.async_remove()
assert entity.state is None
hass.states.async_set("media_player.player_1", STATE_ON)
await hass.async_block_till_done()
assert entity.state is None