home-assistant / home-assistant.io

:blue_book: Home Assistant User documentation
https://www.home-assistant.io
Other
4.88k stars 7.25k forks source link

Text to speech via Bluesound, snapshot-restore #35268

Open jowinthe opened 3 days ago

jowinthe commented 3 days ago

Feedback

In the Sonos integration there is a functionality to take a snapshot of the current state of the media player, and then to restore the same state, e.g. after an interruption from a TTS announcement. This works quite well, and make it relatively easy to use the Sonos system for TTS applications. Are there any plans to incorporate something similar in the Bluesound integration? And, if not, are you aware of any blueprints or other code that one could use as a template for this purpose?

URL

https://www.home-assistant.io/integrations/bluesound/

Version

2024.10.2

Additional information

From a TTS blueprint for Sonos: (https://community.home-assistant.io/t/script-for-sonos-speakers-to-do-text-to-speech-and-handle-typical-oddities/424842)

sequence:

save current state so we can restore to whatever was happening previously

home-assistant[bot] commented 3 days ago

Hey there @thrawnarn, @louischrist, mind taking a look at this feedback as it has been labeled with an integration (bluesound) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `bluesound` can trigger bot actions by commenting: - `@home-assistant close` Closes the feedback. - `@home-assistant rename Awesome new title` Renames the feedback. - `@home-assistant reopen` Reopen the feedback. - `@home-assistant unassign bluesound` Removes the current integration label and assignees on the feedback, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information) to the feedback. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information) on the feedback.
LouisChrist commented 3 days ago

I was not aware of this functionality. This could maybe be implemented for the bluesound integration. It might not work for all audio sources, though. They are inconsistent in how they work and not well documented.

It might already work with the help of source and volume_level. The following is a rough idea of how it might work. I tested the volume part. I haven't tested the source. You might have to call play afterwards. And it might not work for all sources.

alias: New automation
mode: single
description: ""

triggers: []
conditions: []

actions:
  # snapshot
  - variables:
      source: "{{ state_attr('media_player.node', 'source') }}"
      volume: "{{ state_attr('media_player.node', 'volume_level') }}"

  # do tts
  - action: tts.speak
    data: {}

  # restore
  - action: media_player.volume_set
    metadata: {}
    data:
      volume_level: "{{ volume }}"
    target:
      entity_id: media_player.node
  - action: media_player.select_source
    metadata: {}
    data:
      source: "{{ source }}"
    target:
      entity_id: media_player.node
jowinthe commented 3 days ago

Thanks for the swift reply! Yes, that works! I had to put in a delay according to the length of the TTS announcement, but other than that it's a definite thumbs up. Both TuneIN and Spotify reverted back to the original source. Volume adjusted correctly (before and) after the TTS.

Here's the YAML code I used:

alias: Bluesound tts
description: ""
triggers: []
conditions: []
actions:
  - variables:
      source: "{{ state_attr('media_player.bluesound_livingroom', 'source') }}"
      volume: "{{ state_attr('media_player.bluesound_livingroom', 'volume_level') }}"
  - action: media_player.volume_set
    metadata: {}
    data:
      volume_level: 0.1
    target:
      device_id: xxx
  - action: tts.google_translate_say
    metadata: {}
    data:
      entity_id: media_player.bluesound_livingroom
      message: The garage door...is open
      cache: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 4
      milliseconds: 0
  - action: media_player.volume_set
    metadata: {}
    data:
      volume_level: "{{ volume }}"
    target:
      entity_id: media_player.bluesound_livingroom
  - action: media_player.select_source
    metadata: {}
    data:
      source: "{{ source }}"
    target:
      entity_id: media_player.bluesound_livingroom
mode: single
LouisChrist commented 2 days ago

I am a little bit surprised that it works for TuneIn :thinking: Do you have a preset for the TuneIn Station you were listening to?

jowinthe commented 1 day ago

I did some testing. This was a bit tricky. Here's what I found: The code works while;

  1. Not playing anything.
  2. While streaming Spotify (saved as a Preset).
  3. While streaming TuneIN stations that are in the Preset List (regardless of how they are initiated).
  4. While streaming TuneIN stations added as Custom Stations (URLs), saved to the Preset List, and initiated from the Preset List (not when started from the Favorites List).

When a TuneIN station is not stored in the Preset List, the code changes the volume and plays the TTS message, but fails to restore the previous stream. Instead it reverts to the last Library track that was played (in my case a .flac file) in pause mode. In other words, the music stops.

While streaming from the Library (for example a .flac file in a NAS), it works, with the exception that is skips to the next track, and with subsequent runs of the code it fails (until a new track is manually selected or Pause->Play is pressed). By "fails" I mean that the volume changes correctly, but the track just keeps playing when the TTS was supposed to kick in.

I hope this helps. Unfortunately I don't have the programming skills to solve this.