Fix Sonos speaker lookup for Plex (#37942)
This commit is contained in:
parent
1582e4347d
commit
a7dfa60208
4 changed files with 19 additions and 17 deletions
|
@ -215,7 +215,7 @@ def play_on_sonos(hass, service_call):
|
||||||
|
|
||||||
sonos = hass.components.sonos
|
sonos = hass.components.sonos
|
||||||
try:
|
try:
|
||||||
sonos_id = sonos.get_coordinator_id(entity_id)
|
sonos_name = sonos.get_coordinator_name(entity_id)
|
||||||
except HomeAssistantError as err:
|
except HomeAssistantError as err:
|
||||||
_LOGGER.error("Cannot get Sonos device: %s", err)
|
_LOGGER.error("Cannot get Sonos device: %s", err)
|
||||||
return
|
return
|
||||||
|
@ -239,10 +239,10 @@ def play_on_sonos(hass, service_call):
|
||||||
else:
|
else:
|
||||||
plex_server = next(iter(plex_servers))
|
plex_server = next(iter(plex_servers))
|
||||||
|
|
||||||
sonos_speaker = plex_server.account.sonos_speaker_by_id(sonos_id)
|
sonos_speaker = plex_server.account.sonos_speaker(sonos_name)
|
||||||
if sonos_speaker is None:
|
if sonos_speaker is None:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Sonos speaker '%s' could not be found on this Plex account", sonos_id
|
"Sonos speaker '%s' could not be found on this Plex account", sonos_name
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -58,8 +58,10 @@ async def async_setup_entry(hass, entry):
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def get_coordinator_id(hass, entity_id):
|
def get_coordinator_name(hass, entity_id):
|
||||||
"""Obtain the unique_id of a device's coordinator.
|
"""Obtain the room/name of a device's coordinator.
|
||||||
|
|
||||||
|
Used by the Plex integration.
|
||||||
|
|
||||||
This function is safe to run inside the event loop.
|
This function is safe to run inside the event loop.
|
||||||
"""
|
"""
|
||||||
|
@ -71,5 +73,5 @@ def get_coordinator_id(hass, entity_id):
|
||||||
)
|
)
|
||||||
|
|
||||||
if device.is_coordinator:
|
if device.is_coordinator:
|
||||||
return device.unique_id
|
return device.name
|
||||||
return device.coordinator.unique_id
|
return device.coordinator.name
|
||||||
|
|
|
@ -78,9 +78,9 @@ class MockPlexAccount:
|
||||||
"""Mock the PlexAccount resources listing method."""
|
"""Mock the PlexAccount resources listing method."""
|
||||||
return self._resources
|
return self._resources
|
||||||
|
|
||||||
def sonos_speaker_by_id(self, machine_identifier):
|
def sonos_speaker(self, speaker_name):
|
||||||
"""Mock the PlexAccount Sonos lookup method."""
|
"""Mock the PlexAccount Sonos lookup method."""
|
||||||
return MockPlexSonosClient(machine_identifier)
|
return MockPlexSonosClient(speaker_name)
|
||||||
|
|
||||||
|
|
||||||
class MockPlexSystemAccount:
|
class MockPlexSystemAccount:
|
||||||
|
@ -378,9 +378,9 @@ class MockPlexMediaTrack(MockPlexMediaItem):
|
||||||
class MockPlexSonosClient:
|
class MockPlexSonosClient:
|
||||||
"""Mock a PlexSonosClient instance."""
|
"""Mock a PlexSonosClient instance."""
|
||||||
|
|
||||||
def __init__(self, machine_identifier):
|
def __init__(self, name):
|
||||||
"""Initialize the object."""
|
"""Initialize the object."""
|
||||||
self.machineIdentifier = machine_identifier
|
self.name = name
|
||||||
|
|
||||||
def playMedia(self, item):
|
def playMedia(self, item):
|
||||||
"""Mock the playMedia method."""
|
"""Mock the playMedia method."""
|
||||||
|
|
|
@ -39,7 +39,7 @@ async def test_sonos_playback(hass):
|
||||||
|
|
||||||
# Test Sonos integration lookup failure
|
# Test Sonos integration lookup failure
|
||||||
with patch.object(
|
with patch.object(
|
||||||
hass.components.sonos, "get_coordinator_id", side_effect=HomeAssistantError
|
hass.components.sonos, "get_coordinator_name", side_effect=HomeAssistantError
|
||||||
):
|
):
|
||||||
assert await hass.services.async_call(
|
assert await hass.services.async_call(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
@ -55,7 +55,7 @@ async def test_sonos_playback(hass):
|
||||||
# Test success with dict
|
# Test success with dict
|
||||||
with patch.object(
|
with patch.object(
|
||||||
hass.components.sonos,
|
hass.components.sonos,
|
||||||
"get_coordinator_id",
|
"get_coordinator_name",
|
||||||
return_value="media_player.sonos_kitchen",
|
return_value="media_player.sonos_kitchen",
|
||||||
), patch("plexapi.playqueue.PlayQueue.create"):
|
), patch("plexapi.playqueue.PlayQueue.create"):
|
||||||
assert await hass.services.async_call(
|
assert await hass.services.async_call(
|
||||||
|
@ -72,7 +72,7 @@ async def test_sonos_playback(hass):
|
||||||
# Test success with plex_key
|
# Test success with plex_key
|
||||||
with patch.object(
|
with patch.object(
|
||||||
hass.components.sonos,
|
hass.components.sonos,
|
||||||
"get_coordinator_id",
|
"get_coordinator_name",
|
||||||
return_value="media_player.sonos_kitchen",
|
return_value="media_player.sonos_kitchen",
|
||||||
), patch("plexapi.playqueue.PlayQueue.create"):
|
), patch("plexapi.playqueue.PlayQueue.create"):
|
||||||
assert await hass.services.async_call(
|
assert await hass.services.async_call(
|
||||||
|
@ -89,7 +89,7 @@ async def test_sonos_playback(hass):
|
||||||
# Test invalid Plex server requested
|
# Test invalid Plex server requested
|
||||||
with patch.object(
|
with patch.object(
|
||||||
hass.components.sonos,
|
hass.components.sonos,
|
||||||
"get_coordinator_id",
|
"get_coordinator_name",
|
||||||
return_value="media_player.sonos_kitchen",
|
return_value="media_player.sonos_kitchen",
|
||||||
):
|
):
|
||||||
assert await hass.services.async_call(
|
assert await hass.services.async_call(
|
||||||
|
@ -105,10 +105,10 @@ async def test_sonos_playback(hass):
|
||||||
|
|
||||||
# Test no speakers available
|
# Test no speakers available
|
||||||
with patch.object(
|
with patch.object(
|
||||||
loaded_server.account, "sonos_speaker_by_id", return_value=None
|
loaded_server.account, "sonos_speaker", return_value=None
|
||||||
), patch.object(
|
), patch.object(
|
||||||
hass.components.sonos,
|
hass.components.sonos,
|
||||||
"get_coordinator_id",
|
"get_coordinator_name",
|
||||||
return_value="media_player.sonos_kitchen",
|
return_value="media_player.sonos_kitchen",
|
||||||
):
|
):
|
||||||
assert await hass.services.async_call(
|
assert await hass.services.async_call(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue