Skip to content

Commit

Permalink
Merge pull request #58 from bushvin/fix/expand_url
Browse files Browse the repository at this point in the history
Multiple fixes
  • Loading branch information
bushvin authored Nov 13, 2023
2 parents b6a2364 + c354d24 commit 843d7e3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 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.2.0",
"version": "2.2.1",
"zeroconf": [
{
"type": "_mopidy-http._tcp.local."
Expand Down
12 changes: 6 additions & 6 deletions custom_components/mopidy/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,18 +450,18 @@ def extra_state_attributes(self) -> dict[str, Any]:
"""Return entity specific state attributes"""
attributes: dict[str, Any] = {}

if self.speaker.queue.position is not None:
attributes["queue_position"] = self.speaker.queue.position

if self.speaker.queue.size is not None:
attributes["queue_size"] = self.speaker.queue.size

if self.speaker.consume_mode is not None:
attributes["consume_mode"] = self.speaker.consume_mode

if self.speaker.queue.current_track_extension is not None:
attributes["mopidy_extension"] = self.speaker.queue.current_track_extension

if self.speaker.queue.position is not None:
attributes["queue_position"] = self.speaker.queue.position

if self.speaker.queue.size is not None:
attributes["queue_size"] = self.speaker.queue.size

if self.speaker.snapshot_taken_at is not None:
attributes["snapshot_taken_at"] = self.speaker.snapshot_taken_at

Expand Down
49 changes: 28 additions & 21 deletions custom_components/mopidy/speaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ def expand_url(self, extension, url):
parsed_url = urlparse.urlparse(url)
if parsed_url.netloc == "":
url = f"{self.local_url_base}{url}"

# Force the browser to reload the image once per day
query = dict(urlparse.parse_qsl(parsed_url.query))
if query.get("t") is None:
if query.get("mopt") is None:
url_parts = list(urlparse.urlparse(url))
query["t"] = int(time.time() * 1000)
query["mopt"] = datetime.datetime.now().strftime("%Y%m%d")
url_parts[4] = urlencode(query)
url = urlparse.urlunparse(url_parts)

Expand Down Expand Up @@ -267,16 +269,16 @@ def parse_track_info(self, track, tlid=None, current=False):
self.__set_track_info(tlid, track_info)
if current:
self._current_track_tlid = tlid
self._current_track_uri = track_info.get("uri")
self._current_track_album_artist = track_info.get("album_artist")
self._current_track_album_name = track_info.get("album_name")
self._current_track_artist = track_info.get("artist")
self._current_track_duration = track_info.get("duration")
self._current_track_extension = track_info.get("source")
self._current_track_playlist_name = track_info.get("playlist_name")
self._current_track_title = track_info.get("title")
self._current_track_is_stream = track_info.get("is_stream")
self._current_track_number = track_info.get("number")
self._current_track_uri = self.queue[tlid].get("uri")
self._current_track_album_artist = self.queue[tlid].get("album_artist")
self._current_track_album_name = self.queue[tlid].get("album_name")
self._current_track_artist = self.queue[tlid].get("artist")
self._current_track_duration = self.queue[tlid].get("duration")
self._current_track_extension = self.queue[tlid].get("source")
self._current_track_playlist_name = self.queue[tlid].get("playlist_name")
self._current_track_title = self.queue[tlid].get("title")
self._current_track_is_stream = self.queue[tlid].get("is_stream")
self._current_track_number = self.queue[tlid].get("number")

return track_info

Expand Down Expand Up @@ -385,7 +387,7 @@ def update_queued_tracks(self, media_id, media_type, **kwargs):
"playlist_name": res.name,
"playlist_uri": res.uri,
}
self.__set_track_info(tl_track.tlid, **track_info)
self.__set_track_info(tl_track.tlid, track_info)

def update_queue_information(self, updater=None):
"""Get the Mopidy Instance queue information"""
Expand Down Expand Up @@ -478,6 +480,11 @@ def current_track_uri(self):
"""Return the mopidy uri of the current track"""
return self._current_track_uri

@property
def uri_list(self):
"""Return a list of uris of the current queue"""
return [ self.queue[x]["uri"] for x in self.queue ]

@property
def size(self):
"""Return the size of the current queue"""
Expand Down Expand Up @@ -549,6 +556,7 @@ def __init__(self,
self.entity = None
self.queue.api = self.api
self.library.api = self.api
self._attr_snapshot_at = None

def __clear(self):
"""Reset all Values"""
Expand All @@ -561,7 +569,6 @@ def __clear(self):
self._attr_state = None
self._attr_repeat = None
self._attr_shuffle = None
self._attr_snapshot_at = None
self._attr_is_available = False

def __connect(self):
Expand Down Expand Up @@ -802,14 +809,14 @@ def play_media(self, media_type, media_id, **kwargs):

elif enqueue == MediaPlayerEnqueue.NEXT:
# Add media uris to queue after current playing track
index = self.queue_position
index = self.queue.position
queued = self.queue_tracks(media_uris, at_position=index+1)
if self.state != MediaPlayerState.PLAYING:
self.media_play()

elif enqueue == MediaPlayerEnqueue.PLAY:
# Insert media uris before current playing track into queue and play first of new uris
index = self.queue_position
index = self.queue.position
queued = self.queue_tracks(media_uris, at_position=index)
self.media_play(index)

Expand Down Expand Up @@ -841,13 +848,13 @@ def restore_snapshot(self):
return
self.media_stop()
self.clear_queue()
self.queue_tracks(self.snapshot.get("tracklist",[]))
self.queue_tracks(self.snapshot.get("queue_list",[]))
self.set_volume(self.snapshot.get("volume"))
self.set_mute(self.snapshot.get("muted"))
if self.snapshot.get("state", MediaPlayerState.IDLE) in [MediaPlayerState.PLAYING, MediaPlayerState.PAUSED]:
current_tracks = self.api.tracklist.get_tl_tracks()
self.api.playback.play(
tlid=current_tracks[self.snapshot.get("tracklist_index")].tlid
tlid=current_tracks[self.snapshot.get("queue_index")].tlid
)

count = 0
Expand Down Expand Up @@ -930,13 +937,13 @@ def take_snapshot(self):
self.update()
self._attr_snapshot_at = dt_util.utcnow()
self.snapshot = {
"mediaposition": self.media.position,
"mediaposition": self.queue.current_track_position,
"muted": self.is_muted,
"repeat_mode": self.repeat,
"shuffled": self.is_shuffled,
"state": self.state,
"tracklist": self.tracklist_uris,
"tracklist_index": self.queue_position,
"queue_list": self.queue.uri_list,
"queue_index": self.queue.position,
"volume": self.volume_level,
}

Expand Down
10 changes: 10 additions & 0 deletions mopidy-CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ 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.2.1] - 2023-11-13

### Fixed

- expanding the url will add a timestamp based on the day instead of epoch, causing it to reload daily instead of every time the image is refreshed (which is every 10 seconds)
- correct snapshotting variables/methods
- alphabetize `extra_state_attributes` variables
- retrieve the correct current track information
- fix queue variables for `media_play`

## [2.2.0] - 2023-11-11

### Changed
Expand Down

0 comments on commit 843d7e3

Please sign in to comment.