Add warning if Sonos not linked to Plex (#58150)

This commit is contained in:
jjlawren 2021-10-24 17:51:45 -05:00 committed by GitHub
parent 2a6247cf20
commit 3672889609
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View file

@ -2,7 +2,7 @@
import json import json
import logging import logging
from plexapi.exceptions import NotFound from plexapi.exceptions import BadRequest, NotFound
import voluptuous as vol import voluptuous as vol
from homeassistant.exceptions import HomeAssistantError 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. Called by Sonos 'media_player.play_media' service.
""" """
media, plex_server = lookup_plex_media(hass, content_type, content_id) media, plex_server = lookup_plex_media(hass, content_type, content_id)
try:
sonos_speaker = plex_server.account.sonos_speaker(speaker_name) 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: if sonos_speaker is None:
message = f"Sonos speaker '{speaker_name}' is not associated with '{plex_server.friendly_name}'" message = f"Sonos speaker '{speaker_name}' is not associated with '{plex_server.friendly_name}'"
_LOGGER.error(message) _LOGGER.error(message)

View file

@ -154,6 +154,13 @@ async def test_sonos_play_media(
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
mock_plex_server = await setup_plex_server() 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 # Test with no speakers available
requests_mock.get("https://sonos.plex.tv/resources", text=empty_payload) requests_mock.get("https://sonos.plex.tv/resources", text=empty_payload)
with pytest.raises(HomeAssistantError) as excinfo: with pytest.raises(HomeAssistantError) as excinfo: