Add 'heartbeat' to MPD client, so we notice if we disconnect
This commit is contained in:
parent
68609f3d07
commit
c29f4b9b27
2 changed files with 14 additions and 3 deletions
|
@ -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()
|
||||||
|
|
||||||
|
|
|
@ -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: ...
|
||||||
|
|
Loading…
Reference in a new issue