From 083c9e250ef1c14064667a6dce24b1035c847f9f Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 5 Apr 2024 03:40:00 +0200 Subject: [PATCH] [music.mopidy] Better handling of client events. --- platypush/plugins/music/mopidy/_client.py | 16 ++++++++++++---- platypush/plugins/music/mopidy/_track.py | 12 ++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/platypush/plugins/music/mopidy/_client.py b/platypush/plugins/music/mopidy/_client.py index 580e5f333e..26616a330f 100644 --- a/platypush/plugins/music/mopidy/_client.py +++ b/platypush/plugins/music/mopidy/_client.py @@ -346,7 +346,7 @@ class MopidyClient(Thread): def on_volume_change(self, msg: dict, *_, **__): volume = msg.get('volume') - if volume is None: + if volume is None or volume == self._status.volume: return self._status.volume = volume @@ -354,7 +354,7 @@ class MopidyClient(Thread): def on_mute_change(self, msg: dict, *_, **__): mute = msg.get('mute') - if mute is None: + if mute is None or mute == self._status.mute: return self._status.mute = mute @@ -365,7 +365,11 @@ class MopidyClient(Thread): if position is None: return - self._status.time = position / 1000 + t = position / 1000 + if t == self._status.time: + return + + self._status.time = t self._post_event(SeekChangeEvent, position=self._status.time) def on_tracklist_change(self, *_, **__): @@ -402,7 +406,11 @@ class MopidyClient(Thread): return if msg.get('tl_track'): - track = self._status.track = MopidyTrack.parse(msg['tl_track']) + track = MopidyTrack.parse(msg['tl_track']) + is_new_track = track and track != self._status.track + self._status.track = track + if is_new_track: + self._post_event(NewPlayingTrackEvent) hndl = self._msg_handlers.get(event) if not hndl: diff --git a/platypush/plugins/music/mopidy/_track.py b/platypush/plugins/music/mopidy/_track.py index c359f2baf3..81aa0c9f0b 100644 --- a/platypush/plugins/music/mopidy/_track.py +++ b/platypush/plugins/music/mopidy/_track.py @@ -41,3 +41,15 @@ class MopidyTrack: Convert the Mopidy track to a dictionary. """ return dict(MopidyTrackSchema().dump(self)) + + def __eq__(self, other: object) -> bool: + if not isinstance(other, MopidyTrack): + return False + + return ( + self.uri == other.uri + and self.artist == other.artist + and self.title == other.title + and self.album == other.album + and self.time == other.time + )