Fix Spotify Media Browsing fails for new config entries (#124368)
* initial commit * tests * tests * update tests * update tests * update testspull/124378/head
parent
d86b816491
commit
9399a54c7a
|
@ -172,10 +172,17 @@ async def async_browse_media(
|
||||||
|
|
||||||
# Check for config entry specifier, and extract Spotify URI
|
# Check for config entry specifier, and extract Spotify URI
|
||||||
parsed_url = yarl.URL(media_content_id)
|
parsed_url = yarl.URL(media_content_id)
|
||||||
|
host = parsed_url.host
|
||||||
|
|
||||||
if (
|
if (
|
||||||
parsed_url.host is None
|
host is None
|
||||||
or (entry := hass.config_entries.async_get_entry(parsed_url.host)) is None
|
# config entry ids can be upper or lower case. Yarl always returns host
|
||||||
|
# names in lower case, so we need to look for the config entry in both
|
||||||
|
or (
|
||||||
|
entry := hass.config_entries.async_get_entry(host)
|
||||||
|
or hass.config_entries.async_get_entry(host.upper())
|
||||||
|
)
|
||||||
|
is None
|
||||||
or not isinstance(entry.runtime_data, HomeAssistantSpotifyData)
|
or not isinstance(entry.runtime_data, HomeAssistantSpotifyData)
|
||||||
):
|
):
|
||||||
raise BrowseError("Invalid Spotify account specified")
|
raise BrowseError("Invalid Spotify account specified")
|
||||||
|
|
|
@ -0,0 +1,128 @@
|
||||||
|
"""Common test fixtures."""
|
||||||
|
|
||||||
|
from collections.abc import Generator
|
||||||
|
from typing import Any
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components.application_credentials import (
|
||||||
|
ClientCredential,
|
||||||
|
async_import_client_credential,
|
||||||
|
)
|
||||||
|
from homeassistant.components.spotify import DOMAIN
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_config_entry_1() -> MockConfigEntry:
|
||||||
|
"""Mock a config entry with an upper case entry id."""
|
||||||
|
return MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
title="spotify_1",
|
||||||
|
data={
|
||||||
|
"auth_implementation": "spotify_c95e4090d4d3438b922331e7428f8171",
|
||||||
|
"token": {
|
||||||
|
"access_token": "AccessToken",
|
||||||
|
"token_type": "Bearer",
|
||||||
|
"expires_in": 3600,
|
||||||
|
"refresh_token": "RefreshToken",
|
||||||
|
"scope": "playlist-read-private ...",
|
||||||
|
"expires_at": 1724198975.8829377,
|
||||||
|
},
|
||||||
|
"id": "32oesphrnacjcf7vw5bf6odx3oiu",
|
||||||
|
"name": "spotify_account_1",
|
||||||
|
},
|
||||||
|
unique_id="84fce612f5b8",
|
||||||
|
entry_id="01J5TX5A0FF6G5V0QJX6HBC94T",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_config_entry_2() -> MockConfigEntry:
|
||||||
|
"""Mock a config entry with a lower case entry id."""
|
||||||
|
return MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
title="spotify_2",
|
||||||
|
data={
|
||||||
|
"auth_implementation": "spotify_c95e4090d4d3438b922331e7428f8171",
|
||||||
|
"token": {
|
||||||
|
"access_token": "AccessToken",
|
||||||
|
"token_type": "Bearer",
|
||||||
|
"expires_in": 3600,
|
||||||
|
"refresh_token": "RefreshToken",
|
||||||
|
"scope": "playlist-read-private ...",
|
||||||
|
"expires_at": 1724198975.8829377,
|
||||||
|
},
|
||||||
|
"id": "55oesphrnacjcf7vw5bf6odx3oiu",
|
||||||
|
"name": "spotify_account_2",
|
||||||
|
},
|
||||||
|
unique_id="99fce612f5b8",
|
||||||
|
entry_id="32oesphrnacjcf7vw5bf6odx3",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def spotify_playlists() -> dict[str, Any]:
|
||||||
|
"""Mock the return from getting a list of playlists."""
|
||||||
|
return {
|
||||||
|
"href": "https://api.spotify.com/v1/users/31oesphrnacjcf7vw5bf6odx3oiu/playlists?offset=0&limit=48",
|
||||||
|
"limit": 48,
|
||||||
|
"next": None,
|
||||||
|
"offset": 0,
|
||||||
|
"previous": None,
|
||||||
|
"total": 1,
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"collaborative": False,
|
||||||
|
"description": "",
|
||||||
|
"id": "unique_identifier_00",
|
||||||
|
"name": "Playlist1",
|
||||||
|
"type": "playlist",
|
||||||
|
"uri": "spotify:playlist:unique_identifier_00",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def spotify_mock(spotify_playlists: dict[str, Any]) -> Generator[MagicMock]:
|
||||||
|
"""Mock the Spotify API."""
|
||||||
|
with patch("homeassistant.components.spotify.Spotify") as spotify_mock:
|
||||||
|
mock = MagicMock()
|
||||||
|
mock.current_user_playlists.return_value = spotify_playlists
|
||||||
|
spotify_mock.return_value = mock
|
||||||
|
yield spotify_mock
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def spotify_setup(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
spotify_mock: MagicMock,
|
||||||
|
mock_config_entry_1: MockConfigEntry,
|
||||||
|
mock_config_entry_2: MockConfigEntry,
|
||||||
|
):
|
||||||
|
"""Set up the spotify integration."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.spotify.OAuth2Session.async_ensure_token_valid"
|
||||||
|
):
|
||||||
|
await async_setup_component(hass, "application_credentials", {})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await async_import_client_credential(
|
||||||
|
hass,
|
||||||
|
DOMAIN,
|
||||||
|
ClientCredential("CLIENT_ID", "CLIENT_SECRET"),
|
||||||
|
"spotify_c95e4090d4d3438b922331e7428f8171",
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
mock_config_entry_1.add_to_hass(hass)
|
||||||
|
await hass.config_entries.async_setup(mock_config_entry_1.entry_id)
|
||||||
|
mock_config_entry_2.add_to_hass(hass)
|
||||||
|
await hass.config_entries.async_setup(mock_config_entry_2.entry_id)
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
await async_setup_component(hass, DOMAIN, {})
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
yield
|
|
@ -0,0 +1,236 @@
|
||||||
|
# serializer version: 1
|
||||||
|
# name: test_browse_media_categories
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children': list([
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.PLAYLIST: 'playlist'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_playlists',
|
||||||
|
'media_content_type': 'spotify://current_user_playlists',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Playlists',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.ARTIST: 'artist'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_followed_artists',
|
||||||
|
'media_content_type': 'spotify://current_user_followed_artists',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Artists',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.ALBUM: 'album'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_saved_albums',
|
||||||
|
'media_content_type': 'spotify://current_user_saved_albums',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Albums',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.TRACK: 'track'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_saved_tracks',
|
||||||
|
'media_content_type': 'spotify://current_user_saved_tracks',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Tracks',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.PODCAST: 'podcast'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_saved_shows',
|
||||||
|
'media_content_type': 'spotify://current_user_saved_shows',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Podcasts',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.TRACK: 'track'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_recently_played',
|
||||||
|
'media_content_type': 'spotify://current_user_recently_played',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Recently played',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.ARTIST: 'artist'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_top_artists',
|
||||||
|
'media_content_type': 'spotify://current_user_top_artists',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Top Artists',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.TRACK: 'track'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_top_tracks',
|
||||||
|
'media_content_type': 'spotify://current_user_top_tracks',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Top Tracks',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.GENRE: 'genre'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/categories',
|
||||||
|
'media_content_type': 'spotify://categories',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Categories',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.PLAYLIST: 'playlist'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/featured_playlists',
|
||||||
|
'media_content_type': 'spotify://featured_playlists',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Featured Playlists',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': <MediaClass.ALBUM: 'album'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/new_releases',
|
||||||
|
'media_content_type': 'spotify://new_releases',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'New Releases',
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
'children_media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/library',
|
||||||
|
'media_content_type': 'spotify://library',
|
||||||
|
'not_shown': 0,
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Media Library',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_browse_media_playlists
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children': list([
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': True,
|
||||||
|
'children_media_class': <MediaClass.TRACK: 'track'>,
|
||||||
|
'media_class': <MediaClass.PLAYLIST: 'playlist'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/spotify:playlist:unique_identifier_00',
|
||||||
|
'media_content_type': 'spotify://playlist',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Playlist1',
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
'children_media_class': <MediaClass.PLAYLIST: 'playlist'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_playlists',
|
||||||
|
'media_content_type': 'spotify://current_user_playlists',
|
||||||
|
'not_shown': 0,
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Playlists',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_browse_media_playlists[01J5TX5A0FF6G5V0QJX6HBC94T]
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children': list([
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': True,
|
||||||
|
'children_media_class': <MediaClass.TRACK: 'track'>,
|
||||||
|
'media_class': <MediaClass.PLAYLIST: 'playlist'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/spotify:playlist:unique_identifier_00',
|
||||||
|
'media_content_type': 'spotify://playlist',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Playlist1',
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
'children_media_class': <MediaClass.PLAYLIST: 'playlist'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_playlists',
|
||||||
|
'media_content_type': 'spotify://current_user_playlists',
|
||||||
|
'not_shown': 0,
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Playlists',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_browse_media_playlists[32oesphrnacjcf7vw5bf6odx3]
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children': list([
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': True,
|
||||||
|
'children_media_class': <MediaClass.TRACK: 'track'>,
|
||||||
|
'media_class': <MediaClass.PLAYLIST: 'playlist'>,
|
||||||
|
'media_content_id': 'spotify://32oesphrnacjcf7vw5bf6odx3/spotify:playlist:unique_identifier_00',
|
||||||
|
'media_content_type': 'spotify://playlist',
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Playlist1',
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
'children_media_class': <MediaClass.PLAYLIST: 'playlist'>,
|
||||||
|
'media_class': <MediaClass.DIRECTORY: 'directory'>,
|
||||||
|
'media_content_id': 'spotify://32oesphrnacjcf7vw5bf6odx3/current_user_playlists',
|
||||||
|
'media_content_type': 'spotify://current_user_playlists',
|
||||||
|
'not_shown': 0,
|
||||||
|
'thumbnail': None,
|
||||||
|
'title': 'Playlists',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_browse_media_root
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children': list([
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': None,
|
||||||
|
'media_class': <MediaClass.APP: 'app'>,
|
||||||
|
'media_content_id': 'spotify://01J5TX5A0FF6G5V0QJX6HBC94T',
|
||||||
|
'media_content_type': 'spotify://library',
|
||||||
|
'thumbnail': 'https://brands.home-assistant.io/_/spotify/logo.png',
|
||||||
|
'title': 'spotify_1',
|
||||||
|
}),
|
||||||
|
dict({
|
||||||
|
'can_expand': True,
|
||||||
|
'can_play': False,
|
||||||
|
'children_media_class': None,
|
||||||
|
'media_class': <MediaClass.APP: 'app'>,
|
||||||
|
'media_content_id': 'spotify://32oesphrnacjcf7vw5bf6odx3',
|
||||||
|
'media_content_type': 'spotify://library',
|
||||||
|
'thumbnail': 'https://brands.home-assistant.io/_/spotify/logo.png',
|
||||||
|
'title': 'spotify_2',
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
'children_media_class': <MediaClass.APP: 'app'>,
|
||||||
|
'media_class': <MediaClass.APP: 'app'>,
|
||||||
|
'media_content_id': 'spotify://',
|
||||||
|
'media_content_type': 'spotify',
|
||||||
|
'not_shown': 0,
|
||||||
|
'thumbnail': 'https://brands.home-assistant.io/_/spotify/logo.png',
|
||||||
|
'title': 'Spotify',
|
||||||
|
})
|
||||||
|
# ---
|
|
@ -0,0 +1,61 @@
|
||||||
|
"""Test the media browser interface."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from syrupy import SnapshotAssertion
|
||||||
|
|
||||||
|
from homeassistant.components.spotify import DOMAIN
|
||||||
|
from homeassistant.components.spotify.browse_media import async_browse_media
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
|
async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
|
||||||
|
"""Fixture for setting up the component."""
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
await async_setup_component(hass, DOMAIN, {})
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_browse_media_root(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
spotify_setup,
|
||||||
|
) -> None:
|
||||||
|
"""Test browsing the root."""
|
||||||
|
response = await async_browse_media(hass, None, None)
|
||||||
|
assert response.as_dict() == snapshot
|
||||||
|
|
||||||
|
|
||||||
|
async def test_browse_media_categories(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
spotify_setup,
|
||||||
|
) -> None:
|
||||||
|
"""Test browsing categories."""
|
||||||
|
response = await async_browse_media(
|
||||||
|
hass, "spotify://library", "spotify://01J5TX5A0FF6G5V0QJX6HBC94T"
|
||||||
|
)
|
||||||
|
assert response.as_dict() == snapshot
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("config_entry_id"), [("01J5TX5A0FF6G5V0QJX6HBC94T"), ("32oesphrnacjcf7vw5bf6odx3")]
|
||||||
|
)
|
||||||
|
async def test_browse_media_playlists(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
config_entry_id: str,
|
||||||
|
spotify_setup,
|
||||||
|
) -> None:
|
||||||
|
"""Test browsing playlists for the two config entries."""
|
||||||
|
response = await async_browse_media(
|
||||||
|
hass,
|
||||||
|
"spotify://current_user_playlists",
|
||||||
|
f"spotify://{config_entry_id}/current_user_playlists",
|
||||||
|
)
|
||||||
|
assert response.as_dict() == snapshot
|
Loading…
Reference in New Issue