From 0b3bcca49baa48269f0519de01a9f7aad97aabdb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" <nick@koston.org> Date: Sun, 21 Jan 2024 17:53:45 -1000 Subject: [PATCH] Avoid string decode/encode round trip in websocket_api get_services (#108632) The cache was converting from bytes to str and when we read the cache we converted it back to bytes again --- homeassistant/components/websocket_api/commands.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index 32f59bd0c5f..c088acc6e00 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -45,7 +45,7 @@ from homeassistant.helpers.json import ( JSON_DUMP, ExtendedJSONEncoder, find_paths_unserializable_data, - json_dumps, + json_bytes, ) from homeassistant.helpers.service import async_get_all_descriptions from homeassistant.helpers.typing import EventType @@ -460,7 +460,7 @@ def _send_handle_entities_init_response( ) -async def _async_get_all_descriptions_json(hass: HomeAssistant) -> str: +async def _async_get_all_descriptions_json(hass: HomeAssistant) -> bytes: """Return JSON of descriptions (i.e. user documentation) for all service calls.""" descriptions = await async_get_all_descriptions(hass) if ALL_SERVICE_DESCRIPTIONS_JSON_CACHE in hass.data: @@ -469,8 +469,8 @@ async def _async_get_all_descriptions_json(hass: HomeAssistant) -> str: ] # If the descriptions are the same, return the cached JSON payload if cached_descriptions is descriptions: - return cast(str, cached_json_payload) - json_payload = json_dumps(descriptions) + return cast(bytes, cached_json_payload) + json_payload = json_bytes(descriptions) hass.data[ALL_SERVICE_DESCRIPTIONS_JSON_CACHE] = (descriptions, json_payload) return json_payload @@ -482,7 +482,7 @@ async def handle_get_services( ) -> None: """Handle get services command.""" payload = await _async_get_all_descriptions_json(hass) - connection.send_message(construct_result_message(msg["id"], payload.encode())) + connection.send_message(construct_result_message(msg["id"], payload)) @callback -- GitLab