Skip to content

Commit

Permalink
Support devices running https on port 4443 (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
anderscarling authored Dec 2, 2024
1 parent 6e5182b commit 5ce4275
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
26 changes: 16 additions & 10 deletions src/linkplay/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions src/linkplay/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions tests/linkplay/test_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import AsyncMock, patch

import pytest

from linkplay.bridge import (
LinkPlayBridge,
LinkPlayDevice,
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/linkplay/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5ce4275

Please sign in to comment.