From 5120b938ef1f050cb20336829a221e55b67c9fc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6tz?= Date: Sat, 28 Feb 2026 16:50:44 -0500 Subject: [PATCH] Optimize idle wake-ups --- src/mpd_now_playable/cli.py | 2 +- src/mpd_now_playable/mpd/listener.py | 10 ++++++++-- .../receivers/cocoa/convert/playback_to_media_item.py | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/mpd_now_playable/cli.py b/src/mpd_now_playable/cli.py index 34153c2..5bb5395 100644 --- a/src/mpd_now_playable/cli.py +++ b/src/mpd_now_playable/cli.py @@ -34,7 +34,7 @@ def main() -> None: asyncio.run( listen(config, listener, receivers), loop_factory=factory.make_loop, - debug=True, + debug=False, ) diff --git a/src/mpd_now_playable/mpd/listener.py b/src/mpd_now_playable/mpd/listener.py index ccdcdb8..ae162bd 100644 --- a/src/mpd_now_playable/mpd/listener.py +++ b/src/mpd_now_playable/mpd/listener.py @@ -3,7 +3,6 @@ from collections.abc import Iterable from mpd.asyncio import MPDClient from mpd.base import CommandError -from rich import print as rprint from yarl import URL from ..config.model import MpdConfig @@ -26,12 +25,14 @@ class MpdStateListener(Player): receivers: Iterable[Receiver] art_cache: MpdArtworkCache idle_count = 0 + last_playback: Playback | None def __init__(self, cache: URL | None = None) -> None: self.client = MPDClient() self.art_cache = ( MpdArtworkCache(self, cache) if cache else MpdArtworkCache(self) ) + self.last_playback = None async def start(self, conf: MpdConfig) -> None: self.config = conf @@ -53,6 +54,9 @@ class MpdStateListener(Player): async for subsystems in self.client.idle(self.WATCHED_SUBSYSTEMS): # If no subsystems actually changed, we don't need to update the receivers. if not subsystems: + # MPD/python-mpd2 can occasionally wake idle() without reporting a + # changed subsystem; avoid a hot loop if that happens repeatedly. + await asyncio.sleep(0.1) continue self.idle_count += 1 await self.update_receivers() @@ -76,7 +80,9 @@ class MpdStateListener(Player): state = MpdState(status, current, art) pb = to_playback(self.config, state) - rprint(pb) + if pb == self.last_playback: + return + self.last_playback = pb await self.update(pb) async def update(self, playback: Playback) -> None: diff --git a/src/mpd_now_playable/receivers/cocoa/convert/playback_to_media_item.py b/src/mpd_now_playable/receivers/cocoa/convert/playback_to_media_item.py index 7ac8a2b..822108b 100644 --- a/src/mpd_now_playable/receivers/cocoa/convert/playback_to_media_item.py +++ b/src/mpd_now_playable/receivers/cocoa/convert/playback_to_media_item.py @@ -19,7 +19,8 @@ def playback_to_media_item(playback: Playback) -> NSMutableDictionary: if song := playback.active_song: nowplaying_info = song_to_media_item(song) nowplaying_info[MPNowPlayingInfoPropertyPlaybackQueueCount] = playback.queue.length - nowplaying_info[MPNowPlayingInfoPropertyPlaybackQueueIndex] = playback.queue.current + if playback.queue.current is not None: + nowplaying_info[MPNowPlayingInfoPropertyPlaybackQueueIndex] = playback.queue.current return nowplaying_info