forked from amimof/huego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
light.go
183 lines (167 loc) · 5.12 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
package huego
// Light represents a bridge light https://developers.meethue.com/documentation/lights-api
type Light struct {
State *State `json:"state,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
ModelID string `json:"modelid,omitempty"`
ManufacturerName string `json:"manufacturername,omitempty"`
UniqueID string `json:"uniqueid,omitempty"`
SwVersion string `json:"swversion,omitempty"`
SwConfigID string `json:"swconfigid,omitempty"`
ProductID string `json:"productid,omitempty"`
ID int `json:"-"`
bridge *Bridge
}
// State defines the attributes and properties of a light
type State struct {
On bool `json:"on"`
Bri uint8 `json:"bri,omitempty"`
Hue uint16 `json:"hue,omitempty"`
Sat uint8 `json:"sat,omitempty"`
Xy []float32 `json:"xy,omitempty"`
Ct uint16 `json:"ct,omitempty"`
Alert string `json:"alert,omitempty"`
Effect string `json:"effect,omitempty"`
TransitionTime uint16 `json:"transitiontime,omitempty"`
BriInc int `json:"bri_inc,omitempty"`
SatInc int `json:"sat_inc,omitempty"`
HueInc int `json:"hue_inc,omitempty"`
CtInc int `json:"ct_inc,omitempty"`
XyInc int `json:"xy_inc,omitempty"`
ColorMode string `json:"colormode,omitempty"`
Reachable bool `json:"reachable,omitempty"`
Scene string `json:"scene,omitempty"`
}
// NewLight defines a list of lights discovered the last time the bridge performed a light discovery.
// Also stores the timestamp the last time a discovery was performed.
type NewLight struct {
Lights []string
LastScan string `json:"lastscan"`
}
// SetState sets the state of the light to s.
func (l *Light) SetState(s State) error {
_, err := l.bridge.SetLightState(l.ID, s)
if err != nil {
return err
}
l.State = &s
return nil
}
// Off sets the On state of one light to false, turning it off
func (l *Light) Off() error {
state := State{On: false}
_, err := l.bridge.SetLightState(l.ID, state)
if err != nil {
return err
}
l.State.On = false
return nil
}
// On sets the On state of one light to true, turning it on
func (l *Light) On() error {
state := State{On: true}
_, err := l.bridge.SetLightState(l.ID, state)
if err != nil {
return err
}
l.State.On = true
return nil
}
// IsOn returns true if light state On property is true
func (l *Light) IsOn() bool {
return l.State.On
}
// Rename sets the name property of the light
func (l *Light) Rename(new string) error {
update := Light{Name: new}
_, err := l.bridge.UpdateLight(l.ID, update)
if err != nil {
return err
}
l.Name = new
return nil
}
// Bri sets the light brightness state property
func (l *Light) Bri(new uint8) error {
update := State{On: true, Bri: new}
_, err := l.bridge.SetLightState(l.ID, update)
if err != nil {
return err
}
l.State.Bri = new
return nil
}
// Hue sets the light hue state property (0-65535)
func (l *Light) Hue(new uint16) error {
update := State{On: true, Hue: new}
_, err := l.bridge.SetLightState(l.ID, update)
if err != nil {
return err
}
l.State.Hue = new
return nil
}
// Sat sets the light saturation state property (0-254)
func (l *Light) Sat(new uint8) error {
update := State{On: true, Sat: new}
_, err := l.bridge.SetLightState(l.ID, update)
if err != nil {
return err
}
l.State.Sat = new
return nil
}
// Xy sets the x and y coordinates of a color in CIE color space. (0-1 per value)
func (l *Light) Xy(new []float32) error {
update := State{On: true, Xy: new}
_, err := l.bridge.SetLightState(l.ID, update)
if err != nil {
return err
}
l.State.Xy = new
return nil
}
// Ct sets the light color temperature state property
func (l *Light) Ct(new uint16) error {
update := State{On: true, Ct: new}
_, err := l.bridge.SetLightState(l.ID, update)
if err != nil {
return err
}
l.State.Ct = new
return nil
}
// TransitionTime sets the duration of the transition from the light’s current state to the new state
func (l *Light) TransitionTime(new uint16) error {
update := State{On: l.State.On, TransitionTime: new}
_, err := l.bridge.SetLightState(l.ID, update)
if err != nil {
return err
}
l.State.TransitionTime = new
return nil
}
// Effect the dynamic effect of the light, currently “none” and “colorloop” are supported
func (l *Light) Effect(new string) error {
update := State{On: true, Effect: new}
_, err := l.bridge.SetLightState(l.ID, update)
if err != nil {
return err
}
l.State.Effect = new
return nil
}
// Alert makes the light 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 (l *Light) Alert(new string) error {
update := State{On: true, Alert: new}
_, err := l.bridge.SetLightState(l.ID, update)
if err != nil {
return err
}
l.State.Effect = new
return nil
}