This repository has been archived by the owner on Jun 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
siriproxy-hue.rb
178 lines (158 loc) · 4.76 KB
/
siriproxy-hue.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# SiriProxy-Hue
# https://github.com/interstateone/siriproxy-hue
# copyright 2013, Brandon Evans
# extended version and bug fixes, Christoph Weil
# Released under MIT License
# SiriProxy Plugin Requirements
require "cora"
require "siri_objects"
require "pp"
require "rest_client" # HTTP requests
require "json" # Parse Hue responses
require "matrix" # Color space transformations
require "hue-entity"
class SiriProxy::Plugin::Hue < SiriProxy::Plugin
def initialize (config)
@@bridgeIP = config['bridge_ip']
@@username = config['username']
@@registering = false
@@registered = false
end
def checkRegistration
if @@registered then return end
url = "#{@@bridgeIP}/api/#{@@username}"
response = RestClient.get(url)
data = JSON.parse(response)
if data.kind_of?(Array)
data = data[0]
end
if data.has_key? "error"
# Unregistered username
@@registering = true
response = ask "I just need to finish setting up: say \"Ready\" after you've pressed the link utton on your Hue bridge."
if response =~ /ready/i
url = "#{@@bridgeIP}/api"
data = {devicetype: "siriproxy", username: "#{@@username}"}.to_json
response = RestClient.post(url, data, content_type: :json)
request_completed
checkRegistration
end
else
# Success
if @@registering
@@registering = false
say "Thanks, you're ready to go."
request_completed
@@registered = true
else
@@registered = true
end
end
end
def parseNumbers (value)
if (value =~ /%/)
value = value.delete("%").to_i * 254 / 100 # 254 is max brightness value
end
end
def is_numeric?(obj)
obj.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
end
def ensureMatchedEntity(entity)
begin
matchedEntity = HueEntity.new(entity, @@bridgeIP, @@username)
rescue ArgumentError
say "I couldn't find any lights by that name."
request_completed
return
end
matchedEntity
end
def switchEntity(state, matchedEntity)
if (state == "on")
matchedEntity.power(true)
else
matchedEntity.power(false)
end
if matchedEntity.type == :group
if matchedEntity.name == "all"
say "I've turned #{state} all of the lights for you."
else
say "I've turned #{state} the #{matchedEntity.name} lights for you."
end
else
say "I've turned #{state} the #{matchedEntity.name} light for you."
end
request_completed
end
def setRelativeBrightness(change, matchedEntity)
currentBrightness = matchedEntity.brightness
if (change == "up")
newBrightness = currentBrightness + 50
else
newBrightness = currentBrightness - 50
end
matchedEntity.brightness(newBrightness)
response = ask "Is that enough?"
if (response =~ /yes/i)
say "I'm happy to help."
elsif (response =~ /more|no/i)
if (change == "up")
newBrightness += 50
else
newBrightness -= 50
end
matchedEntity.brightness(newBrightness)
say "You're right, that does look better"
end
request_completed
end
def setAbsoluteBrightness(value, matchedEntity)
numericValue = parseNumbers(value)
if (is_numeric? numericValue)
log numericValue
matchedEntity.brightness(numericValue.to_i)
else
# query colourlovers for first rgb value from the given string
url = "http://www.colourlovers.com/api/colors?keywords=#{value}&numResults=1&format=json"
url.gsub!(/\s/,'%20')
response = RestClient.get(url)
if response.eql?("[]") then
say "I couldn't find any color by that name."
request_completed
else
data_rgb = JSON.parse(response)[0]["rgb"]
data_hsv = JSON.parse(response)[0]["hsv"]
matchedEntity.color(data_rgb["red"], data_rgb["green"], data_rgb["blue"], data_hsv["saturation"])
end
end
say "There you go."
request_completed
end
# Binary state
listen_for %r/turn (on|off)(?: the)? ([a-z ]??*)(?: the)? light(?:s)?/i do |state, entity|
checkRegistration
matchedEntity = ensureMatchedEntity(entity)
switchEntity(state, matchedEntity)
end
# Relative brightness
listen_for %r/turn (up|down)(?: the)? ([a-z ]??*)(?: the)? light(?:s)?/i do |change, entity|
checkRegistration
matchedEntity = ensureMatchedEntity(entity)
setRelativeBrightness(change, matchedEntity)
end
# Absolute brightness/color change
# Numbers (0-254) and percentages (0-100) are treated as brightness values
# Single words are used as a color query to lookup HSV values
listen_for %r/set(?: the)? ([a-z ]??*)(?: the)? light(?:s)? to ([a-z0-9% ]*)/i do |entity, value|
checkRegistration
matchedEntity = ensureMatchedEntity(entity)
setAbsoluteBrightness(value, matchedEntity)
end
# TODO Scenes
listen_for %r/make it look like a (.+)/i do |scene|
checkRegistration
# pull n colors, where n is the number of lights
# set each light to color[i]
request_completed
end
end