Skip to content

Commit

Permalink
Add optional flag to bypass the media proxy in esphome media players (#…
Browse files Browse the repository at this point in the history
…131191)

* Add optional flag to play_media to bypass media proxy

* use constants

* add test
  • Loading branch information
marcelveldt authored Nov 21, 2024
1 parent bd0a2b6 commit 9bbf9be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion homeassistant/components/esphome/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from homeassistant.components import media_source
from homeassistant.components.media_player import (
ATTR_MEDIA_ANNOUNCE,
ATTR_MEDIA_EXTRA,
BrowseMedia,
MediaPlayerDeviceClass,
MediaPlayerEntity,
Expand Down Expand Up @@ -50,6 +51,8 @@
}
)

ATTR_BYPASS_PROXY = "bypass_proxy"


class EsphomeMediaPlayer(
EsphomeEntity[MediaPlayerInfo, MediaPlayerEntityState], MediaPlayerEntity
Expand Down Expand Up @@ -108,13 +111,15 @@ async def async_play_media(

media_id = async_process_play_media_url(self.hass, media_id)
announcement = kwargs.get(ATTR_MEDIA_ANNOUNCE)
bypass_proxy = kwargs.get(ATTR_MEDIA_EXTRA, {}).get(ATTR_BYPASS_PROXY)

supported_formats: list[MediaPlayerSupportedFormat] | None = (
self._entry_data.media_player_formats.get(self._static_info.unique_id)
)

if (
supported_formats
not bypass_proxy
and supported_formats
and _is_url(media_id)
and (
proxy_url := self._get_proxy_url(
Expand Down
20 changes: 20 additions & 0 deletions tests/components/esphome/test_media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
ATTR_MEDIA_ANNOUNCE,
ATTR_MEDIA_CONTENT_ID,
ATTR_MEDIA_CONTENT_TYPE,
ATTR_MEDIA_EXTRA,
ATTR_MEDIA_VOLUME_LEVEL,
ATTR_MEDIA_VOLUME_MUTED,
DOMAIN as MEDIA_PLAYER_DOMAIN,
Expand Down Expand Up @@ -414,3 +415,22 @@ async def test_media_player_proxy(

media_args = mock_client.media_player_command.call_args.kwargs
assert media_args["announcement"]

# test with bypass_proxy flag
mock_async_create_proxy_url.reset_mock()
await hass.services.async_call(
MEDIA_PLAYER_DOMAIN,
SERVICE_PLAY_MEDIA,
{
ATTR_ENTITY_ID: "media_player.test_mymedia_player",
ATTR_MEDIA_CONTENT_TYPE: MediaType.MUSIC,
ATTR_MEDIA_CONTENT_ID: media_url,
ATTR_MEDIA_EXTRA: {
"bypass_proxy": True,
},
},
blocking=True,
)
mock_async_create_proxy_url.assert_not_called()
media_args = mock_client.media_player_command.call_args.kwargs
assert media_args["media_url"] == media_url

0 comments on commit 9bbf9be

Please sign in to comment.