2021-01-13 14:24:44 +00:00
|
|
|
"""Tests for the Sonos Media Player platform."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.media_player.const import (
|
|
|
|
ATTR_MEDIA_CONTENT_ID,
|
|
|
|
ATTR_MEDIA_CONTENT_TYPE,
|
|
|
|
DOMAIN as MP_DOMAIN,
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
SERVICE_PLAY_MEDIA,
|
|
|
|
)
|
2021-01-21 10:22:19 +00:00
|
|
|
from homeassistant.components.plex.const import PLEX_URI_SCHEME
|
2021-01-13 14:24:44 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
|
|
|
|
|
2021-12-08 18:28:27 +00:00
|
|
|
async def test_plex_play_media(hass, async_autosetup_sonos):
|
2021-01-13 14:24:44 +00:00
|
|
|
"""Test playing media via the Plex integration."""
|
|
|
|
media_player = "media_player.zone_a"
|
2021-01-21 10:22:19 +00:00
|
|
|
media_content_id = (
|
|
|
|
'{"library_name": "Music", "artist_name": "Artist", "album_name": "Album"}'
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.sonos.media_player.play_on_sonos"
|
|
|
|
) as mock_play:
|
|
|
|
# Test successful Plex service call
|
2021-01-13 14:24:44 +00:00
|
|
|
assert await hass.services.async_call(
|
|
|
|
MP_DOMAIN,
|
|
|
|
SERVICE_PLAY_MEDIA,
|
|
|
|
{
|
|
|
|
ATTR_ENTITY_ID: media_player,
|
|
|
|
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
|
2021-01-21 10:22:19 +00:00
|
|
|
ATTR_MEDIA_CONTENT_ID: f"{PLEX_URI_SCHEME}{media_content_id}",
|
2021-01-13 14:24:44 +00:00
|
|
|
},
|
2021-01-21 10:22:19 +00:00
|
|
|
blocking=True,
|
2021-01-13 14:24:44 +00:00
|
|
|
)
|
|
|
|
|
2021-01-21 10:22:19 +00:00
|
|
|
assert len(mock_play.mock_calls) == 1
|
|
|
|
assert mock_play.mock_calls[0][1][1] == MEDIA_TYPE_MUSIC
|
|
|
|
assert mock_play.mock_calls[0][1][2] == media_content_id
|
|
|
|
assert mock_play.mock_calls[0][1][3] == "Zone A"
|
|
|
|
|
|
|
|
# Test failed Plex service call
|
|
|
|
mock_play.reset_mock()
|
|
|
|
mock_play.side_effect = HomeAssistantError
|
|
|
|
|
|
|
|
with pytest.raises(HomeAssistantError):
|
|
|
|
await hass.services.async_call(
|
|
|
|
MP_DOMAIN,
|
|
|
|
SERVICE_PLAY_MEDIA,
|
|
|
|
{
|
|
|
|
ATTR_ENTITY_ID: media_player,
|
|
|
|
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
|
|
|
|
ATTR_MEDIA_CONTENT_ID: f"{PLEX_URI_SCHEME}{media_content_id}",
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
assert mock_play.called
|