Cleanup async_announce in forked_daapd (#78457)

pull/78462/head
uvjustin 2022-09-14 20:40:20 +08:00 committed by GitHub
parent 003d160a96
commit ae865a07b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 22 deletions

View File

@ -672,7 +672,8 @@ class ForkedDaapdMaster(MediaPlayerEntity):
else:
_LOGGER.debug("Media type '%s' not supported", media_type)
async def _async_announce(self, media_id: str):
async def _async_announce(self, media_id: str) -> None:
"""Play a URI."""
saved_state = self.state # save play state
saved_mute = self.is_volume_muted
sleep_future = asyncio.create_task(
@ -696,14 +697,14 @@ class ForkedDaapdMaster(MediaPlayerEntity):
async with async_timeout.timeout(TTS_TIMEOUT):
await self._tts_playing_event.wait()
# we have started TTS, now wait for completion
await asyncio.sleep(
self._queue["items"][0]["length_ms"]
/ 1000 # player may not have updated yet so grab length from queue
+ self._tts_pause_time
)
except asyncio.TimeoutError:
self._tts_requested = False
_LOGGER.warning("TTS request timed out")
await asyncio.sleep(
self._queue["items"][0]["length_ms"]
/ 1000 # player may not have updated yet so grab length from queue
+ self._tts_pause_time
)
self._tts_playing_event.clear()
# TTS done, return to normal
await self.async_turn_on() # restore outputs and volumes
@ -715,22 +716,22 @@ class ForkedDaapdMaster(MediaPlayerEntity):
)
if saved_state == MediaPlayerState.PLAYING:
await self.async_media_play()
else: # restore stashed queue
if saved_queue:
uris = ""
for item in saved_queue["items"]:
uris += item["uri"] + ","
await self._api.add_to_queue(
uris=uris,
playback="start",
playback_from_position=saved_queue_position,
clear=True,
)
await self._api.seek(position_ms=saved_song_position)
if saved_state == MediaPlayerState.PAUSED:
await self.async_media_pause()
elif saved_state != MediaPlayerState.PLAYING:
await self.async_media_stop()
return
if not saved_queue:
return
# Restore stashed queue
await self._api.add_to_queue(
uris=",".join(item["uri"] for item in saved_queue["items"]),
playback="start",
playback_from_position=saved_queue_position,
clear=True,
)
await self._api.seek(position_ms=saved_song_position)
if saved_state == MediaPlayerState.PAUSED:
await self.async_media_pause()
return
if saved_state != MediaPlayerState.PLAYING:
await self.async_media_stop()
async def async_select_source(self, source: str) -> None:
"""Change source.