From d989e4373d576c403790c9a7e5eb7a29d08e3c47 Mon Sep 17 00:00:00 2001 From: uvjustin <46082645+uvjustin@users.noreply.github.com> Date: Wed, 20 Jul 2022 12:08:11 +0800 Subject: [PATCH] Remove websocket_api send_big_result (#75452) --- homeassistant/components/camera/__init__.py | 34 +--------- .../components/lovelace/websocket.py | 5 +- .../components/media_player/__init__.py | 46 ------------- .../components/websocket_api/connection.py | 5 -- .../components/androidtv/test_media_player.py | 65 +++++++------------ tests/components/camera/test_init.py | 19 ------ tests/components/media_player/test_init.py | 34 ---------- .../websocket_api/test_connection.py | 20 ------ 8 files changed, 27 insertions(+), 201 deletions(-) diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index 35fa6fef1d6..dcee6c6c5ed 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -2,7 +2,6 @@ from __future__ import annotations import asyncio -import base64 import collections from collections.abc import Awaitable, Callable, Iterable from contextlib import suppress @@ -362,12 +361,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: hass.http.register_view(CameraImageView(component)) hass.http.register_view(CameraMjpegStream(component)) - websocket_api.async_register_command( - hass, - WS_TYPE_CAMERA_THUMBNAIL, - websocket_camera_thumbnail, - SCHEMA_WS_CAMERA_THUMBNAIL, - ) + websocket_api.async_register_command(hass, ws_camera_stream) websocket_api.async_register_command(hass, ws_camera_web_rtc_offer) websocket_api.async_register_command(hass, websocket_get_prefs) @@ -793,32 +787,6 @@ class CameraMjpegStream(CameraView): raise web.HTTPBadRequest() from err -@websocket_api.async_response -async def websocket_camera_thumbnail( - hass: HomeAssistant, connection: ActiveConnection, msg: dict -) -> None: - """Handle get camera thumbnail websocket command. - - Async friendly. - """ - _LOGGER.warning("The websocket command 'camera_thumbnail' has been deprecated") - try: - image = await async_get_image(hass, msg["entity_id"]) - connection.send_big_result( - msg["id"], - { - "content_type": image.content_type, - "content": base64.b64encode(image.content).decode("utf-8"), - }, - ) - except HomeAssistantError: - connection.send_message( - websocket_api.error_message( - msg["id"], "image_fetch_failed", "Unable to fetch image" - ) - ) - - @websocket_api.websocket_command( { vol.Required("type"): "camera/stream", diff --git a/homeassistant/components/lovelace/websocket.py b/homeassistant/components/lovelace/websocket.py index 7e04201291d..66ec7f22b09 100644 --- a/homeassistant/components/lovelace/websocket.py +++ b/homeassistant/components/lovelace/websocket.py @@ -37,10 +37,7 @@ def _handle_errors(func): connection.send_error(msg["id"], *error) return - if msg is not None: - connection.send_big_result(msg["id"], result) - else: - connection.send_result(msg["id"], result) + connection.send_result(msg["id"], result) return send_with_error_handling diff --git a/homeassistant/components/media_player/__init__.py b/homeassistant/components/media_player/__init__.py index 5e5347e6806..9ca9613278d 100644 --- a/homeassistant/components/media_player/__init__.py +++ b/homeassistant/components/media_player/__init__.py @@ -2,7 +2,6 @@ from __future__ import annotations import asyncio -import base64 import collections from collections.abc import Callable from contextlib import suppress @@ -27,7 +26,6 @@ from homeassistant.backports.enum import StrEnum from homeassistant.components import websocket_api from homeassistant.components.http import KEY_AUTHENTICATED, HomeAssistantView from homeassistant.components.websocket_api.const import ( - ERR_NOT_FOUND, ERR_NOT_SUPPORTED, ERR_UNKNOWN_ERROR, ) @@ -254,7 +252,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL ) - websocket_api.async_register_command(hass, websocket_handle_thumbnail) websocket_api.async_register_command(hass, websocket_browse_media) hass.http.register_view(MediaPlayerImageView(component)) @@ -1130,49 +1127,6 @@ class MediaPlayerImageView(HomeAssistantView): return web.Response(body=data, content_type=content_type, headers=headers) -@websocket_api.websocket_command( - { - vol.Required("type"): "media_player_thumbnail", - vol.Required("entity_id"): cv.entity_id, - } -) -@websocket_api.async_response -async def websocket_handle_thumbnail(hass, connection, msg): - """Handle get media player cover command. - - Async friendly. - """ - component = hass.data[DOMAIN] - - if (player := component.get_entity(msg["entity_id"])) is None: - connection.send_message( - websocket_api.error_message(msg["id"], ERR_NOT_FOUND, "Entity not found") - ) - return - - _LOGGER.warning( - "The websocket command media_player_thumbnail is deprecated. Use /api/media_player_proxy instead" - ) - - data, content_type = await player.async_get_media_image() - - if data is None: - connection.send_message( - websocket_api.error_message( - msg["id"], "thumbnail_fetch_failed", "Failed to fetch thumbnail" - ) - ) - return - - connection.send_big_result( - msg["id"], - { - "content_type": content_type, - "content": base64.b64encode(data).decode("utf-8"), - }, - ) - - @websocket_api.websocket_command( { vol.Required("type"): "media_player/browse_media", diff --git a/homeassistant/components/websocket_api/connection.py b/homeassistant/components/websocket_api/connection.py index 1c26d958969..87c52288bcc 100644 --- a/homeassistant/components/websocket_api/connection.py +++ b/homeassistant/components/websocket_api/connection.py @@ -11,7 +11,6 @@ import voluptuous as vol from homeassistant.auth.models import RefreshToken, User from homeassistant.core import Context, HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError, Unauthorized -from homeassistant.helpers.json import JSON_DUMP from . import const, messages @@ -54,10 +53,6 @@ class ActiveConnection: """Send a result message.""" self.send_message(messages.result_message(msg_id, result)) - def send_big_result(self, msg_id: int, result: Any) -> None: - """Send a result message that would be expensive to JSON serialize.""" - self.send_message(JSON_DUMP(messages.result_message(msg_id, result))) - @callback def send_error(self, msg_id: int, code: str, message: str) -> None: """Send a error message.""" diff --git a/tests/components/androidtv/test_media_player.py b/tests/components/androidtv/test_media_player.py index 73f8de55cc9..f5487c78425 100644 --- a/tests/components/androidtv/test_media_player.py +++ b/tests/components/androidtv/test_media_player.py @@ -1,5 +1,4 @@ """The tests for the androidtv platform.""" -import base64 import logging from unittest.mock import Mock, patch @@ -52,7 +51,6 @@ from homeassistant.components.media_player import ( SERVICE_VOLUME_SET, SERVICE_VOLUME_UP, ) -from homeassistant.components.websocket_api.const import TYPE_RESULT from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ( ATTR_COMMAND, @@ -851,10 +849,10 @@ async def test_androidtv_volume_set(hass): patch_set_volume_level.assert_called_with(0.5) -async def test_get_image(hass, hass_ws_client): +async def test_get_image_http(hass, hass_client_no_auth): """Test taking a screen capture. - This is based on `test_get_image` in tests/components/media_player/test_init.py. + This is based on `test_get_image_http` in tests/components/media_player/test_init.py. """ patch_key, entity_id, config_entry = _setup(CONFIG_ANDROIDTV_DEFAULT) config_entry.add_to_hass(hass) @@ -868,44 +866,36 @@ async def test_get_image(hass, hass_ws_client): with patchers.patch_shell("11")[patch_key]: await async_update_entity(hass, entity_id) - client = await hass_ws_client(hass) + media_player_name = "media_player." + slugify( + CONFIG_ANDROIDTV_DEFAULT[TEST_ENTITY_NAME] + ) + state = hass.states.get(media_player_name) + assert "entity_picture_local" not in state.attributes + + client = await hass_client_no_auth() with patch( "androidtv.basetv.basetv_async.BaseTVAsync.adb_screencap", return_value=b"image" ): - await client.send_json( - {"id": 5, "type": "media_player_thumbnail", "entity_id": entity_id} - ) + resp = await client.get(state.attributes["entity_picture"]) + content = await resp.read() - msg = await client.receive_json() - - assert msg["id"] == 5 - assert msg["type"] == TYPE_RESULT - assert msg["success"] - assert msg["result"]["content_type"] == "image/png" - assert msg["result"]["content"] == base64.b64encode(b"image").decode("utf-8") + assert content == b"image" with patch( "androidtv.basetv.basetv_async.BaseTVAsync.adb_screencap", side_effect=ConnectionResetError, ): - await client.send_json( - {"id": 6, "type": "media_player_thumbnail", "entity_id": entity_id} - ) + resp = await client.get(state.attributes["entity_picture"]) - msg = await client.receive_json() - - # The device is unavailable, but getting the media image did not cause an exception - state = hass.states.get(entity_id) - assert state is not None - assert state.state == STATE_UNAVAILABLE + # The device is unavailable, but getting the media image did not cause an exception + state = hass.states.get(media_player_name) + assert state is not None + assert state.state == STATE_UNAVAILABLE -async def test_get_image_disabled(hass, hass_ws_client): - """Test taking a screen capture with screencap option disabled. - - This is based on `test_get_image` in tests/components/media_player/test_init.py. - """ +async def test_get_image_disabled(hass): + """Test that the screencap option can disable entity_picture.""" patch_key, entity_id, config_entry = _setup(CONFIG_ANDROIDTV_DEFAULT) config_entry.add_to_hass(hass) hass.config_entries.async_update_entry( @@ -921,17 +911,12 @@ async def test_get_image_disabled(hass, hass_ws_client): with patchers.patch_shell("11")[patch_key]: await async_update_entity(hass, entity_id) - client = await hass_ws_client(hass) - - with patch( - "androidtv.basetv.basetv_async.BaseTVAsync.adb_screencap", return_value=b"image" - ) as screen_cap: - await client.send_json( - {"id": 5, "type": "media_player_thumbnail", "entity_id": entity_id} - ) - - await client.receive_json() - assert not screen_cap.called + media_player_name = "media_player." + slugify( + CONFIG_ANDROIDTV_DEFAULT[TEST_ENTITY_NAME] + ) + state = hass.states.get(media_player_name) + assert "entity_picture_local" not in state.attributes + assert "entity_picture" not in state.attributes async def _test_service( diff --git a/tests/components/camera/test_init.py b/tests/components/camera/test_init.py index ba13bbd6c52..f800a4ac2bd 100644 --- a/tests/components/camera/test_init.py +++ b/tests/components/camera/test_init.py @@ -1,6 +1,5 @@ """The tests for the camera component.""" import asyncio -import base64 from http import HTTPStatus import io from unittest.mock import AsyncMock, Mock, PropertyMock, mock_open, patch @@ -235,24 +234,6 @@ async def test_snapshot_service(hass, mock_camera): assert mock_write.mock_calls[0][1][0] == b"Test" -async def test_websocket_camera_thumbnail(hass, hass_ws_client, mock_camera): - """Test camera_thumbnail websocket command.""" - await async_setup_component(hass, "camera", {}) - - client = await hass_ws_client(hass) - await client.send_json( - {"id": 5, "type": "camera_thumbnail", "entity_id": "camera.demo_camera"} - ) - - msg = await client.receive_json() - - assert msg["id"] == 5 - assert msg["type"] == TYPE_RESULT - assert msg["success"] - assert msg["result"]["content_type"] == "image/jpg" - assert msg["result"]["content"] == base64.b64encode(b"Test").decode("utf-8") - - async def test_websocket_stream_no_source( hass, hass_ws_client, mock_camera, mock_stream ): diff --git a/tests/components/media_player/test_init.py b/tests/components/media_player/test_init.py index eceb7e9ec4f..f0666a11545 100644 --- a/tests/components/media_player/test_init.py +++ b/tests/components/media_player/test_init.py @@ -1,6 +1,5 @@ """Test the base functions of the media player.""" import asyncio -import base64 from http import HTTPStatus from unittest.mock import patch @@ -14,39 +13,6 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF from homeassistant.setup import async_setup_component -async def test_get_image(hass, hass_ws_client, caplog): - """Test get image via WS command.""" - await async_setup_component( - hass, "media_player", {"media_player": {"platform": "demo"}} - ) - await hass.async_block_till_done() - - client = await hass_ws_client(hass) - - with patch( - "homeassistant.components.media_player.MediaPlayerEntity." - "async_get_media_image", - return_value=(b"image", "image/jpeg"), - ): - await client.send_json( - { - "id": 5, - "type": "media_player_thumbnail", - "entity_id": "media_player.bedroom", - } - ) - - msg = await client.receive_json() - - assert msg["id"] == 5 - assert msg["type"] == TYPE_RESULT - assert msg["success"] - assert msg["result"]["content_type"] == "image/jpeg" - assert msg["result"]["content"] == base64.b64encode(b"image").decode("utf-8") - - assert "media_player_thumbnail is deprecated" in caplog.text - - async def test_get_image_http(hass, hass_client_no_auth): """Test get image via http command.""" await async_setup_component( diff --git a/tests/components/websocket_api/test_connection.py b/tests/components/websocket_api/test_connection.py index da31f0ee8a3..fd9af99c1a4 100644 --- a/tests/components/websocket_api/test_connection.py +++ b/tests/components/websocket_api/test_connection.py @@ -7,30 +7,10 @@ import voluptuous as vol from homeassistant import exceptions from homeassistant.components import websocket_api -from homeassistant.components.websocket_api import const from tests.common import MockUser -async def test_send_big_result(hass, websocket_client): - """Test sending big results over the WS.""" - - @websocket_api.websocket_command({"type": "big_result"}) - @websocket_api.async_response - def send_big_result(hass, connection, msg): - connection.send_big_result(msg["id"], {"big": "result"}) - - websocket_api.async_register_command(hass, send_big_result) - - await websocket_client.send_json({"id": 5, "type": "big_result"}) - - msg = await websocket_client.receive_json() - assert msg["id"] == 5 - assert msg["type"] == const.TYPE_RESULT - assert msg["success"] - assert msg["result"] == {"big": "result"} - - async def test_exception_handling(): """Test handling of exceptions.""" send_messages = []