-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathgroup.go
357 lines (309 loc) · 10.3 KB
/
group.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package huego
import (
"context"
"errors"
"image/color"
)
// Group represents a bridge group https://developers.meethue.com/documentation/groups-api
type Group struct {
Name string `json:"name,omitempty"`
Lights []string `json:"lights,omitempty"`
Type string `json:"type,omitempty"`
GroupState *GroupState `json:"state,omitempty"`
Recycle bool `json:"recycle,omitempty"`
Class string `json:"class,omitempty"`
Stream *Stream `json:"stream,omitempty"`
Locations map[string][]float64 `json:"locations,omitempty"`
State *State `json:"action,omitempty"`
ID int `json:"-"`
bridge *Bridge
}
// GroupState defines the state on a group.
// Can be used to control the state of all lights in a group rather than controlling them individually
type GroupState struct {
AllOn bool `json:"all_on,omitempty"`
AnyOn bool `json:"any_on,omitempty"`
}
// Stream define the stream status of a group
type Stream struct {
ProxyMode string `json:"proxymode,omitempty"`
ProxyNode string `json:"proxynode,omitempty"`
ActiveRaw *bool `json:"active,omitempty"`
OwnerRaw *string `json:"owner,omitempty"`
}
// Active returns the stream active state, and will return false if ActiveRaw is nil
func (s *Stream) Active() bool {
if s.ActiveRaw == nil {
return false
}
return *s.ActiveRaw
}
// Owner returns the stream Owner, and will return an empty string if OwnerRaw is nil
func (s *Stream) Owner() string {
if s.OwnerRaw == nil {
return ""
}
return *s.OwnerRaw
}
// SetState sets the state of the group to s.
func (g *Group) SetState(s State) error {
return g.SetStateContext(context.Background(), s)
}
// SetStateContext sets the state of the group to s.
func (g *Group) SetStateContext(ctx context.Context, s State) error {
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, s)
if err != nil {
return err
}
g.State = &s
return nil
}
// Rename sets the name property of the group
func (g *Group) Rename(new string) error {
return g.RenameContext(context.Background(), new)
}
// RenameContext sets the name property of the group
func (g *Group) RenameContext(ctx context.Context, new string) error {
update := Group{Name: new}
_, err := g.bridge.UpdateGroupContext(ctx, g.ID, update)
if err != nil {
return err
}
g.Name = new
return nil
}
// Off sets the On state of one group to false, turning all lights in the group off
func (g *Group) Off() error {
return g.OffContext(context.Background())
}
// OffContext sets the On state of one group to false, turning all lights in the group off
func (g *Group) OffContext(ctx context.Context) error {
state := State{On: false}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, state)
if err != nil {
return err
}
g.State.On = false
return nil
}
// On sets the On state of one group to true, turning all lights in the group on
func (g *Group) On() error {
return g.OnContext(context.Background())
}
// OnContext sets the On state of one group to true, turning all lights in the group on
func (g *Group) OnContext(ctx context.Context) error {
state := State{On: true}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, state)
if err != nil {
return err
}
g.State.On = true
return nil
}
// IsOn returns true if light state On property is true
func (g *Group) IsOn() bool {
return g.State.On
}
// Bri sets the light brightness state property
func (g *Group) Bri(new uint8) error {
return g.BriContext(context.Background(), new)
}
// BriContext sets the light brightness state property
func (g *Group) BriContext(ctx context.Context, new uint8) error {
update := State{On: true, Bri: new}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Bri = new
g.State.On = true
return nil
}
// Hue sets the light hue state property (0-65535)
func (g *Group) Hue(new uint16) error {
return g.HueContext(context.Background(), new)
}
// HueContext sets the light hue state property (0-65535)
func (g *Group) HueContext(ctx context.Context, new uint16) error {
update := State{On: true, Hue: new}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Hue = new
g.State.On = true
return nil
}
// Sat sets the light saturation state property (0-254)
func (g *Group) Sat(new uint8) error {
return g.SatContext(context.Background(), new)
}
// SatContext sets the light saturation state property (0-254)
func (g *Group) SatContext(ctx context.Context, new uint8) error {
update := State{On: true, Sat: new}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Sat = new
g.State.On = true
return nil
}
// Xy sets the x and y coordinates of a color in CIE color space. (0-1 per value)
func (g *Group) Xy(new []float32) error {
return g.XyContext(context.Background(), new)
}
// XyContext sets the x and y coordinates of a color in CIE color space. (0-1 per value)
func (g *Group) XyContext(ctx context.Context, new []float32) error {
update := State{On: true, Xy: new}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Xy = new
g.State.On = true
return nil
}
// Ct sets the light color temperature state property
func (g *Group) Ct(new uint16) error {
return g.CtContext(context.Background(), new)
}
// CtContext sets the light color temperature state property
func (g *Group) CtContext(ctx context.Context, new uint16) error {
update := State{On: true, Ct: new}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Ct = new
g.State.On = true
return nil
}
// Col sets the light color as RGB (will be converted to xy)
func (g *Group) Col(new color.Color) error {
return g.ColContext(context.Background(), new)
}
// ColContext sets the light color as RGB (will be converted to xy)
func (g *Group) ColContext(ctx context.Context, new color.Color) error {
xy, bri := ConvertRGBToXy(new)
update := State{On: true, Xy: xy, Bri: bri}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Xy = xy
g.State.Bri = bri
g.State.On = true
return nil
}
// Scene sets the scene by it's identifier of the scene you wish to recall
func (g *Group) Scene(scene string) error {
return g.SceneContext(context.Background(), scene)
}
// SceneContext sets the scene by it's identifier of the scene you wish to recall
func (g *Group) SceneContext(ctx context.Context, scene string) error {
update := State{On: true, Scene: scene}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Scene = scene
g.State.On = true
return nil
}
// TransitionTime sets the duration of the transition from the light’s current state to the new state
func (g *Group) TransitionTime(new uint16) error {
return g.TransitionTimeContext(context.Background(), new)
}
// TransitionTimeContext sets the duration of the transition from the light’s current state to the new state
func (g *Group) TransitionTimeContext(ctx context.Context, new uint16) error {
update := State{On: g.State.On, TransitionTime: new}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.TransitionTime = new
return nil
}
// Effect the dynamic effect of the lights in the group, currently “none” and “colorloop” are supported
func (g *Group) Effect(new string) error {
return g.EffectContext(context.Background(), new)
}
// EffectContext the dynamic effect of the lights in the group, currently “none” and “colorloop” are supported
func (g *Group) EffectContext(ctx context.Context, new string) error {
update := State{On: true, Effect: new}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Effect = new
g.State.On = true
return nil
}
// Alert makes the lights in the group blink in its current color. Supported values are:
// “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 alert is set to "none".
func (g *Group) Alert(new string) error {
return g.AlertContext(context.Background(), new)
}
// AlertContext makes the lights in the group blink in its current color. Supported values are:
// “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 alert is set to "none".
func (g *Group) AlertContext(ctx context.Context, new string) error {
update := State{On: true, Alert: new}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Effect = new
g.State.On = true
return nil
}
// EnableStreaming enables streaming for the group by setting the Stream Active property to true
func (g *Group) EnableStreaming() error {
return g.EnableStreamingContext(context.Background())
}
// EnableStreamingContext enables streaming for the group by setting the Stream Active property to true
func (g *Group) EnableStreamingContext(ctx context.Context) error {
if g.Type != "Entertainment" {
return errors.New("must be an entertainment group to enable streaming")
}
active := true
update := Group{
Stream: &Stream{
ActiveRaw: &active,
},
}
_, err := g.bridge.UpdateGroupContext(ctx, g.ID, update)
if err != nil {
return err
}
g.Stream.ActiveRaw = &active
g.Stream.OwnerRaw = &g.bridge.User
return nil
}
// DisableStreaming disabled streaming for the group by setting the Stream Active property to false
func (g *Group) DisableStreaming() error {
return g.DisableStreamingContext(context.Background())
}
// DisableStreamingContext disabled streaming for the group by setting the Stream Active property to false
func (g *Group) DisableStreamingContext(ctx context.Context) error {
if g.Type != "Entertainment" {
return errors.New("must be an entertainment group to disable streaming")
}
active := false
update := Group{
Stream: &Stream{
ActiveRaw: &active,
},
}
_, err := g.bridge.UpdateGroupContext(ctx, g.ID, update)
if err != nil {
return err
}
g.Stream.ActiveRaw = &active
g.Stream.OwnerRaw = nil
return nil
}