From 3672889609c79704bd1e8198dc0a43b0cdd5bce6 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Sun, 24 Oct 2021 17:51:45 -0500 Subject: [PATCH] Add warning if Sonos not linked to Plex (#58150) --- homeassistant/components/plex/services.py | 10 ++++++++-- tests/components/plex/test_services.py | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/plex/services.py b/homeassistant/components/plex/services.py index a5faa56a8bb..32af3c429dc 100644 --- a/homeassistant/components/plex/services.py +++ b/homeassistant/components/plex/services.py @@ -2,7 +2,7 @@ import json import logging -from plexapi.exceptions import NotFound +from plexapi.exceptions import BadRequest, NotFound import voluptuous as vol from homeassistant.exceptions import HomeAssistantError @@ -133,7 +133,13 @@ def play_on_sonos(hass, content_type, content_id, speaker_name): Called by Sonos 'media_player.play_media' service. """ media, plex_server = lookup_plex_media(hass, content_type, content_id) - sonos_speaker = plex_server.account.sonos_speaker(speaker_name) + try: + sonos_speaker = plex_server.account.sonos_speaker(speaker_name) + except BadRequest as exc: + raise HomeAssistantError( + "Sonos speakers not linked to Plex account, complete this step in the Plex app" + ) from exc + if sonos_speaker is None: message = f"Sonos speaker '{speaker_name}' is not associated with '{plex_server.friendly_name}'" _LOGGER.error(message) diff --git a/tests/components/plex/test_services.py b/tests/components/plex/test_services.py index 778d9cbde63..161755c9703 100644 --- a/tests/components/plex/test_services.py +++ b/tests/components/plex/test_services.py @@ -154,6 +154,13 @@ async def test_sonos_play_media( await hass.config_entries.async_unload(entry.entry_id) mock_plex_server = await setup_plex_server() + # Test with unlinked Plex/Sonos accounts + requests_mock.get("https://sonos.plex.tv/resources", status_code=403) + with pytest.raises(HomeAssistantError) as excinfo: + play_on_sonos(hass, MEDIA_TYPE_MUSIC, media_content_id, sonos_speaker_name) + assert "Sonos speakers not linked to Plex account" in str(excinfo.value) + assert playback_mock.call_count == 0 + # Test with no speakers available requests_mock.get("https://sonos.plex.tv/resources", text=empty_payload) with pytest.raises(HomeAssistantError) as excinfo: