From 6b60fb954197003c05789a6f34e91d2989e091eb Mon Sep 17 00:00:00 2001 From: uvjustin <46082645+uvjustin@users.noreply.github.com> Date: Tue, 19 Jul 2022 21:40:23 +0800 Subject: [PATCH] Don't use executor in send_big_result (#75427) --- homeassistant/components/camera/__init__.py | 2 +- homeassistant/components/lovelace/websocket.py | 2 +- homeassistant/components/media_player/__init__.py | 2 +- homeassistant/components/websocket_api/connection.py | 7 ++----- tests/components/websocket_api/test_connection.py | 4 ++-- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index 45b77ec1bd6..35fa6fef1d6 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -804,7 +804,7 @@ async def websocket_camera_thumbnail( _LOGGER.warning("The websocket command 'camera_thumbnail' has been deprecated") try: image = await async_get_image(hass, msg["entity_id"]) - await connection.send_big_result( + connection.send_big_result( msg["id"], { "content_type": image.content_type, diff --git a/homeassistant/components/lovelace/websocket.py b/homeassistant/components/lovelace/websocket.py index cb45d9cfbc6..7e04201291d 100644 --- a/homeassistant/components/lovelace/websocket.py +++ b/homeassistant/components/lovelace/websocket.py @@ -38,7 +38,7 @@ def _handle_errors(func): return if msg is not None: - await connection.send_big_result(msg["id"], result) + connection.send_big_result(msg["id"], result) else: connection.send_result(msg["id"], result) diff --git a/homeassistant/components/media_player/__init__.py b/homeassistant/components/media_player/__init__.py index 14546a36ec8..5e5347e6806 100644 --- a/homeassistant/components/media_player/__init__.py +++ b/homeassistant/components/media_player/__init__.py @@ -1164,7 +1164,7 @@ async def websocket_handle_thumbnail(hass, connection, msg): ) return - await connection.send_big_result( + connection.send_big_result( msg["id"], { "content_type": content_type, diff --git a/homeassistant/components/websocket_api/connection.py b/homeassistant/components/websocket_api/connection.py index 26c4c6f8321..1c26d958969 100644 --- a/homeassistant/components/websocket_api/connection.py +++ b/homeassistant/components/websocket_api/connection.py @@ -54,12 +54,9 @@ class ActiveConnection: """Send a result message.""" self.send_message(messages.result_message(msg_id, result)) - async def send_big_result(self, msg_id: int, result: Any) -> None: + def send_big_result(self, msg_id: int, result: Any) -> None: """Send a result message that would be expensive to JSON serialize.""" - content = await self.hass.async_add_executor_job( - JSON_DUMP, messages.result_message(msg_id, result) - ) - self.send_message(content) + self.send_message(JSON_DUMP(messages.result_message(msg_id, result))) @callback def send_error(self, msg_id: int, code: str, message: str) -> None: diff --git a/tests/components/websocket_api/test_connection.py b/tests/components/websocket_api/test_connection.py index 3a54d5912e0..da31f0ee8a3 100644 --- a/tests/components/websocket_api/test_connection.py +++ b/tests/components/websocket_api/test_connection.py @@ -17,8 +17,8 @@ async def test_send_big_result(hass, websocket_client): @websocket_api.websocket_command({"type": "big_result"}) @websocket_api.async_response - async def send_big_result(hass, connection, msg): - await connection.send_big_result(msg["id"], {"big": "result"}) + def send_big_result(hass, connection, msg): + connection.send_big_result(msg["id"], {"big": "result"}) websocket_api.async_register_command(hass, send_big_result)