From 375789b01927de699f2138bda3689c727e844eaa Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 30 Jan 2024 04:00:27 -1000 Subject: [PATCH] Use orjson encoder for websocket messages (#4854) I missed that these need to have dumps passed --- supervisor/api/proxy.py | 13 +++++++++---- supervisor/homeassistant/websocket.py | 9 ++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/supervisor/api/proxy.py b/supervisor/api/proxy.py index e2e1f7345..d8a68aae2 100644 --- a/supervisor/api/proxy.py +++ b/supervisor/api/proxy.py @@ -14,6 +14,7 @@ from aiohttp.web_exceptions import HTTPBadGateway, HTTPUnauthorized from ..coresys import CoreSysAttributes from ..exceptions import APIError, HomeAssistantAPIError, HomeAssistantAuthError +from ..utils.json import json_dumps _LOGGER: logging.Logger = logging.getLogger(__name__) @@ -145,7 +146,8 @@ class APIProxy(CoreSysAttributes): { "type": "auth", "access_token": self.sys_homeassistant.api.access_token, - } + }, + dumps=json_dumps, ) data = await client.receive_json() @@ -202,7 +204,8 @@ class APIProxy(CoreSysAttributes): # handle authentication try: await server.send_json( - {"type": "auth_required", "ha_version": self.sys_homeassistant.version} + {"type": "auth_required", "ha_version": self.sys_homeassistant.version}, + dumps=json_dumps, ) # Check API access @@ -215,14 +218,16 @@ class APIProxy(CoreSysAttributes): if not addon or not addon.access_homeassistant_api: _LOGGER.warning("Unauthorized WebSocket access!") await server.send_json( - {"type": "auth_invalid", "message": "Invalid access"} + {"type": "auth_invalid", "message": "Invalid access"}, + dumps=json_dumps, ) return server _LOGGER.info("WebSocket access from %s", addon.slug) await server.send_json( - {"type": "auth_ok", "ha_version": self.sys_homeassistant.version} + {"type": "auth_ok", "ha_version": self.sys_homeassistant.version}, + dumps=json_dumps, ) except (RuntimeError, ValueError) as err: _LOGGER.error("Can't initialize handshake: %s", err) diff --git a/supervisor/homeassistant/websocket.py b/supervisor/homeassistant/websocket.py index f1a8b3df0..cc7572e19 100644 --- a/supervisor/homeassistant/websocket.py +++ b/supervisor/homeassistant/websocket.py @@ -26,6 +26,7 @@ from ..exceptions import ( HomeAssistantWSError, HomeAssistantWSNotSupported, ) +from ..utils.json import json_dumps from .const import CLOSING_STATES, WSEvent, WSType MIN_VERSION = { @@ -74,7 +75,7 @@ class WSClient: self._message_id += 1 _LOGGER.debug("Sending: %s", message) try: - await self._client.send_json(message) + await self._client.send_json(message, dumps=json_dumps) except ConnectionError as err: raise HomeAssistantWSConnectionError(err) from err @@ -85,7 +86,7 @@ class WSClient: self._futures[message["id"]] = self._loop.create_future() _LOGGER.debug("Sending: %s", message) try: - await self._client.send_json(message) + await self._client.send_json(message, dumps=json_dumps) except ConnectionError as err: raise HomeAssistantWSConnectionError(err) from err @@ -163,7 +164,9 @@ class WSClient: hello_message = await client.receive_json() - await client.send_json({ATTR_TYPE: WSType.AUTH, ATTR_ACCESS_TOKEN: token}) + await client.send_json( + {ATTR_TYPE: WSType.AUTH, ATTR_ACCESS_TOKEN: token}, dumps=json_dumps + ) auth_ok_message = await client.receive_json()