core/tests/components/plex/mock_classes.py

76 lines
2.2 KiB
Python
Raw Normal View History

"""Mock classes used in tests."""
2019-10-14 04:59:25 +00:00
from homeassistant.components.plex.const import CONF_SERVER, CONF_SERVER_IDENTIFIER
from homeassistant.const import CONF_HOST, CONF_PORT
2019-10-14 04:59:25 +00:00
MOCK_SERVERS = [
{
CONF_HOST: "1.2.3.4",
CONF_PORT: 32400,
CONF_SERVER: "Plex Server 1",
CONF_SERVER_IDENTIFIER: "unique_id_123",
},
{
CONF_HOST: "4.3.2.1",
CONF_PORT: 32400,
CONF_SERVER: "Plex Server 2",
CONF_SERVER_IDENTIFIER: "unique_id_456",
},
]
2019-10-14 04:59:25 +00:00
class MockResource:
"""Mock a PlexAccount resource."""
2019-10-14 04:59:25 +00:00
def __init__(self, index):
"""Initialize the object."""
2019-10-14 04:59:25 +00:00
self.name = MOCK_SERVERS[index][CONF_SERVER]
self.clientIdentifier = MOCK_SERVERS[index][ # pylint: disable=invalid-name
CONF_SERVER_IDENTIFIER
]
self.provides = ["server"]
2019-10-14 04:59:25 +00:00
self._mock_plex_server = MockPlexServer(index)
def connect(self, timeout):
2019-10-14 04:59:25 +00:00
"""Mock the resource connect method."""
return self._mock_plex_server
2019-10-14 04:59:25 +00:00
class MockPlexAccount:
"""Mock a PlexAccount instance."""
def __init__(self, servers=1):
"""Initialize the object."""
self._resources = []
for index in range(servers):
self._resources.append(MockResource(index))
def resource(self, name):
"""Mock the PlexAccount resource lookup method."""
return [x for x in self._resources if x.name == name][0]
def resources(self):
"""Mock the PlexAccount resources listing method."""
return self._resources
2019-10-14 04:59:25 +00:00
class MockPlexServer:
"""Mock a PlexServer instance."""
2019-10-14 04:59:25 +00:00
def __init__(self, index=0, ssl=True):
"""Initialize the object."""
2019-10-14 04:59:25 +00:00
host = MOCK_SERVERS[index][CONF_HOST]
port = MOCK_SERVERS[index][CONF_PORT]
self.friendlyName = MOCK_SERVERS[index][ # pylint: disable=invalid-name
CONF_SERVER
]
self.machineIdentifier = MOCK_SERVERS[index][ # pylint: disable=invalid-name
CONF_SERVER_IDENTIFIER
]
prefix = "https" if ssl else "http"
self._baseurl = f"{prefix}://{host}:{port}"
@property
def url_in_use(self):
"""Return URL used by PlexServer."""
return self._baseurl