Discover Plex clients using GDM (#39053)
This commit is contained in:
1 parent
72759d7501
commit
c63c253b7f
9 files changed
+207
-39
No files matched your search
@@ -4,7 +4,7 @@ import pytest
|
||||
from homeassistant.components.plex.const import DOMAIN
|
||||
|
||||
from .const import DEFAULT_DATA, DEFAULT_OPTIONS
|
||||
from .mock_classes import MockPlexAccount, MockPlexServer
|
||||
from .mock_classes import MockGDM, MockPlexAccount, MockPlexServer
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
@@ -43,8 +43,12 @@ def setup_plex_server(hass, entry, mock_plex_account, mock_websocket):
|
||||
async def _wrapper(**kwargs):
|
||||
"""Wrap the fixture to allow passing arguments to the MockPlexServer instance."""
|
||||
config_entry = kwargs.get("config_entry", entry)
|
||||
disable_gdm = kwargs.pop("disable_gdm", True)
|
||||
plex_server = MockPlexServer(**kwargs)
|
||||
with patch("plexapi.server.PlexServer", return_value=plex_server):
|
||||
with patch("plexapi.server.PlexServer", return_value=plex_server), patch(
|
||||
"homeassistant.components.plex.GDM",
|
||||
return_value=MockGDM(disabled=disable_gdm),
|
||||
):
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -13,7 +13,7 @@ from homeassistant.const import CONF_URL
|
||||
|
||||
from .const import DEFAULT_DATA, MOCK_SERVERS, MOCK_USERS
|
||||
|
||||
GDM_PAYLOAD = [
|
||||
GDM_SERVER_PAYLOAD = [
|
||||
{
|
||||
"data": {
|
||||
"Content-Type": "plex/media-server",
|
||||
@@ -27,17 +27,73 @@ GDM_PAYLOAD = [
|
||||
}
|
||||
]
|
||||
|
||||
GDM_CLIENT_PAYLOAD = [
|
||||
{
|
||||
"data": {
|
||||
"Content-Type": "plex/media-player",
|
||||
"Device-Class": "stb",
|
||||
"Name": "plexamp",
|
||||
"Port": "36000",
|
||||
"Product": "Plexamp",
|
||||
"Protocol": "plex",
|
||||
"Protocol-Capabilities": "timeline,playback,playqueues,playqueues-creation",
|
||||
"Protocol-Version": "1",
|
||||
"Resource-Identifier": "client-2",
|
||||
"Version": "1.1.0",
|
||||
},
|
||||
"from": ("1.2.3.10", 32412),
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"Content-Type": "plex/media-player",
|
||||
"Device-Class": "pc",
|
||||
"Name": "Chrome",
|
||||
"Port": "32400",
|
||||
"Product": "Plex Web",
|
||||
"Protocol": "plex",
|
||||
"Protocol-Capabilities": "timeline,playback,navigation,mirror,playqueues",
|
||||
"Protocol-Version": "3",
|
||||
"Resource-Identifier": "client-1",
|
||||
"Version": "4.40.1",
|
||||
},
|
||||
"from": ("1.2.3.4", 32412),
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"Content-Type": "plex/media-player",
|
||||
"Device-Class": "mobile",
|
||||
"Name": "SHIELD Android TV",
|
||||
"Port": "32500",
|
||||
"Product": "Plex for Android (TV)",
|
||||
"Protocol": "plex",
|
||||
"Protocol-Capabilities": "timeline,playback,navigation,mirror,playqueues,provider-playback",
|
||||
"Protocol-Version": "1",
|
||||
"Resource-Identifier": "client-999",
|
||||
"Updated-At": "1597686153",
|
||||
"Version": "8.5.0.19697",
|
||||
},
|
||||
"from": ("1.2.3.11", 32412),
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
class MockGDM:
|
||||
"""Mock a GDM instance."""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, disabled=False):
|
||||
"""Initialize the object."""
|
||||
self.entries = GDM_PAYLOAD
|
||||
self.entries = []
|
||||
self.disabled = disabled
|
||||
|
||||
def scan(self):
|
||||
def scan(self, scan_for_clients=False):
|
||||
"""Mock the scan call."""
|
||||
pass
|
||||
if self.disabled:
|
||||
return
|
||||
|
||||
if scan_for_clients:
|
||||
self.entries = GDM_CLIENT_PAYLOAD
|
||||
else:
|
||||
self.entries = GDM_SERVER_PAYLOAD
|
||||
|
||||
|
||||
class MockResource:
|
||||
@@ -56,7 +112,9 @@ class MockResource:
|
||||
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.device = MockPlexClient(
|
||||
baseurl=f"http://192.168.0.1{index}:32500", index=index + 10
|
||||
)
|
||||
self.presence = index == 0
|
||||
self.publicAddressMatches = True
|
||||
|
||||
@@ -122,6 +180,7 @@ class MockPlexServer:
|
||||
self._systemAccounts = list(map(MockPlexSystemAccount, range(num_users)))
|
||||
|
||||
self._clients = []
|
||||
self._session = None
|
||||
self._sessions = []
|
||||
self.set_clients(num_users)
|
||||
self.set_sessions(num_users, session_type)
|
||||
@@ -130,7 +189,9 @@ class MockPlexServer:
|
||||
|
||||
def set_clients(self, num_clients):
|
||||
"""Set up mock PlexClients for this PlexServer."""
|
||||
self._clients = [MockPlexClient(self._baseurl, x) for x in range(num_clients)]
|
||||
self._clients = [
|
||||
MockPlexClient(baseurl=self._baseurl, index=x) for x in range(num_clients)
|
||||
]
|
||||
|
||||
def set_sessions(self, num_sessions, session_type):
|
||||
"""Set up mock PlexSessions for this PlexServer."""
|
||||
@@ -151,6 +212,10 @@ class MockPlexServer:
|
||||
"""Mock the clients method."""
|
||||
return self._clients
|
||||
|
||||
def createToken(self):
|
||||
"""Mock the createToken method."""
|
||||
return "temporary_token"
|
||||
|
||||
def sessions(self):
|
||||
"""Mock the sessions method."""
|
||||
return self._sessions
|
||||
@@ -204,10 +269,10 @@ class MockPlexServer:
|
||||
class MockPlexClient:
|
||||
"""Mock a PlexClient instance."""
|
||||
|
||||
def __init__(self, url, index=0):
|
||||
def __init__(self, server=None, baseurl=None, token=None, index=0):
|
||||
"""Initialize the object."""
|
||||
self.machineIdentifier = f"client-{index+1}"
|
||||
self._baseurl = url
|
||||
self._baseurl = baseurl
|
||||
self._index = index
|
||||
|
||||
def url(self, key):
|
||||
|
||||
@@ -37,7 +37,13 @@ from homeassistant.const import (
|
||||
|
||||
from .const import DEFAULT_OPTIONS, MOCK_SERVERS, MOCK_TOKEN
|
||||
from .helpers import trigger_plex_update
|
||||
from .mock_classes import MockGDM, MockPlexAccount, MockPlexServer, MockResource
|
||||
from .mock_classes import (
|
||||
MockGDM,
|
||||
MockPlexAccount,
|
||||
MockPlexClient,
|
||||
MockPlexServer,
|
||||
MockResource,
|
||||
)
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
@@ -434,10 +440,11 @@ async def test_option_flow_new_users_available(
|
||||
OPTIONS_OWNER_ONLY[MP_DOMAIN][CONF_MONITORED_USERS] = {"Owner": {"enabled": True}}
|
||||
entry.options = OPTIONS_OWNER_ONLY
|
||||
|
||||
mock_plex_server = await setup_plex_server(config_entry=entry)
|
||||
mock_plex_server = await setup_plex_server(config_entry=entry, disable_gdm=False)
|
||||
|
||||
trigger_plex_update(mock_websocket)
|
||||
await hass.async_block_till_done()
|
||||
with patch("homeassistant.components.plex.server.PlexClient", new=MockPlexClient):
|
||||
trigger_plex_update(mock_websocket)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
monitored_users = hass.data[DOMAIN][SERVERS][server_id].option_monitored_users
|
||||
@@ -640,7 +647,11 @@ async def test_manual_config(hass):
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
||||
), patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()):
|
||||
), patch(
|
||||
"homeassistant.components.plex.GDM", return_value=MockGDM(disabled=True)
|
||||
), patch(
|
||||
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=MANUAL_SERVER
|
||||
)
|
||||
@@ -674,7 +685,11 @@ async def test_manual_config_with_token(hass):
|
||||
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()), patch(
|
||||
"plexapi.server.PlexServer", return_value=mock_plex_server
|
||||
), patch("homeassistant.components.plex.PlexWebsocket", autospec=True):
|
||||
), patch(
|
||||
"homeassistant.components.plex.GDM", return_value=MockGDM(disabled=True)
|
||||
), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_TOKEN: MOCK_TOKEN}
|
||||
)
|
||||
|
||||
@@ -18,7 +18,7 @@ import homeassistant.util.dt as dt_util
|
||||
|
||||
from .const import DEFAULT_DATA, DEFAULT_OPTIONS
|
||||
from .helpers import trigger_plex_update
|
||||
from .mock_classes import MockPlexAccount, MockPlexServer
|
||||
from .mock_classes import MockGDM, MockPlexAccount, MockPlexServer
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
@@ -183,6 +183,8 @@ async def test_bad_token_with_tokenless_server(hass, entry):
|
||||
"""Test setup with a bad token and a server with token auth disabled."""
|
||||
with patch("plexapi.server.PlexServer", return_value=MockPlexServer()), patch(
|
||||
"plexapi.myplex.MyPlexAccount", side_effect=plexapi.exceptions.Unauthorized
|
||||
), patch(
|
||||
"homeassistant.components.plex.GDM", return_value=MockGDM(disabled=True)
|
||||
), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
||||
) as mock_websocket:
|
||||
|
||||
@@ -26,7 +26,7 @@ async def test_plex_tv_clients(hass, entry, mock_plex_account, setup_plex_server
|
||||
# Ensure one more client is discovered
|
||||
await hass.config_entries.async_unload(entry.entry_id)
|
||||
|
||||
mock_plex_server = await setup_plex_server(config_entry=entry)
|
||||
mock_plex_server = await setup_plex_server()
|
||||
plex_server = hass.data[DOMAIN][SERVERS][server_id]
|
||||
|
||||
await plex_server._async_update_platforms()
|
||||
@@ -38,7 +38,7 @@ async def test_plex_tv_clients(hass, entry, mock_plex_account, setup_plex_server
|
||||
# Ensure only plex.tv resource client is found
|
||||
await hass.config_entries.async_unload(entry.entry_id)
|
||||
|
||||
mock_plex_server = await setup_plex_server(config_entry=entry)
|
||||
mock_plex_server = await setup_plex_server()
|
||||
mock_plex_server.clear_clients()
|
||||
mock_plex_server.clear_sessions()
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import copy
|
||||
|
||||
from plexapi.exceptions import BadRequest, NotFound
|
||||
from requests.exceptions import RequestException
|
||||
from requests.exceptions import ConnectionError, RequestException
|
||||
|
||||
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
||||
from homeassistant.components.media_player.const import (
|
||||
@@ -28,6 +28,7 @@ from homeassistant.const import ATTR_ENTITY_ID
|
||||
from .const import DEFAULT_DATA, DEFAULT_OPTIONS
|
||||
from .helpers import trigger_plex_update
|
||||
from .mock_classes import (
|
||||
MockGDM,
|
||||
MockPlexAccount,
|
||||
MockPlexAlbum,
|
||||
MockPlexArtist,
|
||||
@@ -125,6 +126,24 @@ async def test_network_error_during_refresh(
|
||||
)
|
||||
|
||||
|
||||
async def test_gdm_client_failure(hass, mock_websocket, setup_plex_server):
|
||||
"""Test connection failure to a GDM discovered client."""
|
||||
mock_plex_server = await setup_plex_server(disable_gdm=False)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.plex.server.PlexClient", side_effect=ConnectionError
|
||||
):
|
||||
trigger_plex_update(mock_websocket)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
sensor = hass.states.get("sensor.plex_plex_server_1")
|
||||
assert sensor.state == str(len(mock_plex_server.accounts))
|
||||
|
||||
with patch.object(mock_plex_server, "clients", side_effect=RequestException):
|
||||
trigger_plex_update(mock_websocket)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_mark_sessions_idle(hass, mock_plex_server, mock_websocket):
|
||||
"""Test marking media_players as idle when sessions end."""
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
@@ -156,7 +175,7 @@ async def test_ignore_plex_web_client(hass, entry, mock_websocket):
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount(players=0)
|
||||
):
|
||||
), patch("homeassistant.components.plex.GDM", return_value=MockGDM(disabled=True)):
|
||||
entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
Reference in new issue
Block a user