forked from savaki/go.hue
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlight.go
206 lines (180 loc) · 6.73 KB
/
light.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package hue
import (
"strconv"
)
// Light encapsulates the controls for a specific philips hue light
type Light struct {
Id string
Name string
Attributes LightAttributes
bridge *Bridge
}
// LightState encapsulates all attributes for a specific philips hue light state
type LightState struct {
Hue int `json:"hue"`
On bool `json:"on"`
Effect string `json:"effect"`
Alert string `json:"alert"`
Bri int `json:"bri"`
Sat int `json:"sat"`
Ct int `json:"ct"`
Xy []float32 `json:"xy"`
Reachable bool `json:"reachable"`
ColorMode string `json:"colormode"`
}
// SetLightState encapsulates all attributes to set a light to a specific state
type SetLightState struct {
// On/Off state of the light. On=true, Off=false
On string
// The brightness value to set the light to.
// Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum).
// Note: a brightness of 1 is not off.
Bri string
// The hue value to set light to.
// The hue value is a wrapping value between 0 and 65535.
// Both 0 and 65535 are red, 25500 is green and 46920 is blue.
Hue string
// Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white).
Sat string
// The x and y coordinates of a color in CIE color space.
// The first entry is the x coordinate and the second entry is the y coordinate. Both x and y must be between 0 and 1.
// If the specified coordinates are not in the CIE color space, the closest color to the coordinates will be chosen.
Xy []float32
// The Mired Color temperature of the light. 2012 connected lights are capable of 153 (6500K) to 500 (2000K).
Ct string
// The alert effect, is a temporary change to the bulb’s state, and has one of the following values:
// “none” – The light is not performing an alert effect.
// “select” – The light is performing one breathe cycle.
// “lselect” – The light is performing breathe cycles for 15 seconds or until an "alert": "none" command is received.
//
// Note that this contains the last alert sent to the light and not its current state.
// i.e. After the breathe cycle has finished the bridge does not reset the alert to "none".
Alert string
// The dynamic effect of the light. Currently “none” and “colorloop” are supported. Other values will generate an error of type 7.
// Setting the effect to colorloop will cycle through all hues using the current brightness and saturation settings.
Effect string
// The duration of the transition from the light’s current state to the new state.
// This is given as a multiple of 100ms and defaults to 4 (400ms).
// For example, setting transitiontime:10 will make the transition last 1 second.
TransitionTime string
}
// LightAttributes encapsulates all attributes (hardware and state) for a specific device
type LightAttributes struct {
State LightState `json:"state"`
Type string `json:"type"`
Name string `json:"name"`
ModelId string `json:"modelid"`
UniqueId string `json:"uniqueid"`
ManufacturerName string `json:"manufacturername"`
ProductName string `json:"productname"`
SoftwareVersion string `json:"swversion"`
Capabilities Capabilities `json:"capabilities"`
}
// Capabilities encapsulates the hardware capabilites of a specific hue device
type Capabilities struct {
Certified bool `json:"certified"`
Control ControlCapabilities `json:"control"`
Streaming StreamingCapabilities `json:"streaming"`
}
// ControlCapabilites encapsulates the capabilites to control a hue device
type ControlCapabilities struct {
MinDimLevel int `json:"mindimlevel"`
MaxLumen int `json:"maxlumen"`
ColorGamutType string `json:"colorgamuttype"`
ColorGamut [][]float32 `json:"colorgamut"`
ColorTemperature ColorTemperature `json:"ct"`
}
// ColorTemperature represents to possible range (min, max) of mirad color temperature
type ColorTemperature struct {
Min int `json:"min"`
Max int `json:"max"`
}
// StreamingCapabilites encapsulates the capabilites of a hue device in streaming setups
type StreamingCapabilities struct {
Render bool `json:"render"`
Proxy bool `json:"proxy"`
}
// GetLightAttributes retrieves light attributes and state as per
// http://developers.meethue.com/1_lightsapi.html#14_get_light_attributes_and_state
func (light *Light) GetLightAttributes() (*LightAttributes, error) {
var result LightAttributes
err := light.bridge.get("/lights/"+light.Id, &result)
if err != nil {
return nil, err
}
// update locally cached attributes
light.Attributes = result
return &result, nil
}
// SetName sets the name of a light as per
// http://developers.meethue.com/1_lightsapi.html#15_set_light_attributes_rename
func (light *Light) SetName(newName string) ([]Result, error) {
params := map[string]string{"name": newName}
var results []Result
err := light.bridge.put("/lights/"+light.Id, ¶ms, &results)
if err != nil {
return nil, err
}
return results, nil
}
// On is a convenience method to turn on a light and set its effect to "none"
func (light *Light) On() ([]Result, error) {
state := SetLightState{
On: "true",
Effect: "none",
}
return light.SetState(state)
}
// Off is a convenience method to turn off a light
func (light *Light) Off() ([]Result, error) {
state := SetLightState{On: "false"}
return light.SetState(state)
}
// ColorLoop is a convenience method to turn on a light and have it begin
// a colorloop effect
func (light *Light) ColorLoop() ([]Result, error) {
state := SetLightState{
On: "true",
Effect: "colorloop",
}
return light.SetState(state)
}
// SetState sets the state of a light as per
// http://developers.meethue.com/1_lightsapi.html#16_set_light_state
func (light *Light) SetState(state SetLightState) ([]Result, error) {
params := make(map[string]interface{})
if state.On != "" {
value, _ := strconv.ParseBool(state.On)
params["on"] = value
}
if state.Bri != "" {
params["bri"], _ = strconv.Atoi(state.Bri)
}
if state.Hue != "" {
params["hue"], _ = strconv.Atoi(state.Hue)
}
if state.Sat != "" {
params["sat"], _ = strconv.Atoi(state.Sat)
}
if state.Xy != nil {
params["xy"] = state.Xy
}
if state.Ct != "" {
params["ct"], _ = strconv.Atoi(state.Ct)
}
if state.Alert != "" {
params["alert"] = state.Alert
}
if state.Effect != "" {
params["effect"] = state.Effect
}
if state.TransitionTime != "" {
params["transitiontime"], _ = strconv.Atoi(state.TransitionTime)
}
var results []Result
err := light.bridge.put("/lights/"+light.Id+"/state", ¶ms, &results)
if err != nil {
return nil, err
}
return results, nil
}