Add 'heartbeat' to MPD client, so we notice if we disconnect

This commit is contained in:
Danielle McLean 2024-07-29 10:55:27 +10:00
parent 68609f3d07
commit c29f4b9b27
Signed by: 00dani
GPG key ID: 6854781A0488421C
2 changed files with 14 additions and 3 deletions

View file

@ -11,6 +11,7 @@ from ..playback import Playback
from ..playback.state import PlaybackState from ..playback.state import PlaybackState
from ..player import Player from ..player import Player
from ..song_receiver import Receiver from ..song_receiver import Receiver
from ..tools.asyncio import run_background_task
from .artwork_cache import MpdArtworkCache from .artwork_cache import MpdArtworkCache
from .convert.to_playback import to_playback from .convert.to_playback import to_playback
from .types import MpdState from .types import MpdState
@ -37,16 +38,25 @@ class MpdStateListener(Player):
print("Authorising to MPD with your password...") print("Authorising to MPD with your password...")
await self.client.password(conf.password.get_secret_value()) await self.client.password(conf.password.get_secret_value())
print(f"Connected to MPD v{self.client.mpd_version}") print(f"Connected to MPD v{self.client.mpd_version}")
run_background_task(self.heartbeat())
async def heartbeat(self) -> None:
while True:
await self.client.ping()
await asyncio.sleep(10)
async def refresh(self) -> None: async def refresh(self) -> None:
await self.update_receivers() await self.update_receivers()
async def loop(self, receivers: Iterable[Receiver]) -> None: async def loop(self, receivers: Iterable[Receiver]) -> None:
self.receivers = receivers self.receivers = receivers
# notify our receivers of the initial state MPD is in when this script loads up. # Notify our receivers of the initial state MPD is in when this script loads up.
await self.update_receivers() await self.update_receivers()
# then wait for stuff to change in MPD. :) # And then wait for stuff to change in MPD. :)
async for _ in self.client.idle(): async for subsystems in self.client.idle():
# If no subsystems actually changed, we don't need to update the receivers.
if not subsystems:
continue
self.idle_count += 1 self.idle_count += 1
await self.update_receivers() await self.update_receivers()

View file

@ -9,6 +9,7 @@ class MPDClient(MPDClientBase):
def __init__(self) -> None: ... def __init__(self) -> None: ...
async def connect(self, host: str, port: int = ...) -> None: ... async def connect(self, host: str, port: int = ...) -> None: ...
async def ping(self) -> None: ...
async def password(self, password: str) -> None: ... async def password(self, password: str) -> None: ...
def idle(self, subsystems: Sequence[str] = ...) -> AsyncIterator[Sequence[str]]: ... def idle(self, subsystems: Sequence[str] = ...) -> AsyncIterator[Sequence[str]]: ...
async def status(self) -> types.StatusResponse: ... async def status(self) -> types.StatusResponse: ...