diff --git a/src/linkplay/bridge.py b/src/linkplay/bridge.py index 01c25b0..e55c3b5 100644 --- a/src/linkplay/bridge.py +++ b/src/linkplay/bridge.py @@ -26,12 +26,15 @@ class LinkPlayDevice: """Represents a LinkPlay device.""" bridge: LinkPlayBridge - properties: dict[DeviceAttribute, str] = dict.fromkeys( - DeviceAttribute.__members__.values(), "" - ) + properties: dict[DeviceAttribute, str] def __init__(self, bridge: LinkPlayBridge): self.bridge = bridge + self.properties = dict.fromkeys(DeviceAttribute.__members__.values(), "") + + def to_dict(self): + """Return the state of the LinkPlayDevice.""" + return {"properties": self.properties} async def update_status(self) -> None: """Update the device status.""" @@ -74,12 +77,15 @@ class LinkPlayPlayer: """Represents a LinkPlay player.""" bridge: LinkPlayBridge - properties: dict[PlayerAttribute, str] = dict.fromkeys( - PlayerAttribute.__members__.values(), "" - ) + properties: dict[PlayerAttribute, str] def __init__(self, bridge: LinkPlayBridge): self.bridge = bridge + self.properties = dict.fromkeys(PlayerAttribute.__members__.values(), "") + + def to_dict(self): + """Return the state of the LinkPlayPlayer.""" + return {"properties": self.properties} async def update_status(self) -> None: """Update the player status.""" @@ -256,6 +262,14 @@ def __str__(self) -> str: return self.device.name + def to_dict(self): + """Return the state of the LinkPlayBridge.""" + return { + "endpoint": self.endpoint.to_dict(), + "device": self.device.to_dict(), + "player": self.player.to_dict(), + } + async def json_request(self, command: str) -> dict[str, str]: """Performs a GET request on the given command and returns the result as a JSON object.""" return await self.endpoint.json_request(command) diff --git a/src/linkplay/consts.py b/src/linkplay/consts.py index 7468ca0..2bb793b 100644 --- a/src/linkplay/consts.py +++ b/src/linkplay/consts.py @@ -304,6 +304,12 @@ class PlayerAttribute(StrEnum): VOLUME = "vol" MUTED = "mute" + def __str__(self): + return self.value + + def __repr__(self): + return self.value + class DeviceAttribute(StrEnum): """Defines the device attributes.""" @@ -412,6 +418,12 @@ class DeviceAttribute(StrEnum): POWER_MODE = "power_mode" SECURITY_CAPABILITIES = "security_capabilities" + def __str__(self): + return self.value + + def __repr__(self): + return self.value + class MultiroomAttribute(StrEnum): """Defines the player attributes.""" @@ -420,3 +432,9 @@ class MultiroomAttribute(StrEnum): FOLLOWER_LIST = "slave_list" UUID = "uuid" IP = "ip" + + def __str__(self): + return self.value + + def __repr__(self): + return self.value diff --git a/src/linkplay/endpoint.py b/src/linkplay/endpoint.py index e03c22b..dff4858 100644 --- a/src/linkplay/endpoint.py +++ b/src/linkplay/endpoint.py @@ -3,7 +3,6 @@ from aiohttp import ClientSession -from linkplay.consts import TCPPORT from linkplay.utils import ( call_tcpuart, call_tcpuart_json, @@ -23,6 +22,10 @@ async def request(self, command: str) -> None: async def json_request(self, command: str) -> dict[str, str]: """Performs a request on the given command and returns the result as a JSON object.""" + @abstractmethod + def to_dict(self) -> dict[str, str]: + """Return the state of the LinkPlayEndpoint""" + class LinkPlayApiEndpoint(LinkPlayEndpoint): """Represents a LinkPlay HTTP API endpoint.""" @@ -35,6 +38,10 @@ def __init__(self, *, protocol: str, endpoint: str, session: ClientSession): self._endpoint: str = f"{protocol}://{endpoint}" self._session: ClientSession = session + def to_dict(self): + """Return the state of the LinkPlayEndpoint""" + return {"endpoint": self._endpoint} + async def request(self, command: str) -> None: """Performs a GET request on the given command and verifies the result.""" await session_call_api_ok(self._endpoint, self._session, command) @@ -55,6 +62,10 @@ def __init__( ): self._connection = connection + def to_dict(self): + """Return the state of the LinkPlayEndpoint""" + return {} + async def request(self, command: str) -> None: reader, writer = self._connection await call_tcpuart(reader, writer, command) diff --git a/tests/linkplay/test_bridge.py b/tests/linkplay/test_bridge.py index 0af91b8..ff364d7 100644 --- a/tests/linkplay/test_bridge.py +++ b/tests/linkplay/test_bridge.py @@ -4,7 +4,6 @@ from unittest.mock import AsyncMock, patch import pytest - from linkplay.bridge import ( LinkPlayBridge, LinkPlayDevice, @@ -59,6 +58,14 @@ async def test_device_reboot(): bridge.request.assert_called_once_with(LinkPlayCommand.REBOOT) +async def test_deserialize_device(): + """Test the device to deserialize correctly.""" + bridge = AsyncMock() + device = LinkPlayDevice(bridge) + + device.to_dict() + + async def test_player_update_status(): """Tests if the player update_status is correctly called.""" bridge = AsyncMock() @@ -262,6 +269,14 @@ async def test_player_play_preset_raises_value_error(preset_number: int): await player.play_preset(preset_number) +async def test_deserialize_player(): + """Test the player to deserialize correctly.""" + bridge = AsyncMock() + player = LinkPlayPlayer(bridge) + + player.to_dict() + + async def test_multiroom_setup(): """Tests if multiroom sets up correctly.""" leader = AsyncMock()