Discover controllable Plex clients using plex.tv (#36857)
This commit is contained in:
1 parent
94c8d74a66
commit
d5cc3208af
7 files changed
+193
-34
No files matched your search
@@ -38,28 +38,37 @@ class MockGDM:
|
||||
class MockResource:
|
||||
"""Mock a PlexAccount resource."""
|
||||
|
||||
def __init__(self, index):
|
||||
def __init__(self, index, kind="server"):
|
||||
"""Initialize the object."""
|
||||
self.name = MOCK_SERVERS[index][CONF_SERVER]
|
||||
self.clientIdentifier = MOCK_SERVERS[index][ # pylint: disable=invalid-name
|
||||
CONF_SERVER_IDENTIFIER
|
||||
]
|
||||
self.provides = ["server"]
|
||||
self._mock_plex_server = MockPlexServer(index)
|
||||
if kind == "server":
|
||||
self.name = MOCK_SERVERS[index][CONF_SERVER]
|
||||
self.clientIdentifier = MOCK_SERVERS[index][ # pylint: disable=invalid-name
|
||||
CONF_SERVER_IDENTIFIER
|
||||
]
|
||||
self.provides = ["server"]
|
||||
self.device = MockPlexServer(index)
|
||||
else:
|
||||
self.name = f"plex.tv Resource Player {index+10}"
|
||||
self.clientIdentifier = f"client-{index+10}"
|
||||
self.provides = ["player"]
|
||||
self.device = MockPlexClient(f"http://192.168.0.1{index}:32500", index + 10)
|
||||
self.presence = index == 0
|
||||
|
||||
def connect(self, timeout):
|
||||
"""Mock the resource connect method."""
|
||||
return self._mock_plex_server
|
||||
return self.device
|
||||
|
||||
|
||||
class MockPlexAccount:
|
||||
"""Mock a PlexAccount instance."""
|
||||
|
||||
def __init__(self, servers=1):
|
||||
def __init__(self, servers=1, players=3):
|
||||
"""Initialize the object."""
|
||||
self._resources = []
|
||||
for index in range(servers):
|
||||
self._resources.append(MockResource(index))
|
||||
for index in range(players):
|
||||
self._resources.append(MockResource(index, kind="player"))
|
||||
|
||||
def resource(self, name):
|
||||
"""Mock the PlexAccount resource lookup method."""
|
||||
|
||||
@@ -479,8 +479,9 @@ async def test_option_flow_new_users_available(hass, caplog):
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()):
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
monitored_users = hass.data[DOMAIN][SERVERS][server_id].option_monitored_users
|
||||
|
||||
|
||||
@@ -254,8 +254,11 @@ async def test_setup_with_photo_session(hass):
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
|
||||
async_dispatcher_send(hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()):
|
||||
async_dispatcher_send(
|
||||
hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
media_player = hass.states.get("media_player.plex_product_title")
|
||||
assert media_player.state == "idle"
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
"""Tests for Plex media_players."""
|
||||
from plexapi.exceptions import NotFound
|
||||
|
||||
from homeassistant.components.plex.const import DOMAIN, SERVERS
|
||||
|
||||
from .const import DEFAULT_DATA, DEFAULT_OPTIONS
|
||||
from .mock_classes import MockPlexAccount, MockPlexServer
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_plex_tv_clients(hass):
|
||||
"""Test getting Plex clients from plex.tv."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=DEFAULT_DATA,
|
||||
options=DEFAULT_OPTIONS,
|
||||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
|
||||
mock_plex_server = MockPlexServer(config_entry=entry)
|
||||
mock_plex_account = MockPlexAccount()
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket.listen"
|
||||
):
|
||||
entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
plex_server = hass.data[DOMAIN][SERVERS][server_id]
|
||||
|
||||
resource = next(
|
||||
x
|
||||
for x in mock_plex_account.resources()
|
||||
if x.name.startswith("plex.tv Resource Player")
|
||||
)
|
||||
with patch(
|
||||
"plexapi.myplex.MyPlexAccount", return_value=mock_plex_account
|
||||
), patch.object(resource, "connect", side_effect=NotFound):
|
||||
await plex_server._async_update_platforms()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
media_players_before = len(hass.states.async_entity_ids("media_player"))
|
||||
|
||||
# Ensure one more client is discovered
|
||||
await hass.config_entries.async_unload(entry.entry_id)
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket.listen"
|
||||
):
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
plex_server = hass.data[DOMAIN][SERVERS][server_id]
|
||||
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=mock_plex_account):
|
||||
await plex_server._async_update_platforms()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
media_players_after = len(hass.states.async_entity_ids("media_player"))
|
||||
assert media_players_after == media_players_before + 1
|
||||
|
||||
# Ensure only plex.tv resource client is found
|
||||
await hass.config_entries.async_unload(entry.entry_id)
|
||||
|
||||
mock_plex_server.clear_clients()
|
||||
mock_plex_server.clear_sessions()
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket.listen"
|
||||
):
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
plex_server = hass.data[DOMAIN][SERVERS][server_id]
|
||||
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=mock_plex_account):
|
||||
await plex_server._async_update_platforms()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids("media_player")) == 1
|
||||
|
||||
# Ensure cache gets called
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=mock_plex_account):
|
||||
await plex_server._async_update_platforms()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids("media_player")) == 1
|
||||
@@ -26,6 +26,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
|
||||
from .const import DEFAULT_DATA, DEFAULT_OPTIONS
|
||||
from .mock_classes import (
|
||||
MockPlexAccount,
|
||||
MockPlexArtist,
|
||||
MockPlexLibrary,
|
||||
MockPlexLibrarySection,
|
||||
@@ -62,8 +63,9 @@ async def test_new_users_available(hass):
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()):
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
monitored_users = hass.data[DOMAIN][SERVERS][server_id].option_monitored_users
|
||||
|
||||
@@ -101,8 +103,9 @@ async def test_new_ignored_users_available(hass, caplog):
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()):
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
monitored_users = hass.data[DOMAIN][SERVERS][server_id].option_monitored_users
|
||||
|
||||
@@ -253,8 +256,9 @@ async def test_ignore_plex_web_client(hass):
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount(players=0)):
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
sensor = hass.states.get("sensor.plex_plex_server_1")
|
||||
assert sensor.state == str(len(mock_plex_server.accounts))
|
||||
@@ -287,8 +291,9 @@ async def test_media_lookups(hass):
|
||||
loaded_server = hass.data[DOMAIN][SERVERS][server_id]
|
||||
|
||||
# Plex Key searches
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()):
|
||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
media_player_id = hass.states.async_entity_ids("media_player")[0]
|
||||
with patch("homeassistant.components.plex.PlexServer.create_playqueue"):
|
||||
assert await hass.services.async_call(
|
||||
|
||||
Reference in new issue
Block a user