mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Use entity unique id for ESPHome media player formats (#146318)
This commit is contained in:
parent
ce739fd9b6
commit
0c5b7401b9
@ -226,6 +226,7 @@ class EsphomeEntity(EsphomeBaseEntity, Generic[_InfoT, _StateT]):
|
||||
_static_info: _InfoT
|
||||
_state: _StateT
|
||||
_has_state: bool
|
||||
unique_id: str
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
@ -78,7 +78,7 @@ class EsphomeMediaPlayer(
|
||||
if self._static_info.supports_pause:
|
||||
flags |= MediaPlayerEntityFeature.PAUSE | MediaPlayerEntityFeature.PLAY
|
||||
self._attr_supported_features = flags
|
||||
self._entry_data.media_player_formats[static_info.unique_id] = cast(
|
||||
self._entry_data.media_player_formats[self.unique_id] = cast(
|
||||
MediaPlayerInfo, static_info
|
||||
).supported_formats
|
||||
|
||||
@ -114,9 +114,8 @@ class EsphomeMediaPlayer(
|
||||
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)
|
||||
self._entry_data.media_player_formats.get(self.unique_id)
|
||||
)
|
||||
|
||||
if (
|
||||
@ -139,7 +138,7 @@ class EsphomeMediaPlayer(
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Handle entity being removed."""
|
||||
await super().async_will_remove_from_hass()
|
||||
self._entry_data.media_player_formats.pop(self.entity_id, None)
|
||||
self._entry_data.media_player_formats.pop(self.unique_id, None)
|
||||
|
||||
def _get_proxy_url(
|
||||
self,
|
||||
|
@ -429,3 +429,105 @@ async def test_media_player_proxy(
|
||||
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
|
||||
|
||||
|
||||
async def test_media_player_formats_reload_preserves_data(
|
||||
hass: HomeAssistant,
|
||||
mock_client: APIClient,
|
||||
mock_esphome_device: MockESPHomeDeviceType,
|
||||
) -> None:
|
||||
"""Test that media player formats are properly managed on reload."""
|
||||
# Create a media player with supported formats
|
||||
supported_formats = [
|
||||
MediaPlayerSupportedFormat(
|
||||
format="mp3",
|
||||
sample_rate=48000,
|
||||
num_channels=2,
|
||||
purpose=MediaPlayerFormatPurpose.DEFAULT,
|
||||
),
|
||||
MediaPlayerSupportedFormat(
|
||||
format="wav",
|
||||
sample_rate=16000,
|
||||
num_channels=1,
|
||||
purpose=MediaPlayerFormatPurpose.ANNOUNCEMENT,
|
||||
sample_bytes=2,
|
||||
),
|
||||
]
|
||||
|
||||
mock_device = await mock_esphome_device(
|
||||
mock_client=mock_client,
|
||||
entity_info=[
|
||||
MediaPlayerInfo(
|
||||
object_id="test_media_player",
|
||||
key=1,
|
||||
name="Test Media Player",
|
||||
unique_id="test_unique_id",
|
||||
supports_pause=True,
|
||||
supported_formats=supported_formats,
|
||||
)
|
||||
],
|
||||
states=[
|
||||
MediaPlayerEntityState(
|
||||
key=1, volume=50, muted=False, state=MediaPlayerState.IDLE
|
||||
)
|
||||
],
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify entity was created
|
||||
state = hass.states.get("media_player.test_test_media_player")
|
||||
assert state is not None
|
||||
assert state.state == "idle"
|
||||
|
||||
# Test that play_media works with proxy URL (which requires formats to be stored)
|
||||
media_url = "http://127.0.0.1/test.mp3"
|
||||
|
||||
await hass.services.async_call(
|
||||
MEDIA_PLAYER_DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
{
|
||||
ATTR_ENTITY_ID: "media_player.test_test_media_player",
|
||||
ATTR_MEDIA_CONTENT_TYPE: MediaType.MUSIC,
|
||||
ATTR_MEDIA_CONTENT_ID: media_url,
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
# Verify the API was called with a proxy URL (contains /api/esphome/ffmpeg_proxy/)
|
||||
mock_client.media_player_command.assert_called_once()
|
||||
call_args = mock_client.media_player_command.call_args
|
||||
assert "/api/esphome/ffmpeg_proxy/" in call_args.kwargs["media_url"]
|
||||
assert ".mp3" in call_args.kwargs["media_url"] # Should use mp3 format for default
|
||||
assert call_args.kwargs["announcement"] is None
|
||||
|
||||
mock_client.media_player_command.reset_mock()
|
||||
|
||||
# Reload the integration
|
||||
await hass.config_entries.async_reload(mock_device.entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify entity still exists after reload
|
||||
state = hass.states.get("media_player.test_test_media_player")
|
||||
assert state is not None
|
||||
|
||||
# Test that play_media still works after reload with announcement
|
||||
await hass.services.async_call(
|
||||
MEDIA_PLAYER_DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
{
|
||||
ATTR_ENTITY_ID: "media_player.test_test_media_player",
|
||||
ATTR_MEDIA_CONTENT_TYPE: MediaType.MUSIC,
|
||||
ATTR_MEDIA_CONTENT_ID: media_url,
|
||||
ATTR_MEDIA_ANNOUNCE: True,
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
# Verify the API was called with a proxy URL using wav format for announcements
|
||||
mock_client.media_player_command.assert_called_once()
|
||||
call_args = mock_client.media_player_command.call_args
|
||||
assert "/api/esphome/ffmpeg_proxy/" in call_args.kwargs["media_url"]
|
||||
assert (
|
||||
".wav" in call_args.kwargs["media_url"]
|
||||
) # Should use wav format for announcement
|
||||
assert call_args.kwargs["announcement"] is True
|
||||
|
Loading…
x
Reference in New Issue
Block a user