I know that there is another post that has a script to count connected airfoil speakers but I could not get it to work, so I wrote my own. Basically the script will update a variable with the total count of connected speakers, then you can trigger on the variable being 0 and tell iTunes to stop playing. I don't count the local speakers on the mac, because i don't have any, but it's pretty clear in the script where i exclude that, so it matters to you, you can always remove those lines.
- Code: Select all
# this script will go through a folder and check each device to see if it's
# a speaker and it connected
# I am using this with airfoil, to determine if anyone is listening to music
# After the script has run a variable will contain the number of speakers connected
# you can then create a trigger to stop the music from playing if the speaker count is 0
devConnectCount = 0
# Get a list of all devices
devs = indigo.devices
# Scroll through devices looking for ones that are in a specific folder, for me that is
# my airfoil folder
for dev in devs:
if (dev.folderId == 1196330473): # airfoil
# Now check if the device is a speaker and is currently connected
if(dev.deviceTypeId == 'speaker' and dev.displayStateValUi == 'connected'):
# Grab the states and make sure this is not the locally connected speaker
# i.e. the mac default speaker, this speaker will typically alway be connected
# although that is not a requirement, I just don't have speakers connected to
# to my mac, so it's not important to me
states = dev.states
if(states['type'] != 'local'):
devConnectCount = devConnectCount + 1
# Update the variable to the number of connected speakers, for me this 'ConnectedSpeakers'
indigo.variable.updateValue(1954203491, value=unicode(devConnectCount))