[`music.snapcast`] Fixed message serialization issue.

Closes: #338
This commit is contained in:
Fabio Manganiello 2023-11-09 22:46:54 +01:00
parent 1ec42e11fc
commit 6c3edb73f9
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
1 changed files with 7 additions and 7 deletions

View File

@ -1,7 +1,7 @@
import json import json
import socket import socket
import threading import threading
from typing import Collection, Optional from typing import Collection, Optional, Union
from platypush.config import Config from platypush.config import Config
from platypush.context import get_backend from platypush.context import get_backend
@ -52,18 +52,18 @@ class MusicSnapcastPlugin(Plugin):
return sock return sock
@classmethod @classmethod
def _send(cls, sock: socket.socket, req: dict): def _send(cls, sock: socket.socket, req: Union[dict, str, bytes]):
if isinstance(req, dict): if isinstance(req, dict):
r = json.dumps(req) req = json.dumps(req)
if isinstance(req, str): if isinstance(req, str):
r = req.encode() req = req.encode()
if not isinstance(r, bytes): if isinstance(req, bytes):
sock.send(req + cls._SOCKET_EOL)
else:
raise RuntimeError( raise RuntimeError(
f'Unsupported type {type(req)} for Snapcast request: {req}' f'Unsupported type {type(req)} for Snapcast request: {req}'
) )
sock.send(r + cls._SOCKET_EOL)
@classmethod @classmethod
def _recv(cls, sock): def _recv(cls, sock):
buf = b'' buf = b''