HomeKit, AirPlay, and controlling iTunes with Siri
A few days ago I wrote an article about getting my Flic button to work with HomeKit. Since then, I’ve had a few requests from people wondering how I was getting iTunes to work with HomeKit such that it could start a playlist on some AirPlay speakers. Today I’ve released a couple of HomeBridge plugins to NPM and I’ll detail how I’ve got my system working.
First of all, my entire setup is powered by the awesome Homebridge system which I’ve written about at great length. I found a plugin from Dan Budiac called homebridge-applescript which allowed you to run a line of applescript via a system of fake switches (so a different script could be called when the switch is turned on and off). This is great for simple one-liners (like “
tell application iTunes to pause”) but I needed something a bit longer for dealing with AirPlay. I forked Dan’s project to create my own Homebridge plugin that would work with a path to an AppleScript file rather than a single line of AppleScript:
This allowed me to then use my existing alarm clock script in order to make iTunes connect to my kitchen speakers and play a specific playlist1 when my “kitchen speakers” switch is turned on and disconnect from AirPlay and pause iTunes when it is turned off.
This worked well but it has a few problems as the music is coming from a Mac in a different room; there is no way to change the volume, skip a track, or do a basic play / pause toggle2 without going upstairs to do it manually on iTunes. After thinking about it for a little while, I was able to get the track skipping and play /pause done fairly easily. First of all, I use the following config:
I won’t detail the AppleScript for each of these as they are fairly basic.
Next, I used the ‘Scenes’ feature of HomeKit to set up named scenes that related to each toggle. For example, “iTunes next track” is a scene that ensures the “Music Track Control” accessory is turned on whereas “iTunes previous track” ensures that accessory is off. The play / pause controls work in a similar way. Once activated, this meant I could say things like “Hey Siri, iTunes next track” or “Hey Siri, pause iTunes”.
Whilst scenes can be incredibly useful due to the ability to use any text you want, they can’t be used for something like volume where you want to use a specific amount3. Unfortunately HomeKit doesn’t have the concept of speakers so there isn’t a way to naturally create an accessory that deals with volume so I improvised and created a fake lightbulb that controlled an AppleScript via the brightness property; homebridge-applescript-file-lightbulb4.
volume.applescript file looks like this:
With this in place, I can say “Hey Siri, set iTunes volume to 50%” and it will work. This is because lightbulbs have a brightness setting and Siri interprets “set [name] to [percentage]” as being “change the brightness of this device” just in this case we are hitting an AppleScript with our brightness value. This has been a fun little exercise in trying to hack HomeKit into what I want to use it for. I’m hopeful that iOS 10 will add native support for HomeKit speakers as then this could be done without reverting to AppleScript and I’d be able to stream from my iPhone via Siri but it’s pretty cool for the time being.
You can find both of my Homebridge plugins on NPM and GitHub.
Actually my own setup has two playlists. I have a playlist for Monday through Saturday with my music but on Sunday my wife gets up first so I have a different playlist for her. The code for that is
if weekday of (current date) is Sunday then↩︎
At first I’d get around this by just using “turn off kitchen music” followed by “turn on kitchen music” as the playlist was shuffled but hardly an ideal solution. ↩︎
I could have done the same as the track control AppleScript and had a “iTunes volume up” and “iTunes volume down” scene to increase or decrease by 10% each time but that would be a pain if you wanted to change the volume by a large amount. ↩︎
I shouldn’t be allowed to name things. ↩︎