Skip to content

Commit

Permalink
Use new async websockets API
Browse files Browse the repository at this point in the history
  • Loading branch information
noahhusby committed Nov 23, 2024
1 parent 4e9f3af commit 1b7a1e1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions aiostreammagic/stream_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
from datetime import datetime, UTC
from typing import Any, Optional

import websockets
from websockets import WebSocketClientProtocol
from websockets.client import connect as ws_connect
from websockets import ConnectionClosedError, ConnectionClosedOK
from websockets.asyncio.client import connect, ClientConnection

from aiostreammagic.exceptions import StreamMagicError
from aiostreammagic.models import (
Expand Down Expand Up @@ -37,7 +36,7 @@ class StreamMagicClient:

def __init__(self, host: str) -> None:
self.host = host
self.connection: WebSocketClientProtocol | None = None
self.connection: ClientConnection | None = None
self.futures: dict[str, list[Future[Any]]] = {}
self._subscriptions: dict[str, Any] = {}
self._loop: AbstractEventLoop = asyncio.get_running_loop()
Expand Down Expand Up @@ -110,11 +109,14 @@ def is_connected(self) -> bool:
"""Return True if device is connected."""
return self.connect_task is not None and not self.connect_task.done()

async def _ws_connect(self, uri: str) -> WebSocketClientProtocol:
async def _ws_connect(self, uri: str) -> ClientConnection:
"""Establish a connection with a WebSocket."""
return await ws_connect(
return await connect(
uri,
extra_headers={"Origin": f"ws://{self.host}", "Host": f"{self.host}:80"},
additional_headers={
"Origin": f"ws://{self.host}",
"Host": f"{self.host}:80",
},
)

async def _reconnect_handler(self, res: Future[bool]) -> None:
Expand Down Expand Up @@ -210,7 +212,7 @@ async def subscription_handler(queue: Queue, callback) -> None:

async def consumer_handler(
self,
ws: WebSocketClientProtocol,
ws: ClientConnection,
subscriptions: dict[str, list[Any]],
futures: dict[str, list[asyncio.Future]],
) -> None:
Expand Down Expand Up @@ -240,8 +242,8 @@ async def consumer_handler(

except (
asyncio.CancelledError,
websockets.exceptions.ConnectionClosedError,
websockets.exceptions.ConnectionClosedOK,
ConnectionClosedError,
ConnectionClosedOK,
):
pass

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.8.6"
version = "2.9.0"
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>"]
Expand Down

0 comments on commit 1b7a1e1

Please sign in to comment.