Skip to content

Commit

Permalink
Make it possible to fetch proxy media player album art (home-assistan…
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Mar 5, 2020
1 parent 6a21afa commit d885853
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
29 changes: 18 additions & 11 deletions homeassistant/components/media_player/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,11 @@ def entity_picture(self):
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:
Expand Down Expand Up @@ -788,11 +793,15 @@ def state_attributes(self):
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

Expand Down Expand Up @@ -863,12 +872,6 @@ async def get(self, request, entity_id):
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:
Expand All @@ -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:
Expand Down
49 changes: 30 additions & 19 deletions tests/components/media_player/test_init.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""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

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"}}
Expand Down Expand Up @@ -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"

0 comments on commit d885853

Please sign in to comment.