Skip to content

Commit

Permalink
fix: response & add postion_last_updated
Browse files Browse the repository at this point in the history
  • Loading branch information
noahhusby committed Sep 9, 2024
1 parent 0b7f5bf commit 96096bd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
30 changes: 13 additions & 17 deletions aiostreammagic/stream_magic.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
"""Asynchronous Python client for StreamMagic API."""
import asyncio
import json
import socket
from asyncio import AbstractEventLoop, Future, Task
from collections import defaultdict
from dataclasses import dataclass
from datetime import datetime
from typing import Any

import websockets
from aiohttp import ClientSession, ClientError, ClientResponseError
from aiohttp.hdrs import METH_GET
from websockets import WebSocketClientProtocol
from yarl import URL

from aiostreammagic.exceptions import StreamMagicError, StreamMagicConnectionError
from aiostreammagic.models import Info, Source, State, PlayState, NowPlaying

from websockets.client import connect as ws_connect

from aiostreammagic.exceptions import StreamMagicError
from aiostreammagic.models import Info, Source, State, PlayState, NowPlaying
from . import endpoints as ep
from .const import _LOGGER

Expand All @@ -42,6 +35,7 @@ def __init__(self, host):
self.state: State | None = None
self.play_state: PlayState | None = None
self.now_playing: NowPlaying | None = None
self.position_last_updated: datetime = datetime.now()

async def register_state_update_callbacks(self, callback: Any):
"""Register state update callback."""
Expand Down Expand Up @@ -195,9 +189,9 @@ async def request(self, path: str, params=None) -> Any:
message = response["message"]
result = response["result"]
if result != 200:
raise StreamMagicError("Error!")
raise StreamMagicError(message)

return response["params"]["data"]
return response

async def subscribe(self, callback: Any, path: str) -> Any:
self._subscriptions[path] = callback
Expand All @@ -210,28 +204,28 @@ async def subscribe(self, callback: Any, path: str) -> Any:
async def get_info(self) -> Info:
"""Get device information from device."""
data = await self.request(ep.INFO)
return Info.from_dict(data)
return Info.from_dict(data["params"]["data"])

async def get_sources(self) -> list[Source]:
"""Get source information from device."""
data = await self.request(ep.SOURCES)
sources = [Source.from_dict(x) for x in data["sources"]]
sources = [Source.from_dict(x) for x in data["params"]["data"]["sources"]]
return sources

async def get_state(self) -> State:
"""Get state information from device."""
data = await self.request(ep.ZONE_STATE)
return State.from_dict(data)
return State.from_dict(data["params"]["data"])

async def get_play_state(self) -> PlayState:
"""Get play state information from device."""
data = await self.request(ep.PLAY_STATE)
return PlayState.from_dict(data)
return PlayState.from_dict(data["params"]["data"])

async def get_now_playing(self) -> NowPlaying:
"""Get now playing information from device."""
data = await self.request(ep.NOW_PLAYING)
return NowPlaying.from_dict(data)
return NowPlaying.from_dict(data["params"]["data"])

async def _async_handle_info(self, payload) -> None:
"""Handle async info update."""
Expand Down Expand Up @@ -259,13 +253,15 @@ async def _async_handle_play_state(self, payload) -> None:
params = payload["params"]
if "data" in params:
self.play_state = PlayState.from_dict(params["data"])
self.position_last_updated = datetime.now()
await self.do_state_update_callbacks()

async def _async_handle_position(self, payload) -> None:
"""Handle async position update."""
params = payload["params"]
if "data" in params and params["data"]["position"] and self.play_state:
self.play_state.position = params["data"]["position"]
self.position_last_updated = datetime.now()
await self.do_state_update_callbacks()

async def _async_handle_now_playing(self, payload) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aiostreammagic"
version = "2.0.0"
version = "2.0.1"
description = "An async python package for interfacing with Cambridge Audio / Stream Magic compatible streamers."
authors = ["Noah Husby <[email protected]>"]
maintainers = ["Noah Husby <[email protected]>"]
Expand Down

0 comments on commit 96096bd

Please sign in to comment.