I think I found a work-around for playing youtube music through Sonos. Based on this post: Youtube Music Multi Room Audio with Owntone (Supports both Airplay and chromecasts)
I have set up the hassio-addon-owntone (beta version as the older ‘stable’ one didn’t want to start up its web service) and added the pipe which will be used by OwnTone integration to stream the music from youtube_music_player into:
</s> <s>cd ~/media/music </s> <s>mkfifo librespot-java </s> <s>mkfifo librespot-java.metadata</s> <s>
(seems to be not required and even not working as good as the source “Default (no pipe)”)
Then I installed the OwnTone HA integration which automatically detects and connects to the OwnTone Server addon.
OwnTone can now be selected as playback device in youtube_music_player (I needed to restart HA for it to appear) redirecting the stream to that pipe, which will then automatically be picked up by the OwnTone Server addon and start playing.
In the original post I used as as a reference, they would then call an automation to select an output on the OwnTone Server based on room precense. But in my case my Sonos is in a different subnet and I didn’t find immediately how to make OwnTone detect my Sonos. And also the OwnTone Server seems to detect a few other output devices like my amp and kodi instances, but then fails on them with connection timeouts in the logging, not giving me much confidence that I will get it working easily that way.
But OwnTone also supports streaming and exposes http://SERVER_ADDRESS:3689/stream.mp3
for any device to stream from OwnTone.
I currently have only one Sonos device so I just created an automation rule to automatically start streaming OwnTone to my Sonos whenever OwnTone Server would switch to ‘playing’ state:
alias: Play OwnTone stream on Sonos
trigger:
- platform: state
entity_id:
- media_player.owntone_server
to: playing
action:
- service: media_player.play_media
data:
media_content_type: music
media_content_id: http://[server IP]:3689/stream.mp3
target:
entity_id: media_player.sonos
mode: single
When I now start a playlist on youtube_music_player with OwnTone Server as playback device, OwnTone Server will start playing the stream, hence serving/filling the stream.mp3 file. The automation will kick in and will start playing the stream on my Sonos device. This works fine, with only a few seconds delay between starting a song and actually hearing the song through the speaker.
BUT…
for some reason youtube_music_player only wants to play 1 song from a playlist and then turns itself off at the end of the song.
Also when skipping to the next song manually, it shows the metadata of the next song, but then pauses playback. I have to manually press play again to start that next song. Even when skipping within a song: the progress bar jumps to the position I want to skip to, but then playback pauses and I have to press play again.
In HA logging I see this when skipping to the next track:
2023-12-06 11:21:05.723 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - Playing track nr 1 / 25
2023-12-06 11:21:05.723 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_get_url
2023-12-06 11:21:05.723 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_check_api [E]
2023-12-06 11:21:05.723 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - try to find URL on our own
2023-12-06 11:21:05.886 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - self decoded URL via cipher
2023-12-06 11:21:05.995 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] async_get_url
2023-12-06 11:21:05.996 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - forwarding url to player media_player.owntone_server
2023-12-06 11:21:05.996 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_update_extra_sensor [E]
2023-12-06 11:21:06.530 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: off -> paused ... not playing [E]
2023-12-06 11:21:06.530 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: off -> paused [E]
2023-12-06 11:21:06.533 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:06.533 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:06.893 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_update_extra_sensor [E]
2023-12-06 11:21:06.894 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] async_get_track
2023-12-06 11:21:06.894 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] play_media
2023-12-06 11:21:08.550 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> playing ... not playing [E]
2023-12-06 11:21:08.550 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> playing [E]
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_get_track
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - Playing track nr 2 / 25
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_get_url
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_check_api [E]
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - try to find URL on our own
2023-12-06 11:21:17.270 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - self decoded URL via cipher
2023-12-06 11:21:17.343 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] async_get_url
2023-12-06 11:21:17.343 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - forwarding url to player media_player.owntone_server
2023-12-06 11:21:17.343 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_update_extra_sensor [E]
2023-12-06 11:21:17.768 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: playing -> paused ... not playing [E]
2023-12-06 11:21:17.768 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: playing -> paused
2023-12-06 11:21:17.768 ERROR (MainThread) [custom_components.ytube_music_player.media_player] media_pause
2023-12-06 11:21:17.771 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:17.771 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:18.178 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:18.178 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:18.180 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:18.180 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:18.181 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:18.181 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:18.344 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_update_extra_sensor [E]
2023-12-06 11:21:18.345 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] async_get_track
Any idea what may causing this ? And how this can be fixed?
I’m not sure, but from what I understand from the logging, I think the OwnTone Server switches state to paused when current stream is interrupted (for skipping or for next song) and as a reaction to that state change, youtube_music_player takes over that state and also pauses playback. Is this possibly what happens ?
(also, on a sidenote, there is a small input bug in the youtube_music_player configuration flow: when a proxy is previously set, you are unable to unset the proxy settings by just clearing the fields. It says the configuration is saved, but the cleared proxy fields are reverted when checked again. Replacing the proxy field with a single space seemed to work however)
[EDIT: found out that using the pipe is optional and works less reliable than setting OwnTone source to “Default (no pipe)”]