Make it possible to fetch proxy media player album art (#32471)

This commit is contained in:
Paulus Schoutsen 2020-03-05 11:45:36 -08:00 committed by GitHub
parent 6a21afa2a8
commit d885853b35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 30 deletions

View File

@ -757,6 +757,11 @@ class MediaPlayerDevice(Entity):
if self.media_image_remotely_accessible: if self.media_image_remotely_accessible:
return self.media_image_url 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 image_hash = self.media_image_hash
if image_hash is None: if image_hash is None:
@ -788,11 +793,15 @@ class MediaPlayerDevice(Entity):
if self.state == STATE_OFF: if self.state == STATE_OFF:
return None return None
state_attr = { state_attr = {}
attr: getattr(self, attr)
for attr in ATTR_TO_PROPERTY for attr in ATTR_TO_PROPERTY:
if getattr(self, attr) is not None 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 return state_attr
@ -863,12 +872,6 @@ class MediaPlayerImageView(HomeAssistantView):
if not authenticated: if not authenticated:
return web.Response(status=401) 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() data, content_type = await player.async_get_media_image()
if data is None: if data is None:
@ -895,6 +898,10 @@ async def websocket_handle_thumbnail(hass, connection, msg):
) )
return 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() data, content_type = await player.async_get_media_image()
if data is None: if data is None:

View File

@ -1,6 +1,7 @@
"""Test the base functions of the media player.""" """Test the base functions of the media player."""
import base64 import base64
from unittest.mock import patch
from asynctest import patch
from homeassistant.components.websocket_api.const import TYPE_RESULT from homeassistant.components.websocket_api.const import TYPE_RESULT
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -8,7 +9,7 @@ from homeassistant.setup import async_setup_component
from tests.common import mock_coro 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.""" """Test get image via WS command."""
await async_setup_component( await async_setup_component(
hass, "media_player", {"media_player": {"platform": "demo"}} 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_type"] == "image/jpeg"
assert msg["result"]["content"] == base64.b64encode(b"image").decode("utf-8") 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.""" """Test get image via http command."""
await async_setup_component( await async_setup_component(
hass, "media_player", {"media_player": {"platform": "demo"}} 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( with patch(
"homeassistant.components.media_player.MediaPlayerDevice." "homeassistant.components.media_player.MediaPlayerDevice."
"async_get_media_image", "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() content = await resp.read()
assert content == b"image" 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.""" """Test get image url via http command."""
await async_setup_component(
hass, "media_player", {"media_player": {"platform": "demo"}}
)
client = await hass_client()
with patch( with patch(
"homeassistant.components.media_player.MediaPlayerDevice." "homeassistant.components.media_player.MediaPlayerDevice."
"media_image_remotely_accessible", "media_image_remotely_accessible",
return_value=True, return_value=True,
): ):
resp = await client.get( await async_setup_component(
"/api/media_player_proxy/media_player.bedroom", allow_redirects=False hass, "media_player", {"media_player": {"platform": "demo"}}
)
assert (
resp.headers["Location"]
== "https://img.youtube.com/vi/kxopViU98Xo/hqdefault.jpg"
) )
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"