mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add media player external url (#23337)
* Add media player external url * Lint * Simplify * Update __init__.py * Update __init__.py * Use 302
This commit is contained in:
parent
e3e7fb5ff6
commit
de6fdb09f4
@ -1046,6 +1046,11 @@ class CastDevice(MediaPlayerDevice):
|
|||||||
|
|
||||||
return images[0].url if images and images[0].url else None
|
return images[0].url if images and images[0].url else None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def media_image_remotely_accessible(self) -> bool:
|
||||||
|
"""If the image url is remotely accessible."""
|
||||||
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_title(self):
|
def media_title(self):
|
||||||
"""Title of current playing media."""
|
"""Title of current playing media."""
|
||||||
|
@ -324,6 +324,11 @@ class MediaPlayerDevice(Entity):
|
|||||||
"""Image url of current playing media."""
|
"""Image url of current playing media."""
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def media_image_remotely_accessible(self) -> bool:
|
||||||
|
"""If the image url is remotely accessible."""
|
||||||
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_image_hash(self):
|
def media_image_hash(self):
|
||||||
"""Hash value for media image."""
|
"""Hash value for media image."""
|
||||||
@ -722,6 +727,9 @@ class MediaPlayerDevice(Entity):
|
|||||||
if self.state == STATE_OFF:
|
if self.state == STATE_OFF:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
if self.media_image_remotely_accessible:
|
||||||
|
return self.media_image_url
|
||||||
|
|
||||||
image_hash = self.media_image_hash
|
image_hash = self.media_image_hash
|
||||||
|
|
||||||
if image_hash is None:
|
if image_hash is None:
|
||||||
@ -808,6 +816,14 @@ 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:
|
||||||
|
@ -8,8 +8,8 @@ from homeassistant.components.websocket_api.const import TYPE_RESULT
|
|||||||
from tests.common import mock_coro
|
from tests.common import mock_coro
|
||||||
|
|
||||||
|
|
||||||
async def test_get_panels(hass, hass_ws_client):
|
async def test_get_image(hass, hass_ws_client):
|
||||||
"""Test get_panels command."""
|
"""Test get image via WS command."""
|
||||||
await async_setup_component(hass, 'media_player', {
|
await async_setup_component(hass, 'media_player', {
|
||||||
'media_player': {
|
'media_player': {
|
||||||
'platform': 'demo'
|
'platform': 'demo'
|
||||||
@ -35,3 +35,40 @@ async def test_get_panels(hass, hass_ws_client):
|
|||||||
assert msg['result']['content_type'] == 'image/jpeg'
|
assert msg['result']['content_type'] == 'image/jpeg'
|
||||||
assert msg['result']['content'] == \
|
assert msg['result']['content'] == \
|
||||||
base64.b64encode(b'image').decode('utf-8')
|
base64.b64encode(b'image').decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_image_http(hass, hass_client):
|
||||||
|
"""Test get image 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.'
|
||||||
|
'async_get_media_image', return_value=mock_coro(
|
||||||
|
(b'image', 'image/jpeg'))):
|
||||||
|
resp = await client.get('/api/media_player_proxy/media_player.bedroom')
|
||||||
|
content = await resp.read()
|
||||||
|
|
||||||
|
assert content == b'image'
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_image_http_url(hass, hass_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'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user