diff --git a/homeassistant/components/music_assistant/button.py b/homeassistant/components/music_assistant/button.py index 7969954e443..445ef2c3e98 100644 --- a/homeassistant/components/music_assistant/button.py +++ b/homeassistant/components/music_assistant/button.py @@ -41,12 +41,6 @@ class MusicAssistantFavoriteButton(MusicAssistantEntity, ButtonEntity): 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 async def async_press(self) -> None: """Handle the button press command.""" diff --git a/tests/components/music_assistant/snapshots/test_button.ambr b/tests/components/music_assistant/snapshots/test_button.ambr index ac9e4c660f6..d064916e044 100644 --- a/tests/components/music_assistant/snapshots/test_button.ambr +++ b/tests/components/music_assistant/snapshots/test_button.ambr @@ -140,6 +140,6 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'unknown', }) # --- diff --git a/tests/components/music_assistant/test_button.py b/tests/components/music_assistant/test_button.py index 8a1a4b0e241..5a326b1d8ea 100644 --- a/tests/components/music_assistant/test_button.py +++ b/tests/components/music_assistant/test_button.py @@ -2,14 +2,20 @@ from unittest.mock import MagicMock, call +from music_assistant_models.enums import EventType +import pytest from syrupy.assertion import SnapshotAssertion from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS 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 .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( @@ -46,3 +52,35 @@ async def test_button_press_action( "music/favorites/add_item", 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, + )