forked from stefanwichmann/kelvin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huelight.go
288 lines (246 loc) Β· 10.4 KB
/
huelight.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
// MIT License
//
// Copyright (c) 2019 Stefan Wichmann
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"errors"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
hue "github.com/stefanwichmann/go.hue"
)
var lightsSupportingDimming = []string{"Dimmable light", "Color temperature light", "Color light", "Extended color light"}
var lightsSupportingColorTemperature = []string{"Color temperature light", "Extended color light"}
var lightsSupportingXYColor = []string{"Color light", "Extended color light"}
// HueLight represents a physical hue light.
type HueLight struct {
Name string
HueLight hue.Light
SetColorTemperature int
SetBrightness int
TargetColorTemperature int
TargetColor []float32
TargetBrightness int
CurrentColorTemperature int
CurrentColor []float32
CurrentBrightness int
CurrentColorMode string
SupportsColorTemperature bool
SupportsXYColor bool
Dimmable bool
Reachable bool
On bool
MinimumColorTemperature int
}
func (light *HueLight) initialize(attr hue.LightAttributes) {
// initialize non changing values
light.Name = attr.Name
light.Dimmable = containsString(lightsSupportingDimming, attr.Type)
light.SupportsColorTemperature = containsString(lightsSupportingColorTemperature, attr.Type)
light.SupportsXYColor = containsString(lightsSupportingXYColor, attr.Type)
// set minimum color temperature depending on type
if attr.Type == "Color temperature light" {
light.MinimumColorTemperature = 2200
} else if light.SupportsXYColor {
light.MinimumColorTemperature = 1000
} else if light.SupportsColorTemperature {
light.MinimumColorTemperature = 2000
} else {
light.MinimumColorTemperature = 0
}
log.Debugf("π‘ Light %s - Initialization complete. Identified as %s (ModelID: %s, Version: %s)", light.Name, attr.Type, attr.ModelId, attr.SoftwareVersion)
light.updateCurrentLightState(attr)
}
func (light *HueLight) supportsColorTemperature() bool {
if light.SupportsXYColor || light.SupportsColorTemperature {
return true
}
return false
}
func (light *HueLight) supportsBrightness() bool {
if light.Dimmable {
return true
}
return false
}
func (light *HueLight) updateCurrentLightState(attr hue.LightAttributes) {
light.CurrentColorTemperature = attr.State.Ct
var color []float32
for _, value := range attr.State.Xy {
color = append(color, roundFloat(value, 3))
}
light.CurrentColor = color
light.CurrentBrightness = attr.State.Bri
light.CurrentColorMode = attr.State.ColorMode
if !attr.State.Reachable {
light.Reachable = false
light.On = false
} else {
light.Reachable = true
light.On = attr.State.On
}
}
func (light *HueLight) setLightState(colorTemperature int, brightness int, transitionTime time.Duration) error {
if colorTemperature != -1 && (colorTemperature < 1000 || colorTemperature > 6500) {
log.Warningf("π‘ Light %s - Invalid color temperature %d", light.Name, colorTemperature)
}
if brightness < -1 || brightness > 100 {
log.Warningf("π‘ Light %s - Invalid brightness %d", light.Name, brightness)
}
if colorTemperature != -1 && colorTemperature < light.MinimumColorTemperature {
colorTemperature = light.MinimumColorTemperature
log.Debugf("π‘ Light %s - Adjusted color temperature to light capability of %dK", light.Name, colorTemperature)
}
light.SetColorTemperature = colorTemperature
light.SetBrightness = brightness
// map parameters to target values
light.TargetColorTemperature = mapColorTemperature(colorTemperature)
light.TargetColor = colorTemperatureToXYColor(colorTemperature)
light.TargetBrightness = mapBrightness(brightness)
// Send new state to light bulb
var hueLightState hue.SetLightState
hueLightState.TransitionTime = strconv.Itoa(int(transitionTime / time.Millisecond / 100))
if colorTemperature != -1 {
// Set supported colormodes. If both are, the brigde will prefer xy colors
if light.SupportsXYColor {
hueLightState.Xy = light.TargetColor
}
if light.SupportsColorTemperature {
hueLightState.Ct = strconv.Itoa(light.TargetColorTemperature)
}
}
if brightness != -1 {
if brightness == 0 {
// Target brightness zero should turn the light off.
hueLightState.On = "Off"
} else if light.Dimmable {
hueLightState.Bri = strconv.Itoa(light.TargetBrightness)
}
}
// Send new state to the light
log.Debugf("π‘ HueLight %s - Setting light state to %dK and %d%% brightness (TargetColorTemperature: %d, CurrentColorTemperature: %d, TargetColor: %v, CurrentColor: %v, TargetBrightness: %d, CurrentBrightness: %d, TransitionTime: %s)", light.Name, colorTemperature, brightness, light.TargetColorTemperature, light.CurrentColorTemperature, light.TargetColor, light.CurrentColor, light.TargetBrightness, light.CurrentBrightness, hueLightState.TransitionTime)
result, err := light.HueLight.SetState(hueLightState)
if err != nil {
log.Warningf("π‘ HueLight %s - Setting light state failed: %v (Result: %v)", light.Name, err, result)
return err
}
log.Debugf("π‘ HueLight %s - Light was successfully updated (TargetColorTemperature: %d, CurrentColorTemperature: %d, TargetColor: %v, CurrentColor: %v, TargetBrightness: %d, CurrentBrightness: %d, TransitionTime: %s)", light.Name, light.TargetColorTemperature, light.CurrentColorTemperature, light.TargetColor, light.CurrentColor, light.TargetBrightness, light.CurrentBrightness, hueLightState.TransitionTime)
return nil
}
func (light *HueLight) hasChanged() bool {
if light.SupportsXYColor && light.CurrentColorMode == "xy" {
if !equalsFloat(light.TargetColor, []float32{-1, -1}, 0.001) && !equalsFloat(light.TargetColor, light.CurrentColor, 0.001) {
log.Debugf("π‘ HueLight %s - Color has changed! CurrentColor: %v, TargetColor: %v (%dK)", light.Name, light.CurrentColor, light.TargetColor, light.SetColorTemperature)
return true
}
} else if light.SupportsColorTemperature && light.CurrentColorMode == "ct" {
if light.TargetColorTemperature != -1 && !equalsInt(light.TargetColorTemperature, light.CurrentColorTemperature, 2) {
log.Debugf("π‘ HueLight %s - Color temperature has changed! CurrentColorTemperature: %d, TargetColorTemperatur: %d (%dK)", light.Name, light.CurrentColorTemperature, light.TargetColorTemperature, light.SetColorTemperature)
return true
}
}
if light.Dimmable && light.TargetBrightness != -1 && !equalsInt(light.TargetBrightness, light.CurrentBrightness, 2) {
log.Debugf("π‘ HueLight %s - Brightness has changed! CurrentBrightness: %d, TargetBrightness: %d (%d%%)", light.Name, light.CurrentBrightness, light.TargetBrightness, light.SetBrightness)
return true
}
return false
}
func (light *HueLight) hasState(colorTemperature int, brightness int) bool {
return light.hasColorTemperature(colorTemperature) && light.hasBrightness(brightness)
}
func (light *HueLight) hasColorTemperature(colorTemperature int) bool {
if colorTemperature == -1 || light.TargetColorTemperature == -1 {
return true
}
if !light.SupportsXYColor && !light.SupportsColorTemperature {
return true
}
if colorTemperature < light.MinimumColorTemperature {
colorTemperature = light.MinimumColorTemperature
log.Debugf("π‘ Light %s - Adjusted color temperature to light capability of %dK", light.Name, colorTemperature)
}
if light.SupportsXYColor && light.CurrentColorMode == "xy" {
if equalsFloat(colorTemperatureToXYColor(colorTemperature), light.CurrentColor, 0.001) {
return true
}
return false
} else if light.SupportsColorTemperature && light.CurrentColorMode == "ct" {
if equalsInt(light.CurrentColorTemperature, mapColorTemperature(colorTemperature), 2) {
return true
}
return false
}
// Missmatch in color modes? Log warning for debug purposes and assume unchanged
log.Warningf("π‘ HueLight %s - Unknown color mode in HasColorTemperature method! Current light state: %+v", light.Name, light)
return true
}
func (light *HueLight) hasBrightness(brightness int) bool {
if brightness == -1 || light.TargetBrightness == -1 {
return true
}
if !light.Dimmable {
return true
}
if !equalsInt(light.CurrentBrightness, mapBrightness(brightness), 2) {
return false
}
return true
}
func (light *HueLight) getCurrentColorTemperature() (int, error) {
if !light.hasChanged() {
return light.SetColorTemperature, nil
}
if light.CurrentColorTemperature >= 153 && light.CurrentColorTemperature <= 500 {
return int(float64(1000000) / float64(light.CurrentColorTemperature)), nil
}
return 0, errors.New("Could not determine current color temperature")
}
func (light *HueLight) getCurrentBrightness() (int, error) {
if !light.hasChanged() {
return light.SetBrightness, nil
}
if light.CurrentBrightness >= 1 && light.CurrentBrightness <= 254 {
return int((float64(light.CurrentBrightness) / float64(254)) * float64(100)), nil
}
return 0, errors.New("Could not determine current brightness")
}
func mapColorTemperature(colorTemperature int) int {
if colorTemperature == -1 {
return -1
}
if colorTemperature > 6500 {
colorTemperature = 6500
} else if colorTemperature < 1000 {
colorTemperature = 1000
}
return int((float64(1) / float64(colorTemperature)) * float64(1000000))
}
func mapBrightness(brightness int) int {
if brightness == -1 {
return -1
}
if brightness > 100 {
brightness = 100
} else if brightness < 0 {
brightness = 0
}
return int((float64(brightness) / float64(100)) * float64(254))
}