Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Update media_player.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nagyrobi committed Jun 2, 2022
1 parent c4247de commit 2509459
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions custom_components/linkplay/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,18 +1370,6 @@ async def async_play_media(self, media_type, media_id, **kwargs):
_LOGGER.debug("Trying to play media. Device: %s, Media_type: %s, Media_id: %s", self.entity_id, media_type, media_id)
if not self._slave_mode:

if kwargs.get(ATTR_MEDIA_ANNOUNCE):
self._announce = True
await self.async_snapshot(True)

volume = int(self._volume) + int(DEFAULT_ANNOUNCE_VOLUME_INCREASE)
if volume > 100:
volume = 100

value = await self.call_linkplay_httpapi("setPlayerCmd:vol:{0}".format(str(volume)), None)
if value == "OK":
self._volume = volume

if not (media_type in [MEDIA_TYPE_MUSIC, MEDIA_TYPE_URL, MEDIA_TYPE_TRACK] or media_source.is_media_source_id(media_id)):
_LOGGER.warning("For: %s Invalid media type %s. Only %s and %s is supported", self._name, media_type, MEDIA_TYPE_MUSIC, MEDIA_TYPE_URL)
await self.async_media_stop()
Expand All @@ -1391,6 +1379,19 @@ async def async_play_media(self, media_type, media_id, **kwargs):
self._playing_mediabrowser = False
self._nometa = False

if kwargs.get(ATTR_MEDIA_ANNOUNCE):
self._announce = True
self._playing_tts = True
await self.async_snapshot(False)
volume = int(self._volume) + int(DEFAULT_ANNOUNCE_VOLUME_INCREASE)
if volume > 100:
volume = 100
self._playing_mediabrowser = False
self._playing_mass = False
else:
self._playing_tts = False
self._announce = False

if media_source.is_media_source_id(media_id):
play_item = await media_source.async_resolve_media(self.hass, media_id, self.entity_id)
if media_id.find('radio_browser') != -1: # radios are an exception, be treated by server redirect checker and icecast metadata parser
Expand Down Expand Up @@ -1475,14 +1476,6 @@ async def async_play_media(self, media_type, media_id, **kwargs):
return False

self._state = STATE_PLAYING
if media_id.find('tts_proxy') != -1:
#_LOGGER.debug("Setting TTS: %s, %s", self.entity_id, self._name)
self._playing_tts = True
self._playing_mediabrowser = False
self._playing_mass = False
self._playing_stream = False
else:
self._playing_tts = False
self._media_title = None
self._media_artist = None
self._media_album = None
Expand All @@ -1502,6 +1495,13 @@ async def async_play_media(self, media_type, media_id, **kwargs):
self._media_uri = None
self._media_uri_final = None
self._wait_for_mcu = 0.4
if self._announce:
self._playing_stream = False
await asyncio.sleep(.6)
value = await self.call_linkplay_httpapi("setPlayerCmd:vol:{0}".format(str(volume)), None)
if value == "OK":
self._volume = volume

return True

else:
Expand Down Expand Up @@ -2086,7 +2086,7 @@ class InterceptedHTTPResponse():
#_LOGGER.debug('For: %s stated media_artist: %s', self._name, self._media_artist)

async def async_detect_stream_url_redirection(self, uri):
if uri.find('tts_proxy') != -1: # skip redirect check for local TTS streams
if uri.find('tts_proxy') != -1 or self._announce: # skip redirect check for local TTS streams
return uri
_LOGGER.debug('For: %s detect URI redirect-from: %s', self._name, uri)
redirect_detect = True
Expand Down Expand Up @@ -2603,9 +2603,11 @@ async def async_snapshot(self, switchinput):
self._snap_volume = 0
else:
self._snap_volume = int(self._volume)
if self._fwvercheck(self._fw_ver) >= self._fwvercheck(FW_SLOW_STREAMS):
await self.call_linkplay_httpapi("setPlayerCmd:pause", None)
await self.call_linkplay_httpapi("setPlayerCmd:stop", None)
if self._playing_stream:
if self._fwvercheck(self._fw_ver) >= self._fwvercheck(FW_SLOW_STREAMS):
await self.call_linkplay_httpapi("setPlayerCmd:pause", None)
else:
await self.call_linkplay_httpapi("setPlayerCmd:stop", None)
else:
return
#await self._master.async_snapshot(switchinput)
Expand All @@ -2621,12 +2623,6 @@ async def async_restore(self):
if self._snap_state != STATE_UNKNOWN:
self._state = self._snap_state

if self._snap_volume != 0:
await self.call_linkplay_httpapi("setPlayerCmd:vol:{0}".format(str(self._snap_volume)), None)
self._snap_volume = 0

# await asyncio.sleep(.6)

self._playing_tts = False
self._announce = False
self._playhead_position = self._snap_playhead_position
Expand All @@ -2642,6 +2638,8 @@ async def async_restore(self):
elif self._snap_source != "Network":
self._snapshot_active = False
await self.async_select_source(self._snap_source)
if self._snap_uri is None:
await asyncio.sleep(.6)
self._snap_source = None

elif self._snap_uri is not None:
Expand All @@ -2654,6 +2652,10 @@ async def async_restore(self):
self._snapshot_active = False
self._snap_uri = None

if self._snap_volume != 0:
await self.call_linkplay_httpapi("setPlayerCmd:vol:{0}".format(str(self._snap_volume)), None)
self._snap_volume = 0

if self._snap_state in [STATE_PLAYING, STATE_PAUSED]:
await asyncio.sleep(0.5)
if self._snap_seek and self._snap_playhead_position > 0:
Expand Down

0 comments on commit 2509459

Please sign in to comment.