mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 09:17:53 +00:00
Use URI provided by Plex for local connections (#27515)
* Use provided URI for local connections * Use provided plexapi connection method * Remove unused mock from tests * Handle potential edge case(s)
This commit is contained in:
parent
564789470e
commit
0888098718
@ -89,9 +89,10 @@ class PlexFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
_LOGGER.error("Invalid credentials provided, config not created")
|
||||
errors["base"] = "faulty_credentials"
|
||||
except (plexapi.exceptions.NotFound, requests.exceptions.ConnectionError):
|
||||
_LOGGER.error(
|
||||
"Plex server could not be reached: %s", server_config[CONF_URL]
|
||||
server_identifier = (
|
||||
server_config.get(CONF_URL) or plex_server.server_choice or "Unknown"
|
||||
)
|
||||
_LOGGER.error("Plex server could not be reached: %s", server_identifier)
|
||||
errors["base"] = "not_found"
|
||||
|
||||
except ServerNotSpecified as available_servers:
|
||||
|
@ -39,11 +39,12 @@ class PlexServer:
|
||||
self._server_name = server_config.get(CONF_SERVER)
|
||||
self._verify_ssl = server_config.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL)
|
||||
self.options = options
|
||||
self.server_choice = None
|
||||
|
||||
def connect(self):
|
||||
"""Connect to a Plex server directly, obtaining direct URL if necessary."""
|
||||
|
||||
def _set_missing_url():
|
||||
def _connect_with_token():
|
||||
account = plexapi.myplex.MyPlexAccount(token=self._token)
|
||||
available_servers = [
|
||||
(x.name, x.clientIdentifier)
|
||||
@ -56,13 +57,10 @@ class PlexServer:
|
||||
if not self._server_name and len(available_servers) > 1:
|
||||
raise ServerNotSpecified(available_servers)
|
||||
|
||||
server_choice = (
|
||||
self.server_choice = (
|
||||
self._server_name if self._server_name else available_servers[0][0]
|
||||
)
|
||||
connections = account.resource(server_choice).connections
|
||||
local_url = [x.httpuri for x in connections if x.local]
|
||||
remote_url = [x.uri for x in connections if not x.local]
|
||||
self._url = local_url[0] if local_url else remote_url[0]
|
||||
self._plex_server = account.resource(self.server_choice).connect()
|
||||
|
||||
def _connect_with_url():
|
||||
session = None
|
||||
@ -73,10 +71,10 @@ class PlexServer:
|
||||
self._url, self._token, session
|
||||
)
|
||||
|
||||
if self._token and not self._url:
|
||||
_set_missing_url()
|
||||
|
||||
_connect_with_url()
|
||||
if self._url:
|
||||
_connect_with_url()
|
||||
else:
|
||||
_connect_with_token()
|
||||
|
||||
def clients(self):
|
||||
"""Pass through clients call to plexapi."""
|
||||
|
@ -29,34 +29,12 @@ class MockResource:
|
||||
]
|
||||
self.provides = ["server"]
|
||||
self._mock_plex_server = MockPlexServer(index)
|
||||
self._connections = []
|
||||
for connection in range(2):
|
||||
self._connections.append(MockConnection(connection))
|
||||
|
||||
@property
|
||||
def connections(self):
|
||||
"""Mock the resource connection listing method."""
|
||||
return self._connections
|
||||
|
||||
def connect(self):
|
||||
"""Mock the resource connect method."""
|
||||
return self._mock_plex_server
|
||||
|
||||
|
||||
class MockConnection: # pylint: disable=too-few-public-methods
|
||||
"""Mock a single account resource connection object."""
|
||||
|
||||
def __init__(self, index, ssl=True):
|
||||
"""Initialize the object."""
|
||||
prefix = "https" if ssl else "http"
|
||||
self.httpuri = (
|
||||
f"http://{MOCK_SERVERS[index][CONF_HOST]}:{MOCK_SERVERS[index][CONF_PORT]}"
|
||||
)
|
||||
self.uri = f"{prefix}://{MOCK_SERVERS[index][CONF_HOST]}:{MOCK_SERVERS[index][CONF_PORT]}"
|
||||
# Only first server is local
|
||||
self.local = not bool(index)
|
||||
|
||||
|
||||
class MockPlexAccount:
|
||||
"""Mock a PlexAccount instance."""
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user