Skip to content

Commit a4ddaa6

Browse files
committed
Add unfavorite item action
1 parent 103eb3f commit a4ddaa6

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

custom_components/mass_queue/actions.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
)
1515
from homeassistant.exceptions import ServiceValidationError
1616
from homeassistant.helpers import entity_registry as er
17+
from music_assistant_models.errors import (
18+
InvalidCommand,
19+
MediaNotFoundError,
20+
)
1721

1822
from .const import (
1923
ATTR_COMMAND,
@@ -59,7 +63,6 @@
5963

6064
from . import MassQueueEntryData
6165

62-
6366
class MassQueueActions:
6467
"""Class to manage Music Assistant actions without passing `hass` and `mass_client` each time."""
6568

@@ -253,7 +256,24 @@ async def move_queue_item_next(self, call: ServiceCall) -> ServiceResponse:
253256
queue_id,
254257
queue_item_id,
255258
)
256-
259+
async def unfavorite_item(self, call: ServiceCall) -> ServiceResponse:
260+
"""Unfavorites currently playing item in queue."""
261+
entity_id = call.data[ATTR_PLAYER_ENTITY]
262+
attrs = self._hass.states.get(entity_id).attributes
263+
content_id = attrs.get(ATTR_MEDIA_CONTENT_ID)
264+
if not content_id:
265+
msg = f"Cannot find media with content id {content_id}"
266+
raise MediaNotFoundError(msg)
267+
provider = content_id.split("://")[0]
268+
if provider != "library":
269+
msg = f"Unfavorite can only apply to library media items, not from provider {provider}"
270+
raise InvalidCommand(msg)
271+
item_id = str(content_id.split("/")[-1])
272+
await self._client.send_command(
273+
"music/favorites/remove_item",
274+
media_type="track",
275+
library_item_id=item_id,
276+
)
257277

258278
@callback
259279
def get_music_assistant_client(

custom_components/mass_queue/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
SERVICE_MOVE_QUEUE_ITEM_DOWN = "move_queue_item_down"
1212
SERVICE_MOVE_QUEUE_ITEM_NEXT = "move_queue_item_next"
1313
SERVICE_SEND_COMMAND = "send_command"
14-
14+
SERVICE_UNFAVORITE_CURRENT_ITEM = "unfavorite_current_item"
1515
ATTR_QUEUE_ID = "active_queue"
1616
ATTR_CONFIG_ENTRY_ID = "config_entry_id"
1717
ATTR_LIMIT = "limit"

custom_components/mass_queue/schemas.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,9 @@
9292
vol.Required(ATTR_CONFIG_ENTRY_ID): str,
9393
},
9494
)
95+
96+
UNFAVORITE_CURRENT_ITEM_SERVICE_SCHEMA = vol.Schema(
97+
{
98+
vol.Required(ATTR_PLAYER_ENTITY): str,
99+
},
100+
)

custom_components/mass_queue/services.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
SERVICE_PLAY_QUEUE_ITEM,
2626
SERVICE_REMOVE_QUEUE_ITEM,
2727
SERVICE_SEND_COMMAND,
28+
SERVICE_UNFAVORITE_CURRENT_ITEM,
2829
)
2930
from .schemas import (
3031
MOVE_QUEUE_ITEM_DOWN_SERVICE_SCHEMA,
@@ -34,6 +35,7 @@
3435
QUEUE_ITEMS_SERVICE_SCHEMA,
3536
REMOVE_QUEUE_ITEM_SERVICE_SCHEMA,
3637
SEND_COMMAND_SERVICE_SCHEMA,
38+
UNFAVORITE_CURRENT_ITEM_SERVICE_SCHEMA,
3739
)
3840

3941
if TYPE_CHECKING:
@@ -94,6 +96,13 @@ def register_actions(hass) -> None:
9496
schema=SEND_COMMAND_SERVICE_SCHEMA,
9597
supports_response=SupportsResponse.OPTIONAL,
9698
)
99+
hass.services.async_register(
100+
DOMAIN,
101+
SERVICE_UNFAVORITE_CURRENT_ITEM,
102+
unfavorite_current_item,
103+
schema=UNFAVORITE_CURRENT_ITEM_SERVICE_SCHEMA,
104+
supports_response=SupportsResponse.NONE,
105+
)
97106

98107

99108
def _get_mass_entity_config_entry_id(hass, entity_id):
@@ -204,3 +213,10 @@ async def send_command(call: ServiceCall):
204213
entry = hass.config_entries.async_get_entry(entry_id)
205214
actions = entry.runtime_data.actions
206215
return await actions.send_command(call)
216+
217+
async def unfavorite_current_item(call: ServiceCall):
218+
"""Service wrapper to unfavorite currently playing item."""
219+
entity_id = call.data[ATTR_PLAYER_ENTITY]
220+
hass = call.hass
221+
actions = get_entity_actions_controller(hass, entity_id)
222+
await actions.unfavorite_item(call)

custom_components/mass_queue/services.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,14 @@ send_command:
147147
selector:
148148
config_entry:
149149
integration: mass_queue
150+
151+
unfavorite_current_item:
152+
fields:
153+
entity:
154+
name: Entity
155+
description: Music Assistant Media Player Entity
156+
required: true
157+
selector:
158+
entity:
159+
domain: media_player
160+
integration: music_assistant

0 commit comments

Comments
 (0)