Skip to content

Commit

Permalink
Merge pull request #46 from bushvin/fix/play_media
Browse files Browse the repository at this point in the history
Fix `media_player.play_media` service 'enqueue.play` behaviour
  • Loading branch information
bushvin authored Nov 5, 2023
2 parents b197151 + 5d80144 commit b621978
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion custom_components/mopidy/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"requirements": [
"mopidyapi>=1.0.0"
],
"version": "2.0.2",
"version": "2.0.3",
"zeroconf": [
{
"type": "_mopidy-http._tcp.local."
Expand Down
40 changes: 18 additions & 22 deletions custom_components/mopidy/speaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,6 @@ def uri(self):
"""Return the URI of the current track"""
return self._attr_track_uri

# class MopidyQueue:
# """Mopidy doesn't provide information about the playlist a song which is being played"""
# api: MopidyAPI | None = None

# _queue: list | None = None

# def __init__(self):
# self._queue = []



class MopidySpeaker:

hass: HomeAssistant | None = None
Expand Down Expand Up @@ -450,10 +439,6 @@ def update(self):
_LOGGER.debug(str(error))
return

# if not self._attr_is_available:
# _LOGGER.debug("Waiting for network connectivity to be established")
# return

self._attr_software_version = self.api.rpc_call("core.get_version")
self._attr_supported_uri_schemes = self.api.rpc_call("core.get_uri_schemes")
self._attr_consume_mode = self.api.tracklist.get_consume()
Expand Down Expand Up @@ -519,9 +504,17 @@ def media_pause(self):

def media_play(self, index=None):
"""Play the current media"""
if isinstance(index, int):
self.api.playback.play(tlid=index)
else:
_LOGGER.debug("index: %s", index)
try:
int(index)
current_tracks = self.api.tracklist.get_tl_tracks()
self.api.playback.play(
tlid=current_tracks[index].tlid
)

except Exception as error:
_LOGGER.error("The specified index %s could not be resolved", index)
_LOGGER.debug(str(error))
self.api.playback.play()

def media_previous_track(self):
Expand Down Expand Up @@ -552,23 +545,24 @@ def play_media(self, media_type, media_id, **kwargs):
media_uris = [ x.uri for x in self.library.browse(media_id)]

if enqueue == MediaPlayerEnqueue.ADD:
# Add media uris to end of the queue
self.queue_tracks(media_uris)
self.media_play()

elif enqueue == MediaPlayerEnqueue.NEXT:
# Add media uris to queue after current playing track
index = self.queue_position
self.queue_tracks(media_uris, at_position=index+1)

elif enqueue == MediaPlayerEnqueue.PLAY:
# tracks = self.tracklist_uris
# Insert media uris before current playing track into queue and play first of new uris
index = self.queue_position
# new_media_uris = tracks[:index] + media_uris + tracks[index+1:]
self.clear_queue()
# self.queue_tracks(new_media_uris)
self.queue_tracks(media_uris, at_position=index)
self.media_play(index)

elif enqueue == MediaPlayerEnqueue.REPLACE:
# clear queue and replace with media uris
self.media_stop()
self.clear_queue()
self.queue_tracks(media_uris)
self.media_play()
Expand All @@ -581,6 +575,8 @@ def queue_tracks(self, uris, at_position=None):
"""Queue tracks"""
if len(uris) > 0:
self.api.tracklist.add(uris=uris, at_position=at_position)
self._attr_tracklist = self.api.tracklist.get_tracks()
self._attr_queue_position = self.api.tracklist.index()

def restore_snapshot(self):
"""Restore a snapshot"""
Expand Down
10 changes: 9 additions & 1 deletion mopidy-CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.3] - 2023-11-05

### Fixed

- fix `media_player.play_media` service `enqueue.play` behaviour

## [2.0.2] - 2023-11-02

### Fixed wrong varname for youtube (#40)
### Fixed

- wrong varname for youtube (#40)

## [2.0.1] - 2023-10-31

Expand Down

0 comments on commit b621978

Please sign in to comment.