Use orjson encoder for websocket messages (#4854)

I missed that these need to have dumps passed
This commit is contained in:
J. Nick Koston 2024-01-30 04:00:27 -10:00 committed by GitHub
parent 140b769a42
commit 375789b019
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 7 deletions

View File

@ -14,6 +14,7 @@ from aiohttp.web_exceptions import HTTPBadGateway, HTTPUnauthorized
from ..coresys import CoreSysAttributes from ..coresys import CoreSysAttributes
from ..exceptions import APIError, HomeAssistantAPIError, HomeAssistantAuthError from ..exceptions import APIError, HomeAssistantAPIError, HomeAssistantAuthError
from ..utils.json import json_dumps
_LOGGER: logging.Logger = logging.getLogger(__name__) _LOGGER: logging.Logger = logging.getLogger(__name__)
@ -145,7 +146,8 @@ class APIProxy(CoreSysAttributes):
{ {
"type": "auth", "type": "auth",
"access_token": self.sys_homeassistant.api.access_token, "access_token": self.sys_homeassistant.api.access_token,
} },
dumps=json_dumps,
) )
data = await client.receive_json() data = await client.receive_json()
@ -202,7 +204,8 @@ class APIProxy(CoreSysAttributes):
# handle authentication # handle authentication
try: try:
await server.send_json( 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 # Check API access
@ -215,14 +218,16 @@ class APIProxy(CoreSysAttributes):
if not addon or not addon.access_homeassistant_api: if not addon or not addon.access_homeassistant_api:
_LOGGER.warning("Unauthorized WebSocket access!") _LOGGER.warning("Unauthorized WebSocket access!")
await server.send_json( await server.send_json(
{"type": "auth_invalid", "message": "Invalid access"} {"type": "auth_invalid", "message": "Invalid access"},
dumps=json_dumps,
) )
return server return server
_LOGGER.info("WebSocket access from %s", addon.slug) _LOGGER.info("WebSocket access from %s", addon.slug)
await server.send_json( 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: except (RuntimeError, ValueError) as err:
_LOGGER.error("Can't initialize handshake: %s", err) _LOGGER.error("Can't initialize handshake: %s", err)

View File

@ -26,6 +26,7 @@ from ..exceptions import (
HomeAssistantWSError, HomeAssistantWSError,
HomeAssistantWSNotSupported, HomeAssistantWSNotSupported,
) )
from ..utils.json import json_dumps
from .const import CLOSING_STATES, WSEvent, WSType from .const import CLOSING_STATES, WSEvent, WSType
MIN_VERSION = { MIN_VERSION = {
@ -74,7 +75,7 @@ class WSClient:
self._message_id += 1 self._message_id += 1
_LOGGER.debug("Sending: %s", message) _LOGGER.debug("Sending: %s", message)
try: try:
await self._client.send_json(message) await self._client.send_json(message, dumps=json_dumps)
except ConnectionError as err: except ConnectionError as err:
raise HomeAssistantWSConnectionError(err) from err raise HomeAssistantWSConnectionError(err) from err
@ -85,7 +86,7 @@ class WSClient:
self._futures[message["id"]] = self._loop.create_future() self._futures[message["id"]] = self._loop.create_future()
_LOGGER.debug("Sending: %s", message) _LOGGER.debug("Sending: %s", message)
try: try:
await self._client.send_json(message) await self._client.send_json(message, dumps=json_dumps)
except ConnectionError as err: except ConnectionError as err:
raise HomeAssistantWSConnectionError(err) from err raise HomeAssistantWSConnectionError(err) from err
@ -163,7 +164,9 @@ class WSClient:
hello_message = await client.receive_json() 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() auth_ok_message = await client.receive_json()