From 96096bde9dd037378e3519237d92e4ead0912955 Mon Sep 17 00:00:00 2001 From: noahhusby <32528627+noahhusby@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:57:40 -0400 Subject: [PATCH] fix: response & add postion_last_updated --- aiostreammagic/stream_magic.py | 30 +++++++++++++----------------- pyproject.toml | 2 +- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/aiostreammagic/stream_magic.py b/aiostreammagic/stream_magic.py index 4961850..ebe32d0 100644 --- a/aiostreammagic/stream_magic.py +++ b/aiostreammagic/stream_magic.py @@ -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 @@ -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.""" @@ -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 @@ -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.""" @@ -259,6 +253,7 @@ 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: @@ -266,6 +261,7 @@ async def _async_handle_position(self, payload) -> None: 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: diff --git a/pyproject.toml b/pyproject.toml index d3c0077..6a711d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 <32528627+noahhusby@users.noreply.github.com>"] maintainers = ["Noah Husby <32528627+noahhusby@users.noreply.github.com>"]