From 266c1c250697fe56fb933d3023578df7064e622d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 13 Nov 2024 12:05:00 -0600 Subject: [PATCH 1/2] Update WebSocket sender to avoid extra utf-8 decode/encode round-trip closes #676 --- aioshelly/json.py | 9 +++++++++ aioshelly/rpc_device/wsrpc.py | 4 ++-- requirements.txt | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/aioshelly/json.py b/aioshelly/json.py index 3b8b815a..96f79e28 100644 --- a/aioshelly/json.py +++ b/aioshelly/json.py @@ -22,3 +22,12 @@ def json_dumps(data: Any) -> str: option=orjson.OPT_NON_STR_KEYS, default=json_encoder_default, ).decode("utf-8") + + +def json_bytes(data: Any) -> bytes: + """Dump json bytes.""" + return orjson.dumps( + data, + option=orjson.OPT_NON_STR_KEYS, + default=json_encoder_default, + ) diff --git a/aioshelly/rpc_device/wsrpc.py b/aioshelly/rpc_device/wsrpc.py index 18f85490..f3e24ba3 100644 --- a/aioshelly/rpc_device/wsrpc.py +++ b/aioshelly/rpc_device/wsrpc.py @@ -48,7 +48,7 @@ InvalidMessage, RpcCallError, ) -from ..json import json_dumps, json_loads +from ..json import json_bytes, json_loads _LOGGER = logging.getLogger(__name__) @@ -531,7 +531,7 @@ async def _send_json(self, data: dict[str, Any]) -> None: if TYPE_CHECKING: assert self._client - await self._client.send_str(json_dumps(data)) + await self._client.send_frame(json_bytes(data), WSMsgType.TEXT) class WsServer(WsBase): diff --git a/requirements.txt b/requirements.txt index 481c6d22..dbc4956b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ bluetooth-data-tools>=1.19.0 -aiohttp +aiohttp>=3.11.0 habluetooth>=2.1.0 yarl orjson>=3.8.1 From cd490e433424de38f700e68cbf818dfe96f4c97d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 14 Nov 2024 10:47:28 -0600 Subject: [PATCH 2/2] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index dbc4956b..bb90c31b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ bluetooth-data-tools>=1.19.0 -aiohttp>=3.11.0 +aiohttp>=3.11.1 habluetooth>=2.1.0 yarl orjson>=3.8.1