Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 54 additions & 48 deletions tests/components/media_player/test_init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the base functions of the media player."""

from http import HTTPStatus
from typing import Any
from unittest.mock import patch

import pytest
Expand All @@ -19,7 +20,7 @@
SearchMedia,
SearchMediaQuery,
)
from homeassistant.components.media_player.const import (

Check failure on line 23 in tests/components/media_player/test_init.py

View workflow job for this annotation

GitHub Actions / Check pylint on tests

E0611: No name 'SERVICE_GET_GROUPABLE_MEMBERS' in module 'homeassistant.components.media_player.const' (no-name-in-module)
SERVICE_BROWSE_MEDIA,
SERVICE_GET_GROUPABLE_MEMBERS,
SERVICE_SEARCH_MEDIA,
Expand Down Expand Up @@ -722,56 +723,61 @@
)


async def test_get_groupable_players_multiplatform(hass: HomeAssistant) -> None:
"""Test that get_groupable_players service can return players from different platforms.
async def test_get_groupable_players_multiplatform_override(
hass: HomeAssistant,
) -> None:
"""Test that integrations can override async_get_groupable_players for multi-platform support.

This tests cross-platform grouping capability when the service is implemented
to support grouping entities from different platforms.
This demonstrates how an integration can override the default implementation
to return entities from multiple platforms that it can group together.
"""
# Set up demo media player platform
await async_setup_component(
hass, "media_player", {"media_player": {"platform": "demo"}}
)
await hass.async_block_till_done()

# Create entity IDs for different platforms
spotify_id = "media_player.spotify"
sonos_id = "media_player.sonos_living_room"
airplay_id = "media_player.airplay_speaker"

# Mock the async_get_groupable_players method to return cross-platform results
multiplatform_result = {
"result": [
spotify_id,
sonos_id,
airplay_id,
]
}
class MultiPlatformMediaPlayer(MediaPlayerEntity):
"""Test media player that supports grouping across platforms."""

_attr_supported_features = media_player.MediaPlayerEntityFeature.GROUPING

def __init__(self, entity_id: str, name: str) -> None:
"""Initialize the test entity."""
self.entity_id = entity_id
self._attr_name = name

async def async_get_groupable_players(self) -> dict[str, Any]:
"""Override to return multi-platform groupable players."""
# Integration-specific logic that can determine which players
# from different platforms can be grouped together
return {
"result": [
"media_player.spotify_player",
"media_player.sonos_living_room",
"media_player.chromecast_kitchen",
]
}

with patch(
"homeassistant.components.media_player.MediaPlayerEntity.async_get_groupable_players",
return_value=multiplatform_result,
):
# Call the service (not the method directly)
result = await hass.services.async_call(
"media_player",
SERVICE_GET_GROUPABLE_MEMBERS,
{
ATTR_ENTITY_ID: "media_player.walkman"
}, # Music player with grouping support
blocking=True,
return_response=True,
)
# Create and register the custom entity
entity = MultiPlatformMediaPlayer("media_player.test_player", "Test Player")
entity.hass = hass
entity.platform = MockEntityPlatform(hass)

# Add entity to registry and component
component = await media_player.async_get_component(hass)
component.entities.append(entity)
await entity.async_internal_added_to_hass()

# Call the service
result = await hass.services.async_call(
"media_player",
SERVICE_GET_GROUPABLE_MEMBERS,
{ATTR_ENTITY_ID: "media_player.test_player"},
blocking=True,
return_response=True,
)

# Verify service returns proper response structure
assert "media_player.walkman" in result
assert isinstance(result["media_player.walkman"], dict)
assert "result" in result["media_player.walkman"]

# Verify multiplatform results are returned
groupable_players = result["media_player.walkman"]["result"]
assert isinstance(groupable_players, list)
assert len(groupable_players) == 3
assert spotify_id in groupable_players
assert sonos_id in groupable_players
assert airplay_id in groupable_players
# Verify the overridden implementation's results are returned
assert "media_player.test_player" in result
groupable_players = result["media_player.test_player"]["result"]
assert isinstance(groupable_players, list)
assert len(groupable_players) == 3
assert "media_player.spotify_player" in groupable_players
assert "media_player.sonos_living_room" in groupable_players
assert "media_player.chromecast_kitchen" in groupable_players
Loading