Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
silamon committed Sep 24, 2024
1 parent 0f0d6de commit a5988ee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
19 changes: 16 additions & 3 deletions src/linkplay/bridge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Any

from linkplay.consts import (
Expand All @@ -23,7 +22,6 @@
from linkplay.utils import fixup_player_properties


@dataclass
class LinkPlayDevice:
"""Represents a LinkPlay device."""

Expand All @@ -34,6 +32,10 @@ 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."""
self.properties = await self.bridge.json_request(LinkPlayCommand.DEVICE_STATUS) # type: ignore[assignment]
Expand Down Expand Up @@ -71,7 +73,6 @@ def eth(self) -> str:
)


@dataclass
class LinkPlayPlayer:
"""Represents a LinkPlay player."""

Expand All @@ -82,6 +83,10 @@ 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."""
properties: dict[PlayerAttribute, str] = await self.bridge.json_request(
Expand Down Expand Up @@ -257,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
16 changes: 12 additions & 4 deletions src/linkplay/endpoint.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import asyncio
from abc import ABC, abstractmethod
from dataclasses import dataclass

from aiohttp import ClientSession

from linkplay.consts import TCPPORT
from linkplay.utils import (
call_tcpuart,
call_tcpuart_json,
Expand All @@ -13,7 +11,6 @@
)


@dataclass
class LinkPlayEndpoint(ABC):
"""Represents an abstract LinkPlay endpoint."""

Expand All @@ -25,8 +22,11 @@ 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"""


@dataclass
class LinkPlayApiEndpoint(LinkPlayEndpoint):
"""Represents a LinkPlay HTTP API endpoint."""

Expand All @@ -38,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 @@ -58,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

0 comments on commit a5988ee

Please sign in to comment.