Do not make the favorite button unavailable when no content playing on a Music Assistant player (#147579)

This commit is contained in:
Marcel van der Veldt 2025-06-26 11:49:06 +02:00 committed by Franck Nijhof
parent 398dd3ae46
commit 153e1e43e8
No known key found for this signature in database
GPG Key ID: AB33ADACE7101952
3 changed files with 41 additions and 9 deletions

View File

@ -41,12 +41,6 @@ class MusicAssistantFavoriteButton(MusicAssistantEntity, ButtonEntity):
translation_key="favorite_now_playing", translation_key="favorite_now_playing",
) )
@property
def available(self) -> bool:
"""Return availability of entity."""
# mark the button as unavailable if the player has no current media item
return super().available and self.player.current_media is not None
@catch_musicassistant_error @catch_musicassistant_error
async def async_press(self) -> None: async def async_press(self) -> None:
"""Handle the button press command.""" """Handle the button press command."""

View File

@ -140,6 +140,6 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': 'unavailable', 'state': 'unknown',
}) })
# --- # ---

View File

@ -2,14 +2,20 @@
from unittest.mock import MagicMock, call from unittest.mock import MagicMock, call
from music_assistant_models.enums import EventType
import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, HomeAssistantError
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .common import setup_integration_from_fixtures, snapshot_music_assistant_entities from .common import (
setup_integration_from_fixtures,
snapshot_music_assistant_entities,
trigger_subscription_callback,
)
async def test_button_entities( async def test_button_entities(
@ -46,3 +52,35 @@ async def test_button_press_action(
"music/favorites/add_item", "music/favorites/add_item",
item="spotify://track/5d95dc5be77e4f7eb4939f62cfef527b", item="spotify://track/5d95dc5be77e4f7eb4939f62cfef527b",
) )
# test again without current_media
mass_player_id = "00:00:00:00:00:02"
music_assistant_client.players._players[mass_player_id].current_media = None
await trigger_subscription_callback(
hass, music_assistant_client, EventType.PLAYER_CONFIG_UPDATED, mass_player_id
)
with pytest.raises(HomeAssistantError, match="No current item to add to favorites"):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{
ATTR_ENTITY_ID: entity_id,
},
blocking=True,
)
# test again without active source
mass_player_id = "00:00:00:00:00:02"
music_assistant_client.players._players[mass_player_id].active_source = None
await trigger_subscription_callback(
hass, music_assistant_client, EventType.PLAYER_CONFIG_UPDATED, mass_player_id
)
with pytest.raises(HomeAssistantError, match="Player has no active source"):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{
ATTR_ENTITY_ID: entity_id,
},
blocking=True,
)