Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization of some classes #46

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/linkplay/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions src/linkplay/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand All @@ -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
13 changes: 12 additions & 1 deletion src/linkplay/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from aiohttp import ClientSession

from linkplay.consts import TCPPORT
from linkplay.utils import (
call_tcpuart,
call_tcpuart_json,
Expand All @@ -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."""
Expand All @@ -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)
Expand All @@ -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)
Expand Down
17 changes: 16 additions & 1 deletion tests/linkplay/test_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from unittest.mock import AsyncMock, patch

import pytest

from linkplay.bridge import (
LinkPlayBridge,
LinkPlayDevice,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down