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
This commit is contained in:
J. Nick Koston 2024-01-21 17:53:45 -10:00 committed by GitHub
parent a3f9fc45e3
commit 0b3bcca49b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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