-
Notifications
You must be signed in to change notification settings - Fork 17
/
hklifxd.go
284 lines (224 loc) · 7.48 KB
/
hklifxd.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
package main
import (
"flag"
"math"
"os"
"time"
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/log"
"github.com/pdf/golifx"
"github.com/pdf/golifx/common"
"github.com/pdf/golifx/protocol"
"github.com/pdf/golifx/protocol/v2/shared"
)
const (
// from https://github.com/LIFX/LIFXKit/blob/master/LIFXKit/Classes-Common/LFXHSBKColor.h
HSBKKelvinDefault = uint16(3500)
HSBKKelvinMin = uint16(2500)
HSBKKelvinMax = uint16(9000)
HueMax float64 = 360
BrightnessMax int = 100
SaturationMax float64 = 100
)
type HKLight struct {
accessory *accessory.ColoredLightbulb
transport hc.Transport
sub *common.Subscription
}
var (
lights map[uint64]*HKLight
pin string
transitionDuration time.Duration
)
func Connect() {
client, err := golifx.NewClient(&protocol.V2{Reliable: true})
if err != nil {
log.Info.Panic("Failed to initiliaze the client:", err)
}
client.SetDiscoveryInterval(30 * time.Second)
sub := client.Subscribe()
for {
event := <-sub.Events()
switch event.(type) {
case common.EventNewLocation:
log.Debug.Printf("Discovered Location %s", event.(common.EventNewLocation).Location.GetLabel())
case common.EventNewGroup:
log.Debug.Printf("Discovered Group %s", event.(common.EventNewGroup).Group.GetLabel())
case common.EventNewDevice:
label, _ := event.(common.EventNewDevice).Device.GetLabel()
log.Debug.Printf("Discovered Device %s", label)
go NewDevice(event.(common.EventNewDevice).Device)
case common.EventExpiredLocation:
log.Debug.Printf("Expired Location %s", event.(common.EventExpiredLocation).Location.GetLabel())
case common.EventExpiredGroup:
log.Debug.Printf("Expired Group %s", event.(common.EventExpiredGroup).Group.GetLabel())
case common.EventExpiredDevice:
label, _ := event.(common.EventExpiredDevice).Device.GetLabel()
log.Debug.Printf("Expired Device %s", label)
ExpireDevice(event.(common.EventExpiredDevice).Device)
default:
log.Debug.Printf("Unknown Client Event: %T", event)
}
}
}
func NewDevice(device common.Device) {
if light, ok := device.(common.Light); ok {
hkLight := GetHKLight(light)
hkLight.sub = light.Subscribe()
for {
event := <-hkLight.sub.Events()
switch event.(type) {
case common.EventUpdateLabel:
log.Debug.Printf("Updated Label for %s to %s", hkLight.accessory.Info.Name.GetValue(), event.(common.EventUpdateLabel).Label)
// TODO Add support for label changes to HomeControl
log.Debug.Printf("Unsupported by HomeControl")
case common.EventUpdatePower:
log.Debug.Printf("Updated Power for %s", hkLight.accessory.Info.Name.GetValue())
hkLight.accessory.Lightbulb.On.SetValue(event.(common.EventUpdatePower).Power)
case common.EventUpdateColor:
log.Debug.Printf("Updated Color for %s", hkLight.accessory.Info.Name.GetValue())
hue, saturation, brightness := ConvertLIFXColor(event.(common.EventUpdateColor).Color)
hkLight.accessory.Lightbulb.Hue.SetValue(hue)
hkLight.accessory.Lightbulb.Saturation.SetValue(saturation)
hkLight.accessory.Lightbulb.Brightness.SetValue(int(brightness))
case shared.EventBroadcastSent:
// Suppress event
default:
log.Debug.Printf("Unknown Device Event: %T", event)
}
}
} else {
log.Info.Println("Unsupported Device")
}
}
func ExpireDevice(device common.Device) {
if light, ok := device.(common.Light); ok {
if hkLight, found := lights[light.ID()]; found == true {
hkLight.sub.Close()
hkLight.transport.Stop()
delete(lights, light.ID())
}
} else {
log.Info.Println("Unsupported Device")
}
}
func GetHKLight(light common.Light) *HKLight {
hkLight, found := lights[light.ID()]
if found {
return hkLight
}
label, _ := light.GetLabel()
log.Debug.Printf("Creating New HKLight for %s", label)
info := accessory.Info{
Name: label,
Manufacturer: "LIFX",
}
acc := accessory.NewColoredLightbulb(info)
power, _ := light.GetPower()
acc.Lightbulb.On.SetValue(power)
color, _ := light.GetColor()
hue, saturation, brightness := ConvertLIFXColor(color)
acc.Lightbulb.Brightness.SetValue(int(brightness))
acc.Lightbulb.Saturation.SetValue(saturation)
acc.Lightbulb.Hue.SetValue(hue)
config := hc.Config{Pin: pin}
transport, err := hc.NewIPTransport(config, acc.Accessory)
if err != nil {
log.Info.Panic(err)
}
go func() {
transport.Start()
}()
hkLight = &HKLight{acc, transport, nil}
lights[light.ID()] = hkLight
acc.OnIdentify(func() {
timeout := 1 * time.Second
for i := 0; i < 4; i++ {
ToggleLight(light)
time.Sleep(timeout)
}
})
acc.Lightbulb.On.OnValueRemoteUpdate(func(power bool) {
log.Debug.Printf("Changed State for %s", label)
light.SetPowerDuration(power, transitionDuration)
})
updateColor := func(light common.Light) {
currentPower, _ := light.GetPower()
// HAP: [0...360]
// LIFX: [0...MAX_UINT16]
hue := acc.Lightbulb.Hue.GetValue()
// HAP: [0...100]
// LIFX: [0...MAX_UINT16]
saturation := acc.Lightbulb.Saturation.GetValue()
// HAP: [0...100]
// LIFX: [0...MAX_UINT16]
brightness := acc.Lightbulb.Brightness.GetValue()
// [HSBKKelvinMin..HSBKKelvinMax]
kelvin := HSBKKelvinDefault
lifxHue := math.MaxUint16 * float64(hue) / float64(HueMax)
lifxSaturation := math.MaxUint16 * float64(saturation) / float64(SaturationMax)
lifxBrightness := math.MaxUint16 * float64(brightness) / float64(BrightnessMax)
color := common.Color{
uint16(lifxHue),
uint16(lifxSaturation),
uint16(lifxBrightness),
kelvin,
}
light.SetColor(color, transitionDuration)
if brightness > 0 && !currentPower {
log.Debug.Printf("Color changed for %s, turning on power.", label)
light.SetPowerDuration(true, transitionDuration)
} else if brightness == 0 && currentPower {
log.Debug.Printf("Color changed for %s, but brightness = 0 turning off power.", label)
light.SetPower(false)
}
}
acc.Lightbulb.Hue.OnValueRemoteUpdate(func(value float64) {
log.Debug.Printf("Changed Hue for %s to %f", label, value)
updateColor(light)
})
acc.Lightbulb.Saturation.OnValueRemoteUpdate(func(value float64) {
log.Debug.Printf("Changed Saturation for %s to %f", label, value)
updateColor(light)
})
acc.Lightbulb.Brightness.OnValueRemoteUpdate(func(value int) {
log.Debug.Printf("Changed Brightness for %s to %d", label, value)
updateColor(light)
})
return hkLight
}
func Round(f float64) float64 {
return math.Floor(f + 0.5)
}
func ConvertLIFXColor(color common.Color) (float64, float64, float64) {
hue := float64(color.Hue) / float64(math.MaxUint16) * float64(HueMax)
saturation := float64(color.Saturation) / float64(math.MaxUint16) * float64(SaturationMax)
brightness := float64(color.Brightness) / float64(math.MaxUint16) * float64(BrightnessMax)
// Return rounded value, HomeKit only uses integer value
return Round(hue), Round(saturation), Round(brightness)
}
func ToggleLight(light common.Light) {
power, _ := light.GetPower()
light.SetPower(!power)
}
func main() {
lights = map[uint64]*HKLight{}
pinArg := flag.String("pin", "", "PIN used to pair the LIFX bulbs with HomeKit")
verboseArg := flag.Bool("v", false, "Whether or not log output is displayed")
transitionArg := flag.Float64("transition-duration", 1, "Transition time in seconds")
flag.Parse()
pin = *pinArg
if *verboseArg {
log.Debug.Enable()
}
transitionDuration = time.Duration(*transitionArg) * time.Second
hc.OnTermination(func() {
for _, light := range lights {
<-light.transport.Stop()
}
time.Sleep(100 * time.Millisecond)
os.Exit(1)
})
Connect()
}