Normalize enqueuing Plex media on Sonos (#68132)

This commit is contained in:
jjlawren 2022-03-17 15:52:59 -05:00 committed by GitHub
parent a8dae97917
commit f75d621888
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 10 deletions

View File

@ -574,11 +574,14 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
else: else:
shuffle = False shuffle = False
media = lookup_plex_media(self.hass, media_type, json.dumps(payload)) media = lookup_plex_media(self.hass, media_type, json.dumps(payload))
if not kwargs.get(ATTR_MEDIA_ENQUEUE):
soco.clear_queue()
if shuffle: if shuffle:
self.set_shuffle(True) self.set_shuffle(True)
plex_plugin.play_now(media) if kwargs.get(ATTR_MEDIA_ENQUEUE):
plex_plugin.add_to_queue(media)
else:
soco.clear_queue()
plex_plugin.add_to_queue(media)
soco.play_from_queue(0)
return return
share_link = self.coordinator.share_link share_link = self.coordinator.share_link

View File

@ -25,8 +25,8 @@ async def test_plex_play_media(hass, async_autosetup_sonos):
with patch( with patch(
"homeassistant.components.sonos.media_player.lookup_plex_media" "homeassistant.components.sonos.media_player.lookup_plex_media"
) as mock_lookup, patch( ) as mock_lookup, patch(
"soco.plugins.plex.PlexPlugin.play_now" "soco.plugins.plex.PlexPlugin.add_to_queue"
) as mock_play_now, patch( ) as mock_add_to_queue, patch(
"homeassistant.components.sonos.media_player.SonosMediaPlayerEntity.set_shuffle" "homeassistant.components.sonos.media_player.SonosMediaPlayerEntity.set_shuffle"
) as mock_shuffle: ) as mock_shuffle:
# Test successful Plex service call # Test successful Plex service call
@ -42,14 +42,14 @@ async def test_plex_play_media(hass, async_autosetup_sonos):
) )
assert len(mock_lookup.mock_calls) == 1 assert len(mock_lookup.mock_calls) == 1
assert len(mock_play_now.mock_calls) == 1 assert len(mock_add_to_queue.mock_calls) == 1
assert not mock_shuffle.called assert not mock_shuffle.called
assert mock_lookup.mock_calls[0][1][1] == MEDIA_TYPE_MUSIC assert mock_lookup.mock_calls[0][1][1] == MEDIA_TYPE_MUSIC
assert mock_lookup.mock_calls[0][1][2] == media_content_id assert mock_lookup.mock_calls[0][1][2] == media_content_id
# Test handling shuffle in payload # Test handling shuffle in payload
mock_lookup.reset_mock() mock_lookup.reset_mock()
mock_play_now.reset_mock() mock_add_to_queue.reset_mock()
shuffle_media_content_id = '{"library_name": "Music", "artist_name": "Artist", "album_name": "Album", "shuffle": 1}' shuffle_media_content_id = '{"library_name": "Music", "artist_name": "Artist", "album_name": "Album", "shuffle": 1}'
assert await hass.services.async_call( assert await hass.services.async_call(
@ -65,14 +65,14 @@ async def test_plex_play_media(hass, async_autosetup_sonos):
assert mock_shuffle.called assert mock_shuffle.called
assert len(mock_lookup.mock_calls) == 1 assert len(mock_lookup.mock_calls) == 1
assert len(mock_play_now.mock_calls) == 1 assert len(mock_add_to_queue.mock_calls) == 1
assert mock_lookup.mock_calls[0][1][1] == MEDIA_TYPE_MUSIC assert mock_lookup.mock_calls[0][1][1] == MEDIA_TYPE_MUSIC
assert mock_lookup.mock_calls[0][1][2] == media_content_id assert mock_lookup.mock_calls[0][1][2] == media_content_id
# Test failed Plex service call # Test failed Plex service call
mock_lookup.reset_mock() mock_lookup.reset_mock()
mock_lookup.side_effect = HomeAssistantError mock_lookup.side_effect = HomeAssistantError
mock_play_now.reset_mock() mock_add_to_queue.reset_mock()
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await hass.services.async_call( await hass.services.async_call(
@ -86,4 +86,4 @@ async def test_plex_play_media(hass, async_autosetup_sonos):
blocking=True, blocking=True,
) )
assert mock_lookup.called assert mock_lookup.called
assert not mock_play_now.called assert not mock_add_to_queue.called