diff --git a/custom_components/oasis_mini/media_player.py b/custom_components/oasis_mini/media_player.py index 92bc306..0b163e4 100644 --- a/custom_components/oasis_mini/media_player.py +++ b/custom_components/oasis_mini/media_player.py @@ -340,9 +340,7 @@ class OasisDeviceMediaPlayerEntity(OasisDeviceEntity, MediaPlayerEntity): return if enqueue == MediaPlayerEnqueue.REPLACE: - await device.async_stop() - await device.async_set_playlist(track_ids) - await device.async_play() + await device.async_set_playlist(track_ids, start_playing=True) return insert_at = (device.playlist_index or 0) + 1 diff --git a/custom_components/oasis_mini/pyoasiscontrol/device.py b/custom_components/oasis_mini/pyoasiscontrol/device.py index bb02fde..65bd6c2 100644 --- a/custom_components/oasis_mini/pyoasiscontrol/device.py +++ b/custom_components/oasis_mini/pyoasiscontrol/device.py @@ -11,6 +11,7 @@ from .const import ( LED_EFFECTS, STATUS_CODE_MAP, STATUS_ERROR, + STATUS_PLAYING, STATUS_SLEEPING, TRACKS, ) @@ -645,21 +646,33 @@ class OasisDevice: client = self._require_client() await client.async_send_add_joblist_command(self, tracks) - async def async_set_playlist(self, playlist: int | Iterable[int]) -> None: + async def async_set_playlist( + self, playlist: int | Iterable[int], *, start_playing: bool | None = None + ) -> None: """ Set the device's playlist to the provided track or tracks. - Accepts a single track ID or an iterable of track IDs and replaces the device's playlist by sending the corresponding command to the attached client. + Accepts a single track ID or an iterable of track IDs, stops the device, + replaces the playlist, and resumes playback based on the `start_playing` + parameter or, if unspecified, the device's prior playing state. Parameters: - playlist (int | Iterable[int]): A single track ID or an iterable of track IDs to set as the new playlist. + playlist (int | Iterable[int]): + A single track ID or an iterable of track IDs to set as the new playlist. + start_playing (bool | None, keyword-only): + Whether to start playback after updating the playlist. If None (default), + playback will resume only if the device was previously playing and the + new playlist is non-empty. """ - if isinstance(playlist, int): - playlist_list = [playlist] - else: - playlist_list = list(playlist) + playlist = [playlist] if isinstance(playlist, int) else list(playlist) + if start_playing is None: + start_playing = self.status_code == STATUS_PLAYING + client = self._require_client() - await client.async_send_set_playlist_command(self, playlist_list) + await client.async_send_stop_command(self) # needed before replacing playlist + await client.async_send_set_playlist_command(self, playlist) + if start_playing and playlist: + await client.async_send_play_command(self) async def async_set_repeat_playlist(self, repeat: bool) -> None: """