1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00

Fallback to try all known Plex servers if none marked present (#53643)

This commit is contained in:
jjlawren 2021-08-24 05:05:24 -05:00 committed by GitHub
parent 19d81af4c1
commit d5fe7e0e5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 9 deletions

View File

@ -144,11 +144,11 @@ class PlexServer:
config_entry_update_needed = False
def _connect_with_token():
available_servers = [
(x.name, x.clientIdentifier)
for x in self.account.resources()
if "server" in x.provides and x.presence
all_servers = [
x for x in self.account.resources() if "server" in x.provides
]
servers = [x for x in all_servers if x.presence] or all_servers
available_servers = [(x.name, x.clientIdentifier) for x in servers]
if not available_servers:
raise NoServersFound

View File

@ -239,7 +239,7 @@ def plextv_resources_base_fixture():
@pytest.fixture(name="plextv_resources", scope="session")
def plextv_resources_fixture(plextv_resources_base):
"""Load default payload for plex.tv resources and return it."""
return plextv_resources_base.format(second_server_enabled=0)
return plextv_resources_base.format(first_server_enabled=1, second_server_enabled=0)
@pytest.fixture(name="plextv_shared_users", scope="session")

View File

@ -210,7 +210,9 @@ async def test_multiple_servers_with_selection(
requests_mock.get(
"https://plex.tv/api/resources",
text=plextv_resources_base.format(second_server_enabled=1),
text=plextv_resources_base.format(
first_server_enabled=1, second_server_enabled=1
),
)
with patch("plexauth.PlexAuth.initiate_auth"), patch(
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
@ -248,6 +250,42 @@ async def test_multiple_servers_with_selection(
assert result["data"][PLEX_SERVER_CONFIG][CONF_TOKEN] == MOCK_TOKEN
async def test_only_non_present_servers(
hass,
mock_plex_calls,
requests_mock,
plextv_resources_base,
current_request_with_host,
):
"""Test creating an entry with one server available."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == "form"
assert result["step_id"] == "user"
requests_mock.get(
"https://plex.tv/api/resources",
text=plextv_resources_base.format(
first_server_enabled=0, second_server_enabled=0
),
)
with patch("plexauth.PlexAuth.initiate_auth"), patch(
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
assert result["type"] == "external"
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] == "external_done"
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] == "form"
assert result["step_id"] == "select_server"
async def test_adding_last_unconfigured_server(
hass,
mock_plex_calls,
@ -272,7 +310,9 @@ async def test_adding_last_unconfigured_server(
requests_mock.get(
"https://plex.tv/api/resources",
text=plextv_resources_base.format(second_server_enabled=1),
text=plextv_resources_base.format(
first_server_enabled=1, second_server_enabled=1
),
)
with patch("plexauth.PlexAuth.initiate_auth"), patch(
@ -332,7 +372,9 @@ async def test_all_available_servers_configured(
requests_mock.get("https://plex.tv/users/account", text=plextv_account)
requests_mock.get(
"https://plex.tv/api/resources",
text=plextv_resources_base.format(second_server_enabled=1),
text=plextv_resources_base.format(
first_server_enabled=1, second_server_enabled=1
),
)
with patch("plexauth.PlexAuth.initiate_auth"), patch(

View File

@ -1,5 +1,5 @@
<MediaContainer size="5">
<Device name="Plex Server 1" product="Plex Media Server" productVersion="1.20.4.3517-ab5e1197c" platform="Linux" platformVersion="20.04.1 LTS (Focal Fossa)" device="PC" clientIdentifier="unique_id_123" createdAt="1429510140" lastSeenAt="1605500006" provides="server" owned="1" accessToken="faketoken" publicAddress="10.20.30.40" httpsRequired="0" synced="0" relay="0" dnsRebindingProtection="0" natLoopbackSupported="1" publicAddressMatches="1" presence="1">
<Device name="Plex Server 1" product="Plex Media Server" productVersion="1.20.4.3517-ab5e1197c" platform="Linux" platformVersion="20.04.1 LTS (Focal Fossa)" device="PC" clientIdentifier="unique_id_123" createdAt="1429510140" lastSeenAt="1605500006" provides="server" owned="1" accessToken="faketoken" publicAddress="10.20.30.40" httpsRequired="0" synced="0" relay="0" dnsRebindingProtection="0" natLoopbackSupported="1" publicAddressMatches="1" presence="{first_server_enabled}">
<Connection protocol="https" address="1.2.3.4" port="32400" uri="https://1-2-3-4.123456789001234567890.plex.direct:32400" local="1"/>
</Device>
<Device name="Plex Server 2" product="Plex Media Server" productVersion="1.20.4.3517-ab5e1197c" platform="Linux" platformVersion="20.04.1 LTS (Focal Fossa)" device="PC" clientIdentifier="unique_id_456" createdAt="1429510140" lastSeenAt="1605500006" provides="server" owned="1" accessToken="faketoken" publicAddress="10.20.30.40" httpsRequired="0" synced="0" relay="0" dnsRebindingProtection="0" natLoopbackSupported="1" publicAddressMatches="1" presence="{second_server_enabled}">