Skip to content

Commit

Permalink
Add add_search_to_queue (#80)
Browse files Browse the repository at this point in the history
* Add add_search_to_queue

* Removed unneeded player_id
  • Loading branch information
andrewsayre authored Jan 11, 2025
1 parent dffd597 commit 1d49cc0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pyheos/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
SYSTEM_ERROR_CONTENT_AUTHORIZATION_ERROR: Final = -1232
SYSTEM_ERROR_ACCOUNT_PARAMETERS_INVALID: Final = -1239

# Search Crtieria Container IDs (keep discrete values as we do not control the list)
SEARCHED_TRACKS: Final = "SEARCHED_TRACKS-"

# Music Sources (keep discrete values as we do not control the list)
MUSIC_SOURCE_CONNECT: Final = 0 # TIDAL Connect // possibly Spotify Connect as well (?)
MUSIC_SOURCE_PANDORA: Final = 1
Expand Down
27 changes: 27 additions & 0 deletions pyheos/heos.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,33 @@ async def add_to_queue(
)
)

async def add_search_to_queue(
self,
player_id: int,
source_id: int,
search: str,
criteria_container_id: str = const.SEARCHED_TRACKS,
add_criteria: AddCriteriaType = AddCriteriaType.PLAY_NOW,
) -> None:
"""Add searched tracks to the queue of the specified player.
References:
4.4.11 Add Container to Queue with Options
Args:
player_id: The identifier of the player to add the search results.
source_id: The identifier of the source to search.
search: The search string.
criteria_container_id: the criteria container id prefix.
add_criteria: Determines how tracks are added to the queue. The default is AddCriteriaType.PLAY_NOW.
"""
await self.add_to_queue(
player_id=player_id,
source_id=source_id,
container_id=f"{criteria_container_id}{search}",
add_criteria=add_criteria,
)

async def rename_playlist(
self, source_id: int, container_id: str, new_name: str
) -> None:
Expand Down
27 changes: 27 additions & 0 deletions pyheos/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,33 @@ async def add_to_queue(
self.player_id, source_id, container_id, media_id, add_criteria
)

async def add_search_to_queue(
self,
source_id: int,
search: str,
criteria_container_id: str = const.SEARCHED_TRACKS,
add_criteria: AddCriteriaType = AddCriteriaType.PLAY_NOW,
) -> None:
"""Add searched tracks to the queue of the specified player.
References:
4.4.11 Add Container to Queue with Options
Args:
source_id: The identifier of the source to search.
search: The search string.
criteria_container_id: the criteria container id prefix.
add_criteria: Determines how tracks are added to the queue. The default is AddCriteriaType.PLAY_NOW.
"""
assert self.heos, "Heos instance not set"
await self.heos.add_search_to_queue(
player_id=self.player_id,
source_id=source_id,
search=search,
criteria_container_id=criteria_container_id,
add_criteria=add_criteria,
)

async def play_media(
self,
media: MediaItem,
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/browse.add_to_queue_search.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"heos": {"command": "browse/add_to_queue", "result": "success", "message": "pid={player_id}&sid=10&cid=SEARCHED_TRACKS-Tangerine Rays&aid=3"}}
23 changes: 22 additions & 1 deletion tests/test_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import pytest

from pyheos import command as c
from pyheos.const import INPUT_AUX_IN_1, MUSIC_SOURCE_DEEZER, MUSIC_SOURCE_PLAYLISTS
from pyheos.const import (
INPUT_AUX_IN_1,
MUSIC_SOURCE_DEEZER,
MUSIC_SOURCE_PLAYLISTS,
MUSIC_SOURCE_TIDAL,
SEARCHED_TRACKS,
)
from pyheos.media import MediaItem
from pyheos.player import HeosPlayer
from pyheos.types import (
Expand Down Expand Up @@ -441,6 +447,21 @@ async def test_add_to_queue(player: HeosPlayer) -> None:
)


@calls_command(
"browse.add_to_queue_search",
{
c.ATTR_PLAYER_ID: 1,
c.ATTR_SOURCE_ID: MUSIC_SOURCE_TIDAL,
c.ATTR_CONTAINER_ID: SEARCHED_TRACKS + "Tangerine Rays",
c.ATTR_ADD_CRITERIA_ID: AddCriteriaType.PLAY_NOW,
},
add_command_under_process=True,
)
async def test_add_search_to_queue(player: HeosPlayer) -> None:
"""Test adding a track to the queue."""
await player.add_search_to_queue(MUSIC_SOURCE_TIDAL, "Tangerine Rays")


@calls_command("player.get_now_playing_media_blank", {c.ATTR_PLAYER_ID: 1})
async def test_now_playing_media_unavailable(player: HeosPlayer) -> None:
"""Test edge case where now_playing_media returns an empty payload."""
Expand Down

0 comments on commit 1d49cc0

Please sign in to comment.