diff --git a/src/linkplay/discovery.py b/src/linkplay/discovery.py index 8c878a5..e17d3a8 100644 --- a/src/linkplay/discovery.py +++ b/src/linkplay/discovery.py @@ -21,7 +21,7 @@ async def linkplay_factory_bridge( """Attempts to create a LinkPlayBridge from the given IP address. Returns None if the device is not an expected LinkPlay device.""" endpoint: LinkPlayApiEndpoint = LinkPlayApiEndpoint( - protocol="http", endpoint=ip_address, session=session + protocol="http", port=80, endpoint=ip_address, session=session ) try: return await linkplay_factory_bridge_endpoint(endpoint) @@ -48,16 +48,22 @@ async def linkplay_factory_httpapi_bridge( Attempts to use HTTPS first, then falls back to HTTP. Raises LinkPlayRequestException if the device is not an expected LinkPlay device.""" - https_endpoint: LinkPlayApiEndpoint = LinkPlayApiEndpoint( - protocol="https", endpoint=ip_address, session=session - ) - try: - return await linkplay_factory_bridge_endpoint(https_endpoint) - except LinkPlayRequestException: - http_endpoint: LinkPlayApiEndpoint = LinkPlayApiEndpoint( - protocol="http", endpoint=ip_address, session=session + protocol_port_pairs = [("https", 443), ("https", 4443)] + + for protocol, port in protocol_port_pairs: + endpoint: LinkPlayApiEndpoint = LinkPlayApiEndpoint( + protocol=protocol, port=port, endpoint=ip_address, session=session ) - return await linkplay_factory_bridge_endpoint(http_endpoint) + + try: + return await linkplay_factory_bridge_endpoint(endpoint) + except LinkPlayRequestException: + pass + + http_endpoint: LinkPlayApiEndpoint = LinkPlayApiEndpoint( + protocol="http", port=80, endpoint=ip_address, session=session + ) + return await linkplay_factory_bridge_endpoint(http_endpoint) async def discover_linkplay_bridges( diff --git a/src/linkplay/endpoint.py b/src/linkplay/endpoint.py index dff4858..1c06d49 100644 --- a/src/linkplay/endpoint.py +++ b/src/linkplay/endpoint.py @@ -30,12 +30,14 @@ def to_dict(self) -> dict[str, str]: class LinkPlayApiEndpoint(LinkPlayEndpoint): """Represents a LinkPlay HTTP API endpoint.""" - def __init__(self, *, protocol: str, endpoint: str, session: ClientSession): + def __init__( + self, *, protocol: str, port: int, endpoint: str, session: ClientSession + ): assert protocol in [ "http", "https", ], "Protocol must be either 'http' or 'https'" - self._endpoint: str = f"{protocol}://{endpoint}" + self._endpoint: str = f"{protocol}://{endpoint}:{port}" self._session: ClientSession = session def to_dict(self): diff --git a/tests/linkplay/test_bridge.py b/tests/linkplay/test_bridge.py index 0b88985..df78fbd 100644 --- a/tests/linkplay/test_bridge.py +++ b/tests/linkplay/test_bridge.py @@ -4,6 +4,7 @@ from unittest.mock import AsyncMock, patch import pytest + from linkplay.bridge import ( LinkPlayBridge, LinkPlayDevice, @@ -27,10 +28,10 @@ def test_device_name(): """Tests if the device name is correctly set up.""" endpoint: LinkPlayApiEndpoint = LinkPlayApiEndpoint( - protocol="http", endpoint="1.2.3.4", session=None + protocol="http", port=80, endpoint="1.2.3.4", session=None ) bridge: LinkPlayBridge = LinkPlayBridge(endpoint=endpoint) - assert f"{bridge}" == "http://1.2.3.4" + assert f"{bridge}" == "http://1.2.3.4:80" bridge.device.properties[DeviceAttribute.DEVICE_NAME] = "TestDevice" assert f"{bridge}" == "TestDevice" diff --git a/tests/linkplay/test_endpoint.py b/tests/linkplay/test_endpoint.py index bba7e29..02ec823 100644 --- a/tests/linkplay/test_endpoint.py +++ b/tests/linkplay/test_endpoint.py @@ -10,4 +10,4 @@ def test_api_endpoint_protocol_raises_assertion_error() -> None: with an invalid protocol raises an AssertionError.""" with pytest.raises(AssertionError): - LinkPlayApiEndpoint(protocol="ftp", endpoint="1.2.3.4", session=None) + LinkPlayApiEndpoint(protocol="ftp", port=21, endpoint="1.2.3.4", session=None)