Skip to content

Instantly share code, notes, and snippets.

@searls
Last active June 27, 2016 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save searls/d7c1ce5ef51c9aca8adb7a450bca23b3 to your computer and use it in GitHub Desktop.
Save searls/d7c1ce5ef51c9aca8adb7a450bca23b3 to your computer and use it in GitHub Desktop.
Tying up Philips Hue lights to the exit-code status of my shell
#!/usr/bin/env node
/**
* Activates all Philips Hue scenes for a given name, optionally
* restricted to a particular group (the new app seems to make scenes universal
* across groups so it may be necessary to look up the group ID associated with
* the room you want to be affected.
*
* Example usage:
*
* $ hue-ci "Office Green" 9
*
* Requires a HUE_IP (e.g. '10.0.1.6') and HUE_TOKEN (the "user name" portion of
* the HTTP API URL string) to be set as env vars.
*/
var http = require('http')
var hueIp = process.env['HUE_IP']
var hueToken = process.env['HUE_TOKEN']
var sceneName = process.argv[2]
var groupId = process.argv[3] || 0
var baseUrl = 'http://' + hueIp + '/api/' + hueToken
http.get(baseUrl, function (res) {
var body = ''
res.on('data', function (data) { body += data.toString() })
res.on('end', function () {
var hueConfig = JSON.parse(body)
Object.keys(hueConfig.scenes).forEach(function (sceneId) {
var scene = hueConfig.scenes[sceneId]
if (scene.name === sceneName) {
put(baseUrl + '/groups/' + groupId + '/action', {
on: true,
scene: sceneId
}, function (res) {
if (res.statusCode !== 200) {
console.error('Failed to set scene to ' + sceneName + '!')
process.exit(1)
}
})
}
})
})
})
var url = require('url')
function put (uri, body, cb) {
var bodyString = JSON.stringify(body)
var opts = Object.assign({}, url.parse(uri), {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Content-Length': bodyString.length
}
})
http.request(opts, cb).write(bodyString)
}
hue_ci()
{
exit_code=$?
if [ $exit_code -ne 0 ]
then
hue-ci "Office Red" 9
else
hue-ci "Office Green" 9
fi
}
export PS1='$(hue_ci) <rest of prompt here> $'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment