Search code examples
spotifyhomekitphilips-hueios-homekit

Combine Philips Hue, HomeKit and Music (iTunes or Spotify)


First time using HomeKit here.

I have Philips Hue and using Siri (HomeKit) I activate different scenes/ambients with my voice, which is really cool.

I'd like to go one step further...

In WWDC I see that regarding HomeKit we can use termostats, fans, lights, doors, curtains, swithches, alarms, sensors... But I see a key element to create ambient is missing: Music.

I know using Hue it is possible to create ambients, like a beach sunset, using red and orange colors for lights, but at the same time I would like to play a specific playlist, for example sea or waves sounds, or just hawaiian music. The possibilities could be endless and super cool.

What do I have to do?

Do I have to create my own app that uses HomeKit and Spotify API or Apple Music API?

My idea is to start the ambient using Siri (that is, lights and music), but I don't know if what I want is technically possible.

Any suggestion? That would be awesome.


Solution

  • You can pretty easily do this with AppleScript (if you use a Macintosh, that is...) Here's some sample AppleScript code you could start with. (Paste this code into a Script Editor window.)

    -- define baseUrl to point to your Hue hub address and one of the keys in your whitelist
    set baseUrl to " http://YOUR-HUB-IP/api/YOUR-WHITELIST-ENTRY"
    
    -- read the info about light 1
    set lightJson to do shell script "curl " & baseUrl & "/lights/1"
    
    -- define some JSON to set a light state
    set lightStateOn to the quoted form of " {\"on\": true,\"bri\": 254,\"hue\": 8000,\"sat\": 254} "
    
    -- send the JSON to set a light state (on and with specified hue, saturation, and brightness)
    do shell script "curl --request PUT --data " & lightStateOn & baseUrl & "/lights/1/state/"
    
    tell application "Spotify"
        play track "spotify:track:3AhXZa8sUQht0UEdBJgpGc"
    end tell
    
    set lightStateOff to the quoted form of "{\"on\": false}"
    do shell script "curl --request PUT --data " & lightStateOff & baseUrl & "/lights/1/state/"
    

    Edit the baseUrl to include the real IP of your hub and one of the keys in the whitelist (users) from your hub's JSON file.

    Then script the curl command to get or send JSON to the hub, which changes your lights.

    Finally, both Spotify and iTunes are scriptable, so you can tell them to play songs, playlists, etc. See http://dougscripts.com/ to learn more about scripting iTunes.

    You can do this in other languages and platforms also, depending on what your hardware and skill are. The syntax will be different, but the strategy will be similar: send the commands to control the hue hub and then send other commands to control the music player.