Show internet radio station if no artist is available in Cambridge Audio (#140716)

* Add media_channel property to cambridge audio

* Return channel instead of artist when playing internet radio to mimick behaviour of CXN100 and StreamMagic app

* Add test for media_artist attribute

* Add test that media_artist is not set in certain cases

* Update homeassistant/components/cambridge_audio/media_player.py

Co-authored-by: Noah Husby <32528627+noahhusby@users.noreply.github.com>

---------

Co-authored-by: Noah Husby <32528627+noahhusby@users.noreply.github.com>
pull/141641/head
Solmath 2025-03-28 09:49:20 +01:00 committed by GitHub
parent 078be3b8df
commit 267a80e70c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 0 deletions

View File

@ -142,6 +142,12 @@ class CambridgeAudioDevice(CambridgeAudioEntity, MediaPlayerEntity):
@property
def media_artist(self) -> str | None:
"""Artist of current playing media, music track only."""
if (
not self.client.play_state.metadata.artist
and self.client.state.source == "IR"
):
# Return channel instead of artist when playing internet radio
return self.client.play_state.metadata.station
return self.client.play_state.metadata.artist
@property
@ -169,6 +175,11 @@ class CambridgeAudioDevice(CambridgeAudioEntity, MediaPlayerEntity):
"""Last time the media position was updated."""
return self.client.position_last_updated
@property
def media_channel(self) -> str | None:
"""Channel currently playing."""
return self.client.play_state.metadata.station
@property
def is_volume_muted(self) -> bool | None:
"""Volume mute status."""

View File

@ -10,6 +10,7 @@ from aiostreammagic import (
import pytest
from homeassistant.components.media_player import (
ATTR_MEDIA_ARTIST,
ATTR_MEDIA_CONTENT_ID,
ATTR_MEDIA_CONTENT_TYPE,
ATTR_MEDIA_REPEAT,
@ -489,3 +490,41 @@ async def test_play_media_unknown_type(
},
blocking=True,
)
@pytest.mark.parametrize(
("source_id", "artist", "station", "display"),
[
("MEDIA_PLAYER", "Metallica", None, "Metallica"),
("USB_AUDIO", "Iron Maiden", "Radio BOB!", "Iron Maiden"),
("IR", "In Flames", "Radio BOB!", "In Flames"),
("IR", None, "Radio BOB!", "Radio BOB!"),
("IR", None, None, None),
("MEDIA_PLAYER", None, "Radio BOB!", None),
],
)
async def test_media_artist(
hass: HomeAssistant,
mock_stream_magic_client: AsyncMock,
mock_config_entry: MockConfigEntry,
source_id: str,
artist: str,
station: str,
display: str,
) -> None:
"""Test media player state."""
await setup_integration(hass, mock_config_entry)
mock_stream_magic_client.play_state.metadata.artist = artist
mock_stream_magic_client.play_state.metadata.station = station
mock_stream_magic_client.state.source = source_id
await mock_state_update(mock_stream_magic_client)
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)
if (artist is None and source_id != "IR") or (
source_id == "IR" and station is None
):
assert ATTR_MEDIA_ARTIST not in state.attributes
else:
assert state.attributes[ATTR_MEDIA_ARTIST] == display