I figured it out, finally! Here’s a brief (EDIT: maybe not-so-brief!) description, in the hope that it helps someone.
I noticed that in the MA web interface if I open the Chrome dev tools’ console tab (the network tab isn’t showing anything useful when I am using the interface), I can see the message “Connecting to Music Assistant API ws://172.20.1.100:8123/api/…/ws” and also the JS source file which printed this message. Clicking the filename opens the source itself, allowing me to do a Ctrl+F to find “mediaPlay”, which I did. I put a break-point at that line and clicked the “Play Playlist from here” menu item in the MA interface, and execution stopped at this line, as expected. From there, I could trace the execution to the most important line of code I was looking for:
this.ws.send(JSON.stringify(o))
Inspecting the value of “o” got me what I wanted. It looks something like this:
{
"command": "player_queues/play_media",
"message_id": 30,
"args": {
"queue_id": "767077c6-8498-5920-c0ae-04029797bdb2",
"media": "library://playlist/109",
"radio_mode": false,
"start_item": "Hg8Fa_EUQqY"
}
}
I took this data and used it in the websocket node of Node-RED and voila! My speaker started playing from the selected track! I was relieved that there wan’t any complicated authentication needed!
It goes without saying that every operation that one can do from the MA web interface can be automated this way! For example, I found the JSON payload for the WS call that adds and removes a track from Favorites.
If only the MA websocket API was documented somewhere (didn’t find any after some search. I might have missed it too), it would have saved me a few days.
So if you’re wondering what this project of mine is all about, it is this -
I wanted to be able to play and navigate music from a few playlists I created in MA, on my Google Nest Audio speaker, with minimal effort - just by picking up an IR remote and pressing a key (key-presses received by ESPHome-based IR Receiver and relayed to Home-Assistant/Node-RED). For feedback on what’s going on, like which playlist, what track, and the media transport operations, I wanted it all displayed at appropriate times or when needed, on a ESPHome-based MAX7219 Dot Matrix display (that also doubles as a digital clock).
Other features include being able to do all of the following using just the remote -
- start music with a single button press - start at the last played playlist and track, no matter the state (off, idle, empty queue etc.,) of the speaker.
- cycle through playlists
- skip to next/go to previous track
- seek within playing track
- display on demand (using remote), as well as on track change, what’s currently playing
Some more features -
- display a progress-bar of the current track
- automatic saving of the current queue_index of each playlist when played, so when I come back to a previously played playlist, I can continue from where I left off (I love this feature )
So this project is finished now, with all of the above features working. It is all mostly done with Node-RED flows in the Node-RED add-on of Home Assistant. I find wiring up Node-RED flows for complex tasks a lot faster than, say scripting or automation using YAML.
And finally, the WebSocket API of Music Assistant is a major part of my project now! A lot of its features wouldn’t have been possible without it!
With more usage, I will surely add more features. If readers have suggestions to improve the user-experience, you are very welcome to suggest!