From d885853b355d9457c56ce7ac4485510048de39e9 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 5 Mar 2020 11:45:36 -0800 Subject: [PATCH] Make it possible to fetch proxy media player album art (#32471) --- .../components/media_player/__init__.py | 29 ++++++----- tests/components/media_player/test_init.py | 49 ++++++++++++------- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/media_player/__init__.py b/homeassistant/components/media_player/__init__.py index 8a31dbe6bdb..a62b6bd7c2b 100644 --- a/homeassistant/components/media_player/__init__.py +++ b/homeassistant/components/media_player/__init__.py @@ -757,6 +757,11 @@ class MediaPlayerDevice(Entity): if self.media_image_remotely_accessible: return self.media_image_url + return self.media_image_local + + @property + def media_image_local(self): + """Return local url to media image.""" image_hash = self.media_image_hash if image_hash is None: @@ -788,11 +793,15 @@ class MediaPlayerDevice(Entity): if self.state == STATE_OFF: return None - state_attr = { - attr: getattr(self, attr) - for attr in ATTR_TO_PROPERTY - if getattr(self, attr) is not None - } + state_attr = {} + + for attr in ATTR_TO_PROPERTY: + value = getattr(self, attr) + if value is not None: + state_attr[attr] = value + + if self.media_image_remotely_accessible: + state_attr["entity_picture_local"] = self.media_image_local return state_attr @@ -863,12 +872,6 @@ class MediaPlayerImageView(HomeAssistantView): if not authenticated: return web.Response(status=401) - if player.media_image_remotely_accessible: - url = player.media_image_url - if url is not None: - return web.Response(status=302, headers={"location": url}) - return web.Response(status=500) - data, content_type = await player.async_get_media_image() if data is None: @@ -895,6 +898,10 @@ async def websocket_handle_thumbnail(hass, connection, msg): ) 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: diff --git a/tests/components/media_player/test_init.py b/tests/components/media_player/test_init.py index 3db92cda42d..f3d8ec3298a 100644 --- a/tests/components/media_player/test_init.py +++ b/tests/components/media_player/test_init.py @@ -1,6 +1,7 @@ """Test the base functions of the media player.""" import base64 -from unittest.mock import patch + +from asynctest import patch from homeassistant.components.websocket_api.const import TYPE_RESULT from homeassistant.setup import async_setup_component @@ -8,7 +9,7 @@ from homeassistant.setup import async_setup_component from tests.common import mock_coro -async def test_get_image(hass, hass_ws_client): +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"}} @@ -37,43 +38,53 @@ async def test_get_image(hass, hass_ws_client): 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): + +async def test_get_image_http(hass, aiohttp_client): """Test get image via http command.""" await async_setup_component( hass, "media_player", {"media_player": {"platform": "demo"}} ) - client = await hass_client() + state = hass.states.get("media_player.bedroom") + assert "entity_picture_local" not in state.attributes + + client = await aiohttp_client(hass.http.app) with patch( "homeassistant.components.media_player.MediaPlayerDevice." "async_get_media_image", - return_value=mock_coro((b"image", "image/jpeg")), + return_value=(b"image", "image/jpeg"), ): - resp = await client.get("/api/media_player_proxy/media_player.bedroom") + resp = await client.get(state.attributes["entity_picture"]) content = await resp.read() assert content == b"image" -async def test_get_image_http_url(hass, hass_client): +async def test_get_image_http_remote(hass, aiohttp_client): """Test get image url via http command.""" - await async_setup_component( - hass, "media_player", {"media_player": {"platform": "demo"}} - ) - - client = await hass_client() - with patch( "homeassistant.components.media_player.MediaPlayerDevice." "media_image_remotely_accessible", return_value=True, ): - resp = await client.get( - "/api/media_player_proxy/media_player.bedroom", allow_redirects=False - ) - assert ( - resp.headers["Location"] - == "https://img.youtube.com/vi/kxopViU98Xo/hqdefault.jpg" + await async_setup_component( + hass, "media_player", {"media_player": {"platform": "demo"}} ) + + state = hass.states.get("media_player.bedroom") + assert "entity_picture_local" in state.attributes + + client = await aiohttp_client(hass.http.app) + + with patch( + "homeassistant.components.media_player.MediaPlayerDevice." + "async_get_media_image", + return_value=(b"image", "image/jpeg"), + ): + resp = await client.get(state.attributes["entity_picture_local"]) + content = await resp.read() + + assert content == b"image"