This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Control Philips Hue lights from Hubot #1198
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fb9a195
added Philips Hue Light script
c095e9e
tweakin documentation
b56a421
philips tweak
kingbin 8dfb340
grab a hash code from the bridge
fbf5258
Merge pull request #1 from bwynants/master
kingbin 61eaea8
update Author before pull request
kingbin 7c3e691
Updating directions on the configuration for the script to run
kingbin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Description: | ||
# Control your Philips Hue Lights from HUBOT! BAM, easy candy for the kids | ||
# | ||
# Dependencies: | ||
# | ||
# Configuration: | ||
# This script is dependent on environment variables: | ||
# PHILIPS_HUE_HASH : export PHILIPS_HUE_HASH="secrets" | ||
# PHILIPS_HUE_IP : export PHILIPS_HUE_IP="xxx.xxx.xxx.xxx" | ||
# | ||
# Setting your Hue Hash | ||
# | ||
# This needs to be done once, it seems older versions of the Hue bridge used an md5 hash as a 'username' but now you can use anything. | ||
# | ||
# Make an HTTP POST request of the following to http://YourHueHub/api | ||
# | ||
# {"username": "YourHash", "devicetype": "YourAppName"} | ||
# If you have not pressed the button on the Hue Hub you will receive an error like this; | ||
# | ||
# {"error":{"type":101,"address":"/","description":"link button not pressed"}} | ||
# Press the link button on the hub and try again and you should receive; | ||
# | ||
# {"success":{"username":"YourHash"}} | ||
# The key above will be the username you sent, remember this, you'll need it in all future requests | ||
# | ||
# ie: curl -v -H "Content-Type: application/json" -X POST 'http://YourHueHub/api' -d '{"username": "YourHash", "devicetype": "YourAppName"}' | ||
# | ||
# | ||
# Commands: | ||
# hue lights - list all lights | ||
# hue light <light number> - shows light status | ||
# hue turn light <light number> <on|off> - flips the switch | ||
# hue groups - groups lights together to control with one API call | ||
# hue config - reads bridge config | ||
# hue hash - get a hash code (press the link button) | ||
# hue set config (name|linkbutton) <value>- change the name or programatically press the link button | ||
# hue (alert|alerts) light <light number> - blink once or blink for 10 seconds specific light | ||
# | ||
# Notes: | ||
# | ||
# Author: | ||
# kingbin - [email protected] | ||
|
||
|
||
module.exports = (robot) -> | ||
base_url = process.env.PHILIPS_HUE_IP | ||
hash = process.env.PHILIPS_HUE_HASH | ||
|
||
|
||
# GROUP COMMANDS | ||
robot.hear /hue groups/i, (msg) -> | ||
url = "http://#{base_url}/api/#{hash}/groups" | ||
getGenInfo msg, url, (responseText) -> | ||
msg.send "Groups: " + responseText | ||
|
||
|
||
# LIGHT COMMANDS | ||
robot.hear /hue lights/i, (msg) -> | ||
light = msg.match[1] | ||
url = "http://#{base_url}/api/#{hash}/lights" | ||
getGenInfo msg, url, (responseText) -> | ||
msg.send responseText | ||
|
||
robot.hear /hue light (.*)/i, (msg) -> | ||
light = msg.match[1] | ||
url = "http://#{base_url}/api/#{hash}/lights/#{light}" | ||
getGenInfo msg, url, (responseText) -> | ||
msg.send responseText | ||
|
||
robot.hear /hue turn light (.+) (.+)/i, (msg) -> | ||
[light, state] = msg.match[1..2] | ||
msg.send "get off your LAZY ass and turn it " + state + " yourself" | ||
jsonParams = | ||
on: if state is "on" then true else false | ||
url = "http://#{base_url}/api/#{hash}/lights/#{light}/state" | ||
setInfo msg, url, jsonParams, (responseText) -> | ||
msg.send responseText | ||
|
||
robot.hear /hue (alert|alerts) light (.+)/i, (msg) -> | ||
[state,light] = msg.match[1..2] | ||
msg.send "ain't nobody got time for that!" | ||
|
||
jsonParams = alert: if state is "alert" then "select" else "lselect" | ||
|
||
url = "http://#{base_url}/api/#{hash}/lights/#{light}/state" | ||
setInfo msg, url, jsonParams, (responseText) -> | ||
msg.send responseText | ||
|
||
|
||
# BRIDGE COMMANDS | ||
robot.hear /hue config/i, (msg) -> | ||
url = "http://#{base_url}/api/#{hash}/config" | ||
getGenInfo msg, url, (responseText) -> | ||
msg.send "Config: " + responseText | ||
|
||
robot.hear /hue hash/i, (msg) -> | ||
msg.http("http://#{base_url}/api") | ||
.headers(Accept: 'application/json') | ||
.post(JSON.stringify({devicetype: "hubot"})) (err, res, body) -> | ||
for line in JSON.parse(body) | ||
do (line) -> | ||
if (line.error) | ||
msg.send line.error.description | ||
if (line.success) | ||
msg.send "Hash:" + line.success.username | ||
|
||
robot.hear /hue set config ([^\s]*) (.*)/i, (msg) -> | ||
[setting,val] = msg.match[1..2] | ||
supportedSettings = ['name','linkbutton'] | ||
if setting in supportedSettings | ||
|
||
switch setting | ||
when "name" then jsonParams = name: val | ||
when "linkbutton" then jsonParams = linkbutton: val | ||
|
||
msg.send JSON.stringify(jsonParams) | ||
url = "http://#{base_url}/api/#{hash}/config" | ||
setInfo msg, url, jsonParams, (responseText) -> | ||
msg.send responseText | ||
else | ||
msg.send "unsupported config setting" | ||
|
||
|
||
# robot.respond /rollout activate_user ([^\s]*) ([^\s]*)/i, (msg) -> | ||
# msg.http(endpoint + msg.match[1] + '/users').query(user: msg.match[2]).put() (err, res, body) -> | ||
# show(msg, msg.match[1]) | ||
|
||
|
||
getGenInfo = (msg, url, callback) -> | ||
msg.http(url) | ||
.headers(Accept: 'application/json') | ||
.get() (err, res, body) -> | ||
if 200 <= res.statusCode <= 299 | ||
callback JSON.stringify(JSON.parse(body),null,'\t') | ||
else | ||
callback res.statusCode | ||
|
||
|
||
setInfo = (msg, url, jsonParams, callback) -> | ||
msg.http(url) | ||
.put(JSON.stringify(jsonParams)) (err, res, body) -> | ||
if 200 <= res.statusCode <= 299 | ||
callback JSON.stringify(JSON.parse(body),null,'\t') | ||
else | ||
callback res.statusCode |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to remove this commented out code if it's not being used.