Fix media_player grouping while media_player is off (#58070)

Co-authored-by: Erik Montnemery <erik@montnemery.com>
pull/58516/head
Tom Schneider 2021-10-27 12:08:19 +02:00 committed by GitHub
parent 8017a1e141
commit a3dbe8e1e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 6 deletions

View File

@ -896,11 +896,14 @@ class MediaPlayerEntity(Entity):
@property
def state_attributes(self):
"""Return the state attributes."""
if self.state == STATE_OFF:
return None
state_attr = {}
if self.support_grouping:
state_attr[ATTR_GROUP_MEMBERS] = self.group_members
if self.state == STATE_OFF:
return state_attr
for attr in ATTR_TO_PROPERTY:
value = getattr(self, attr)
if value is not None:
@ -909,9 +912,6 @@ class MediaPlayerEntity(Entity):
if self.media_image_remotely_accessible:
state_attr["entity_picture_local"] = self.media_image_local
if self.support_grouping:
state_attr[ATTR_GROUP_MEMBERS] = self.group_members
return state_attr
async def async_browse_media(

View File

@ -4,6 +4,7 @@ from unittest.mock import patch
from homeassistant.components import media_player
from homeassistant.components.websocket_api.const import TYPE_RESULT
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF
from homeassistant.setup import async_setup_component
@ -183,3 +184,27 @@ async def test_media_browse(hass, hass_ws_client):
assert msg["type"] == TYPE_RESULT
assert msg["success"]
assert msg["result"] == {"bla": "yo"}
async def test_group_members_available_when_off(hass):
"""Test that group_members are still available when media_player is off."""
await async_setup_component(
hass, "media_player", {"media_player": {"platform": "demo"}}
)
await hass.async_block_till_done()
# Fake group support for DemoYoutubePlayer
with patch(
"homeassistant.components.demo.media_player.YOUTUBE_PLAYER_SUPPORT",
media_player.SUPPORT_GROUPING | media_player.SUPPORT_TURN_OFF,
):
await hass.services.async_call(
"media_player",
"turn_off",
{ATTR_ENTITY_ID: "media_player.bedroom"},
blocking=True,
)
state = hass.states.get("media_player.bedroom")
assert state.state == STATE_OFF
assert "group_members" in state.attributes