Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WebSocket sender to avoid extra utf-8 decode/encode round-trip #713

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions aioshelly/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
4 changes: 2 additions & 2 deletions aioshelly/rpc_device/wsrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
InvalidMessage,
RpcCallError,
)
from ..json import json_dumps, json_loads
from ..json import json_bytes, json_loads

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bluetooth-data-tools>=1.19.0
aiohttp
aiohttp>=3.11.1
habluetooth>=2.1.0
yarl
orjson>=3.8.1
Loading