Avoid proxied Roku images during internal requests (#43547)

This commit is contained in:
Chris Talkington 2021-01-26 16:21:25 -06:00 committed by GitHub
parent fb527814f9
commit 14785660b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 2 deletions

View File

@ -34,6 +34,7 @@ from homeassistant.const import (
STATE_STANDBY,
)
from homeassistant.helpers import entity_platform
from homeassistant.helpers.network import is_internal_request
from . import RokuDataUpdateCoordinator, RokuEntity, roku_exception_handler
from .browse_media import build_item_response, library_payload
@ -250,9 +251,19 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
async def async_browse_media(self, media_content_type=None, media_content_id=None):
"""Implement the websocket media browsing helper."""
is_internal = is_internal_request(self.hass)
def _get_thumbnail_url(*args, **kwargs):
return self.get_browse_image_url(*args, **kwargs)
def _get_thumbnail_url(
media_content_type, media_content_id, media_image_id=None
):
if is_internal:
if media_content_type == MEDIA_TYPE_APP and media_content_id:
return self.coordinator.roku.app_icon_url(media_content_id)
return None
return self.get_browse_image_url(
media_content_type, media_content_id, media_image_id
)
if media_content_type in [None, "library"]:
return library_payload(self.coordinator, _get_thumbnail_url)

View File

@ -40,6 +40,7 @@ from homeassistant.components.media_player.const import (
)
from homeassistant.components.roku.const import ATTR_KEYWORD, DOMAIN, SERVICE_SEARCH
from homeassistant.components.websocket_api.const import TYPE_RESULT
from homeassistant.config import async_process_ha_core_config
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_MEDIA_NEXT_TRACK,
@ -599,6 +600,68 @@ async def test_media_browse(hass, aioclient_mock, hass_ws_client):
assert not msg["success"]
async def test_media_browse_internal(hass, aioclient_mock, hass_ws_client):
"""Test browsing media with internal url."""
await async_process_ha_core_config(
hass,
{"internal_url": "http://example.local:8123"},
)
assert hass.config.internal_url == "http://example.local:8123"
await setup_integration(
hass,
aioclient_mock,
device="rokutv",
app="tvinput-dtv",
host=TV_HOST,
unique_id=TV_SERIAL,
)
client = await hass_ws_client(hass)
with patch(
"homeassistant.helpers.network._get_request_host", return_value="example.local"
):
await client.send_json(
{
"id": 2,
"type": "media_player/browse_media",
"entity_id": TV_ENTITY_ID,
"media_content_type": MEDIA_TYPE_APPS,
"media_content_id": "apps",
}
)
msg = await client.receive_json()
assert msg["id"] == 2
assert msg["type"] == TYPE_RESULT
assert msg["success"]
assert msg["result"]
assert msg["result"]["title"] == "Apps"
assert msg["result"]["media_class"] == MEDIA_CLASS_DIRECTORY
assert msg["result"]["media_content_type"] == MEDIA_TYPE_APPS
assert msg["result"]["children_media_class"] == MEDIA_CLASS_APP
assert msg["result"]["can_expand"]
assert not msg["result"]["can_play"]
assert len(msg["result"]["children"]) == 11
assert msg["result"]["children_media_class"] == MEDIA_CLASS_APP
assert msg["result"]["children"][0]["title"] == "Satellite TV"
assert msg["result"]["children"][0]["media_content_type"] == MEDIA_TYPE_APP
assert msg["result"]["children"][0]["media_content_id"] == "tvinput.hdmi2"
assert "/query/icon/tvinput.hdmi2" in msg["result"]["children"][0]["thumbnail"]
assert msg["result"]["children"][0]["can_play"]
assert msg["result"]["children"][3]["title"] == "Roku Channel Store"
assert msg["result"]["children"][3]["media_content_type"] == MEDIA_TYPE_APP
assert msg["result"]["children"][3]["media_content_id"] == "11"
assert "/query/icon/11" in msg["result"]["children"][3]["thumbnail"]
assert msg["result"]["children"][3]["can_play"]
async def test_integration_services(
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
) -> None: