(11.09.2019, 07:47)Daniel Wrote: Hi
Here is how to use new Sonos app via script:
The app runs a daemon which keeps communication with each player/group The daemon will be closed down after 120s if no update is send so we need to run such script every 60s to keep communication up and running.
Code:require('custom.sonos.lib')
groupID=sonos_app.GetStoragePlayerGroups()
for k, v in pairs(groupID) do
sonos_app.SendApiActionCommand(k, 'watchPlayerGroup', os.time())
end
To control the players we need to know groupID which can be read via such command.
Code:groupID=sonos_app.GetStoragePlayerGroups()
Basic control is done like this
Code:-- simle command: play, pause, skipToPrev, skipToNext
sonos_app.SendApiActionCommand(url_group_id, 'play')Code:-- set volume
sonos_app.SendApiActionCommand(url_group_id, 'setVolume', 50)
The url_group_id is in the table from the groupID but it can be read automatically when we know group name so example start/pause script would looks like that.
Code:require('custom.sonos.lib')
groupName='Kitchen'
-------------------------------------------------------------
groupID=sonos_app.GetStoragePlayerGroups()
for k, v in pairs(sonos_app.GetStoragePlayerGroups()) do
if groupID[k].groupName == groupName then
groupID=k
break
end
end
-------------------------------------------------------------
value = event.getvalue()
if value then
sonos_app.SendApiActionCommand(groupID, 'play')
else
sonos_app.SendApiActionCommand(groupID, 'pause')
end
To read Playlist table use this
Code:sonos_app.GetStoragePlayerGroupData(groupID, 'Playlists')
To Play a playlist
playlist_id looks like a number but it is a string!Code:-- play playlist by ID
sonos_app.SendApiActionCommand(url_group_id, 'playPlaylist', playlist_id)
To read Favorites use
Code:sonos_app.GetStoragePlayerGroupData(groupID, 'Favorites')
To play a Favorites
Code:sonos_app.SendApiActionCommand(url_groupID, 'playFavorite', url_favID)
Be aware that in some examples url_groupID is used instead of the correct groupID
So you need to replace this to get it work.
To play your own (doorbell) file using the new Sonos API you just need to follow the steps below.
First we need to add the url as a radiostation in TuneIn.
1. Go to the Browse tab within the Sonos app and select TuneIn.
2. Tap My radio stations.
3. Tap the three dots at the top right and tap Add new radio station.
4. Enter the streaming URL and station name and tap OK.
5. The station will be listed and available in the My radio stations section under TuneIn.
6. Add the station as a fovorite.
Now you can use the new api.
1. Read favorites table to see what id the station got.
favorites = sonos_app.GetStoragePlayerGroupData(groupID, 'Favorites')
2. play favorite by ID
favorite_id = '27'
sonos_app.SendApiActionCommand(groupID, 'playFavorite', favorite_id)