service: media_player.play_media
data:
media_content_id: /media/music_party/Anastacia - Not that kind.mp3
media_content_type: music
target:
entity_id: media_player.fernseher
This is going to a Google Chrome Cast built into my Philips TV.
I had some trouble finding the correct URL for media_content_id. I finally found the right path by using the method described by @antimage in post #7.
When it finally worked, I had no idea where HA took that path segment “/music_party” from. The files are really stored in the /media partition of HA and the folder is called “/Party”. Later I found that the path is actually built using the media_dirs key from configuration yaml:
As you can see I did NOT have to put any host in front of the media_content_id, so no problem with host name vs. IP. Also all the authSig token generation and handling mechanics are done automatically in the background. No need to touch that. The param media_content_type works for me with both “music” and “audio/mp3”.
Thats weird.
Playing mp3 in /config/www in my home assistant with “music” works perfectely on old Sonos. However, on a new Sonos or Amazon Echo I got the error “sorry tts can only be called with notify.alexa_media service”. When I use “audio/mp3” or “audio” or “mp3” instead of music, I don’t get anything at all.
If anyone needs python code to generate a “signed” URL to an .mp3 (or some other asset), I wrote this:
from websocket import create_connection
import json
import sys
# Python code to get a "signed path" to a Home Assistant asset via the websocket API
# As per docs:
# https://developers.home-assistant.io/docs/api/websocket/
# https://developers.home-assistant.io/docs/auth_api/#signed-paths
# Make sure to substitute your server IP, desired path to sign, and long-lived access token
# You can generate your own in HA, click on your username (bottom left) then "Long-Lived Access Tokens" -> "CREATE TOKEN"
host = "192.168.0.158:8123"
path_to_sign = "/media/local/doorbell.mp3"
protocol = "http"
access_token = "XXXXXXXX"
ws = create_connection("ws://%s/api/websocket" % host)
ws.recv()
ws.send(json.dumps({
"type": "auth",
"access_token": access_token
}))
auth_resp = json.loads(ws.recv())
if auth_resp.get('type') != 'auth_ok':
print('failed to login, check your auth token -- response: %s' % (auth_resp))
else:
ws.send(json.dumps(
{
"type": "auth/sign_path",
"path": path_to_sign,
"id": 1,
"expires": 300
}))
result = json.loads(ws.recv())
if result.get('success') == True:
signed_path = result.get('result').get('path')
print('%s://%s%s' % (protocol, host, signed_path))
else:
print('ERROR: %s' % result)
ws.close()
Spent a good two hours pulling my hair out on this one. Soin case anyone else stumbles across this, Jpsy has the solution. There’s no token or auth needed. If you have the media folder set up properly in the config it will work.
Not sure if this will help you guys, but I just started using an Add-on called “Music assistant”. Works pretty well, and casts to all my Chromecast devices MUCH more reliably than Plex. Just thought I’d toss that out there. The interface is remarkably similar to Plex, but it displays right in HA, which is nice. No switching back and forth between apps.
You cannot play a folder or playlist without add-ons. This is because HomeAssist does not stream the media files, but only sends a link to the media file to the speaker, and does not process its contents in any way, so HA does not know the current status, i.e. playback duration. And in order to send the next file from a folder or playlist, you need to know that the previous file has finished playing. Something has to keep track of this, which is why media servers and other additional tools are installed for this purpose.