Skip to content

Commit 00f7bdb

Browse files
authored
Add more Spotify tests for the media player (#127999)
* Add more Spotify tests for the media player * Fix comments * Rename test
1 parent e682d34 commit 00f7bdb

File tree

1 file changed

+254
-0
lines changed

1 file changed

+254
-0
lines changed

tests/components/spotify/test_media_player.py

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,33 @@
77
from syrupy import SnapshotAssertion
88

99
from homeassistant.components.media_player import (
10+
ATTR_MEDIA_CONTENT_ID,
11+
ATTR_MEDIA_CONTENT_TYPE,
12+
ATTR_MEDIA_ENQUEUE,
13+
ATTR_MEDIA_REPEAT,
14+
ATTR_MEDIA_SEEK_POSITION,
15+
ATTR_MEDIA_SHUFFLE,
16+
ATTR_MEDIA_VOLUME_LEVEL,
17+
DOMAIN as MEDIA_PLAYER_DOMAIN,
18+
SERVICE_PLAY_MEDIA,
19+
MediaPlayerEnqueue,
1020
MediaPlayerEntityFeature,
1121
MediaPlayerState,
22+
MediaType,
23+
RepeatMode,
1224
)
1325
from homeassistant.components.spotify import DOMAIN
26+
from homeassistant.const import (
27+
ATTR_ENTITY_ID,
28+
SERVICE_MEDIA_NEXT_TRACK,
29+
SERVICE_MEDIA_PAUSE,
30+
SERVICE_MEDIA_PLAY,
31+
SERVICE_MEDIA_PREVIOUS_TRACK,
32+
SERVICE_MEDIA_SEEK,
33+
SERVICE_REPEAT_SET,
34+
SERVICE_SHUFFLE_SET,
35+
SERVICE_VOLUME_SET,
36+
)
1437
from homeassistant.core import HomeAssistant
1538
from homeassistant.helpers import entity_registry as er
1639

@@ -137,3 +160,234 @@ async def test_idle(
137160
assert (
138161
state.attributes["supported_features"] == MediaPlayerEntityFeature.SELECT_SOURCE
139162
)
163+
164+
165+
@pytest.mark.usefixtures("setup_credentials")
166+
@pytest.mark.parametrize(
167+
("service", "method"),
168+
[
169+
(SERVICE_MEDIA_PLAY, "start_playback"),
170+
(SERVICE_MEDIA_PAUSE, "pause_playback"),
171+
(SERVICE_MEDIA_PREVIOUS_TRACK, "previous_track"),
172+
(SERVICE_MEDIA_NEXT_TRACK, "next_track"),
173+
],
174+
)
175+
async def test_simple_actions(
176+
hass: HomeAssistant,
177+
mock_spotify: MagicMock,
178+
mock_config_entry: MockConfigEntry,
179+
service: str,
180+
method: str,
181+
) -> None:
182+
"""Test the Spotify media player."""
183+
await setup_integration(hass, mock_config_entry)
184+
await hass.services.async_call(
185+
MEDIA_PLAYER_DOMAIN,
186+
service,
187+
{ATTR_ENTITY_ID: "media_player.spotify_spotify_1"},
188+
blocking=True,
189+
)
190+
getattr(mock_spotify.return_value, method).assert_called_once_with()
191+
192+
193+
@pytest.mark.usefixtures("setup_credentials")
194+
async def test_repeat_mode(
195+
hass: HomeAssistant,
196+
mock_spotify: MagicMock,
197+
mock_config_entry: MockConfigEntry,
198+
) -> None:
199+
"""Test the Spotify media player repeat mode."""
200+
await setup_integration(hass, mock_config_entry)
201+
for mode, spotify_mode in (
202+
(RepeatMode.ALL, "context"),
203+
(RepeatMode.ONE, "track"),
204+
(RepeatMode.OFF, "off"),
205+
):
206+
await hass.services.async_call(
207+
MEDIA_PLAYER_DOMAIN,
208+
SERVICE_REPEAT_SET,
209+
{ATTR_ENTITY_ID: "media_player.spotify_spotify_1", ATTR_MEDIA_REPEAT: mode},
210+
blocking=True,
211+
)
212+
mock_spotify.return_value.repeat.assert_called_once_with(spotify_mode)
213+
mock_spotify.return_value.repeat.reset_mock()
214+
215+
216+
@pytest.mark.usefixtures("setup_credentials")
217+
async def test_shuffle(
218+
hass: HomeAssistant,
219+
mock_spotify: MagicMock,
220+
mock_config_entry: MockConfigEntry,
221+
) -> None:
222+
"""Test the Spotify media player shuffle."""
223+
await setup_integration(hass, mock_config_entry)
224+
for shuffle in (True, False):
225+
await hass.services.async_call(
226+
MEDIA_PLAYER_DOMAIN,
227+
SERVICE_SHUFFLE_SET,
228+
{
229+
ATTR_ENTITY_ID: "media_player.spotify_spotify_1",
230+
ATTR_MEDIA_SHUFFLE: shuffle,
231+
},
232+
blocking=True,
233+
)
234+
mock_spotify.return_value.shuffle.assert_called_once_with(shuffle)
235+
mock_spotify.return_value.shuffle.reset_mock()
236+
237+
238+
@pytest.mark.usefixtures("setup_credentials")
239+
async def test_volume_level(
240+
hass: HomeAssistant,
241+
mock_spotify: MagicMock,
242+
mock_config_entry: MockConfigEntry,
243+
) -> None:
244+
"""Test the Spotify media player volume level."""
245+
await setup_integration(hass, mock_config_entry)
246+
await hass.services.async_call(
247+
MEDIA_PLAYER_DOMAIN,
248+
SERVICE_VOLUME_SET,
249+
{
250+
ATTR_ENTITY_ID: "media_player.spotify_spotify_1",
251+
ATTR_MEDIA_VOLUME_LEVEL: 0.5,
252+
},
253+
blocking=True,
254+
)
255+
mock_spotify.return_value.volume.assert_called_with(50)
256+
257+
258+
@pytest.mark.usefixtures("setup_credentials")
259+
async def test_seek(
260+
hass: HomeAssistant,
261+
mock_spotify: MagicMock,
262+
mock_config_entry: MockConfigEntry,
263+
) -> None:
264+
"""Test the Spotify media player seeking."""
265+
await setup_integration(hass, mock_config_entry)
266+
await hass.services.async_call(
267+
MEDIA_PLAYER_DOMAIN,
268+
SERVICE_MEDIA_SEEK,
269+
{
270+
ATTR_ENTITY_ID: "media_player.spotify_spotify_1",
271+
ATTR_MEDIA_SEEK_POSITION: 100,
272+
},
273+
blocking=True,
274+
)
275+
mock_spotify.return_value.seek_track.assert_called_with(100000)
276+
277+
278+
@pytest.mark.usefixtures("setup_credentials")
279+
@pytest.mark.parametrize(
280+
("media_type", "media_id"),
281+
[
282+
("spotify://track", "spotify:track:3oRoMXsP2NRzm51lldj1RO"),
283+
("spotify://episode", "spotify:episode:3oRoMXsP2NRzm51lldj1RO"),
284+
(MediaType.MUSIC, "spotify:track:3oRoMXsP2NRzm51lldj1RO"),
285+
],
286+
)
287+
async def test_play_media_in_queue(
288+
hass: HomeAssistant,
289+
mock_spotify: MagicMock,
290+
mock_config_entry: MockConfigEntry,
291+
media_type: str,
292+
media_id: str,
293+
) -> None:
294+
"""Test the Spotify media player play media."""
295+
await setup_integration(hass, mock_config_entry)
296+
await hass.services.async_call(
297+
MEDIA_PLAYER_DOMAIN,
298+
SERVICE_PLAY_MEDIA,
299+
{
300+
ATTR_ENTITY_ID: "media_player.spotify_spotify_1",
301+
ATTR_MEDIA_CONTENT_TYPE: media_type,
302+
ATTR_MEDIA_CONTENT_ID: media_id,
303+
ATTR_MEDIA_ENQUEUE: MediaPlayerEnqueue.ADD,
304+
},
305+
blocking=True,
306+
)
307+
mock_spotify.return_value.add_to_queue.assert_called_with(media_id, None)
308+
309+
310+
@pytest.mark.usefixtures("setup_credentials")
311+
@pytest.mark.parametrize(
312+
("media_type", "media_id", "called_with"),
313+
[
314+
(
315+
"spotify://artist",
316+
"spotify:artist:74Yus6IHfa3tWZzXXAYtS2",
317+
{"context_uri": "spotify:artist:74Yus6IHfa3tWZzXXAYtS2"},
318+
),
319+
(
320+
"spotify://playlist",
321+
"spotify:playlist:74Yus6IHfa3tWZzXXAYtS2",
322+
{"context_uri": "spotify:playlist:74Yus6IHfa3tWZzXXAYtS2"},
323+
),
324+
(
325+
"spotify://album",
326+
"spotify:album:74Yus6IHfa3tWZzXXAYtS2",
327+
{"context_uri": "spotify:album:74Yus6IHfa3tWZzXXAYtS2"},
328+
),
329+
(
330+
"spotify://show",
331+
"spotify:show:74Yus6IHfa3tWZzXXAYtS2",
332+
{"context_uri": "spotify:show:74Yus6IHfa3tWZzXXAYtS2"},
333+
),
334+
(
335+
MediaType.MUSIC,
336+
"spotify:track:3oRoMXsP2NRzm51lldj1RO",
337+
{"uris": ["spotify:track:3oRoMXsP2NRzm51lldj1RO"]},
338+
),
339+
(
340+
"spotify://track",
341+
"spotify:track:3oRoMXsP2NRzm51lldj1RO",
342+
{"uris": ["spotify:track:3oRoMXsP2NRzm51lldj1RO"]},
343+
),
344+
(
345+
"spotify://episode",
346+
"spotify:episode:3oRoMXsP2NRzm51lldj1RO",
347+
{"uris": ["spotify:episode:3oRoMXsP2NRzm51lldj1RO"]},
348+
),
349+
],
350+
)
351+
async def test_play_media(
352+
hass: HomeAssistant,
353+
mock_spotify: MagicMock,
354+
mock_config_entry: MockConfigEntry,
355+
media_type: str,
356+
media_id: str,
357+
called_with: dict,
358+
) -> None:
359+
"""Test the Spotify media player play media."""
360+
await setup_integration(hass, mock_config_entry)
361+
await hass.services.async_call(
362+
MEDIA_PLAYER_DOMAIN,
363+
SERVICE_PLAY_MEDIA,
364+
{
365+
ATTR_ENTITY_ID: "media_player.spotify_spotify_1",
366+
ATTR_MEDIA_CONTENT_TYPE: media_type,
367+
ATTR_MEDIA_CONTENT_ID: media_id,
368+
},
369+
blocking=True,
370+
)
371+
mock_spotify.return_value.start_playback.assert_called_with(**called_with)
372+
373+
374+
@pytest.mark.usefixtures("setup_credentials")
375+
async def test_play_unsupported_media(
376+
hass: HomeAssistant,
377+
mock_spotify: MagicMock,
378+
mock_config_entry: MockConfigEntry,
379+
) -> None:
380+
"""Test the Spotify media player play media."""
381+
await setup_integration(hass, mock_config_entry)
382+
await hass.services.async_call(
383+
MEDIA_PLAYER_DOMAIN,
384+
SERVICE_PLAY_MEDIA,
385+
{
386+
ATTR_ENTITY_ID: "media_player.spotify_spotify_1",
387+
ATTR_MEDIA_CONTENT_TYPE: MediaType.COMPOSER,
388+
ATTR_MEDIA_CONTENT_ID: "spotify:track:3oRoMXsP2NRzm51lldj1RO",
389+
},
390+
blocking=True,
391+
)
392+
assert mock_spotify.return_value.start_playback.call_count == 0
393+
assert mock_spotify.return_value.add_to_queue.call_count == 0

0 commit comments

Comments
 (0)