Controlling Sonos plugin via Python script

Posted on
Fri Nov 25, 2016 4:11 pm
Ramias offline
Posts: 272
Joined: Nov 24, 2015

Controlling Sonos plugin via Python script

I am trying to control my Sonos plugin via Python.

I'm able now to group various players together and query properties.

I am trying to get it to play various stations (all of which are favorites on my Sonos system).

What is the python command line syntax to play something? Is it different if it is Pandora, iHeartRadio etc? Or can I reference all by their string names if they are saved favorites?

This is the party of my code that is failing. In this case I was testing Pandora. Station Name as a string failed. I then tried "1" and 1 w/o quotes but no luck.

Any pointers on this?

Thanks

Code: Select all
sonosPlugin.executeAction("actionZP_Pandora", props={'setting':"1"}, deviceId=SonosLivingRoom.id)

Posted on
Fri Nov 25, 2016 9:17 pm
Ramias offline
Posts: 272
Joined: Nov 24, 2015

Re: Controlling Sonos plugin via Python script

Sp here is more of an example of what I am trying to do.

I want a universal "Sonos On" action that will work regardless of situation -- if Sonos is on mute but playing, this action will just unsure and do nothing more.

If Sonos is not playing, this action will play a certain station based on month/day of week (and in the future, hour, when I decide to listen to something else at night).

I need some help on the actual "Play this station" part -- you can see some comments in the existing code.

I'm doing this because of reliability and performance issues with my current action group(s) that do this. I figured having this in code would work better.

I've sone it in both indigo python and regular python using SoCo. Seems harder with SoCo -- more of a delay in getting my own station to play (That's as far as I went).

Thoughts on this?

Thanks

Code: Select all
sonosID = "com.ssi.indigoplugin.Sonos"
sonosPlugin = indigo.server.getPlugin(sonosID)
from datetime import datetime

SonosLivingRoom = indigo.devices[1904190489] # "Sonos Living Room"
SonosTVRoom = indigo.devices[89173419] # "Sonos TV Room"
SonosOffice = indigo.devices[393468053] # "Sonos Office"
SonosRowingRoom = indigo.devices[986089169] # "Sonos Rowing Room"
SonosBasement = indigo.devices[1177387431] # "Sonos Basement"
SonosBedroom = indigo.devices[1413241449] # "Sonos Bedroom"

def checkZones(zInput, zMaster):
   if zInput.states["ZoneGroupID"] == zMaster.states["ZoneGroupID"]:
      indigo.server.log("Zone for " + str(zInput.states["ZP_ZoneName"]) + " is already in the zone controlled by " + str(zMaster.states["ZP_ZoneName"]) + ".  No action needed.")
   else:
      indigo.server.log("Zone for " + str(zInput.states["ZP_ZoneName"]) + " is not equal to master")
      sonosPlugin.executeAction("actionZP_addPlayerToZone", props={'setting':zInput.id}, deviceId=zMaster.id)
   return

def CheckTheZones ():
   checkZones (SonosLivingRoom, SonosLivingRoom)
   checkZones (SonosTVRoom, SonosLivingRoom)
   checkZones (SonosOffice, SonosLivingRoom)
   checkZones (SonosRowingRoom, SonosLivingRoom)
   checkZones (SonosBasement, SonosLivingRoom)
   checkZones (SonosBedroom, SonosLivingRoom)
   return

def WMAL ():
   print ("WMAL Function Called")
   # Need to figure out how to play stuff on Sonos
   return

def ChristmasMusic():
   print ("Play some Christmas Music")
   # Need to figure out how to play stuff on Sonos
   return

def SaturdayMusic():
   print ("Play some Saturday Music")
   # Need to figure out how to play stuff on Sonos
   return

def playSelector ():
   print ("Play Selecter Called")
   day, month, hour = datetime.now().strftime("%w, %m, %H").split(',')
   if str(month).replace(" ", "") == "12" and str(day).replace(" ", "") == "7":
      ChristmasMusic()
   elif str(day).replace(" ", "") == "6":
      SaturdayMusic()
   else:
      WMAL()
   return

# Check Zones.  Make sure everybody is part of Living Room Zone.  Do this before even checking if it is playing or not.
CheckTheZones()

if SonosLivingRoom.states["ZP_STATE"] == "PLAYING" and SonosLivingRoom.states["ZP_MUTE"] == "1":
   sonosPlugin.executeAction("actionGroupMuteOff", deviceId=SonosLivingRoom.id)
else:
   indigo.server.log("SonosLivingRoom not on mute and not currently playing.  Beginning to play")
   # Add function to turn on Yamaha Receivers (both zones each) and set volume
   playSelector()

Posted on
Wed Nov 30, 2016 4:11 pm
Ramias offline
Posts: 272
Joined: Nov 24, 2015

Re: Controlling Sonos plugin via Python script

Well, i just hacked together a solution and now from my script call the indigo action group that plays the station since I couldn't figure out the various parameters. That actually seems to be working pretty good.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests