diff --git a/README.md b/README.md index 3a1fc57..be7efa2 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,47 @@ If parameter `notify: False` is omitted, results will appear in Lovelace UI's le ## Automation examples +Intrerupt playback of a source, incrase volume by 15%, say a TTS message and resume playback when TTS finishes: +```yaml +- alias: 'Notify by TTS that Hanna has arrived Sound Room 1' + id: tts_mary_home_sound_room1 + trigger: + - platform: state + entity_id: person.mary + to: 'home' + action: + - service: linkplay.snapshot + data: + entity_id: media_player.sound_room1 + - service: media_player.volume_set + data: + entity_id: media_player.sound_room1 + volume_level: "{{ state_attr('media_player.sound_room1', 'volume_level') | float + 0.15 }}" + - service: tts.google_translate_say + data: + entity_id: media_player.sound_room1 + language: en + message: "Hanna has arrived home." + +- alias: 'Restore state after TTS for snapshotted Sound Room 1' + id: tts_restore_sound_room1 + trigger: + - platform: state + entity_id: media_player.sound_room1 + attribute: tts_active + to: false + condition: + - condition: state + entity_id: media_player.sound_room1 + attribute: snapshot_active + state: true + action: + - service: linkplay.restore + data: + entity_id: media_player.sound_room1 +``` +No need for any delays in the automations. Trigger on attribute `tts_active` goes _false_ when the player finishes playing the TTS stream, and if `snapshot_active` is _true_ means that snapshot exists and can be restored. + Play a sound file located on an http server or a webradio stream: ```yaml - service: media_player.play_media @@ -241,31 +282,6 @@ Select an input and set volume and unmute via an automation: ``` Note that you have to specify source names as you've set them in the configuration of the component. -Intrerupt playback of a source, say a TTS message and resume playback afterwards: -```yaml -- alias: 'Notify by TTS that Mary has arrived' - trigger: - - platform: state - entity_id: person.mary - to: 'home' - action: - - service: linkplay.snapshot - data: - entity_id: media_player.sound_room1 - - service: media_player.volume_set - data: - entity_id: media_player.hang_nappali - volume_level: 0.8 - - service: tts.google_translate_say - data: - entity_id: media_player.sound_room1 - message: 'Mary arrived home' - - delay: '00:00:02' - - service: linkplay.restore - data: - entity_id: media_player.sound_room1 -``` -Note the `delay`, that should be equal or more with the time it takes for the TTS to spkeak out the text, usually that's an average of 1 second for every 3 words spoken out. ## About Linkplay diff --git a/custom_components/linkplay/media_player.py b/custom_components/linkplay/media_player.py index 344a1e1..888d701 100644 --- a/custom_components/linkplay/media_player.py +++ b/custom_components/linkplay/media_player.py @@ -104,9 +104,11 @@ ATTR_FWVER = 'firmware' ATTR_TRCNT = 'tracks_local' ATTR_TRCRT = 'track_current' -ATTR_DEBUG = 'debug_info' ATTR_STURI = 'stream_uri' ATTR_UUID = 'uuid' +ATTR_TTS = 'tts_active' +ATTR_SNAPSHOT = 'snapshot_active' +ATTR_DEBUG = 'debug_info' CONF_NAME = 'name' CONF_LASTFM_API_KEY = 'lastfm_api_key' @@ -141,7 +143,7 @@ MROOM_UJWDIR = timedelta(seconds=20) MROOM_UJWROU = timedelta(seconds=3) SPOTIFY_PAUSED_TIMEOUT = timedelta(seconds=300) -AUTOIDLE_STATE_TIMEOUT = timedelta(seconds=3) +AUTOIDLE_STATE_TIMEOUT = timedelta(seconds=1) #PARALLEL_UPDATES = 0 CUT_EXTENSIONS = ['mp3', 'mp2', 'm2a', 'mpg', 'wav', 'aac', 'flac', 'flc', 'm4a', 'ape', 'wma', 'ac3', 'ogg'] @@ -492,7 +494,7 @@ async def async_update(self): if self._master is None: self._slave_mode = False - if self._slave_mode or self._snapshot_active: + if self._slave_mode: # or self._snapshot_active: return True if self._multiroom_unjoinat is not None: @@ -1088,6 +1090,9 @@ def extra_state_attributes(self): if self._uuid != '': attributes[ATTR_UUID] = self._uuid + attributes[ATTR_TTS] = self._playing_tts + attributes[ATTR_SNAPSHOT] = self._snapshot_active + if DEBUGSTR_ATTR: atrdbg = "" if self._playing_localfile: @@ -1633,6 +1638,8 @@ async def async_set_volume_level(self, volume): if self._is_master: value = await self.call_linkplay_httpapi("setPlayerCmd:slave_vol:{0}".format(str(volume)), None) else: + if self._snapshot_active: + await asyncio.sleep(0.6) value = await self.call_linkplay_httpapi("setPlayerCmd:vol:{0}".format(str(volume)), None) if value == "OK": @@ -2357,6 +2364,8 @@ 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) else: return