Skip to content

Instantly share code, notes, and snippets.

@ptone
Created January 6, 2017 17:49
Show Gist options
  • Save ptone/282a27fedeef45a2e481c55efc8c4abd to your computer and use it in GitHub Desktop.
Save ptone/282a27fedeef45a2e481c55efc8c4abd to your computer and use it in GitHub Desktop.
Mirroring Hue lights by watching Lutron integration local-hub
package main
import (
"log"
"github.com/heatxsink/go-hue/groups"
"github.com/heatxsink/go-hue/lights"
"github.com/heatxsink/go-hue/portal"
lutronlib "github.com/ptone/go-lutron"
)
var (
apiKey string = "---------------"
blinkState lights.State
)
func interpolate(x, in_min, in_max, out_min, out_max int64) int64 {
return (x-in_min)*(out_max-out_min)/(in_max-in_min) + out_min
}
func lutronBrightnessToHue(x int64) int64 {
return interpolate(x, 0, 100, 0, 255)
}
type targets struct {
lights []int
groups []int
}
var mirrorMapping = map[int]targets{
// living room mirror
7: targets{groups: []int{1}},
// pico remote to bedroom
8: targets{groups: []int{2}},
}
func main() {
lutron := lutronlib.NewLutron("192.168.1.21", "./report.json")
err := lutron.Connect()
defer lutron.Disconnect()
if err != nil {
log.Fatal("Unable to connect to lutron bridge %v", err)
}
p := &portal.Portal{
ID: "--------------",
InternalIPAddress: "192.168.1.22",
}
ll := lights.New(p.InternalIPAddress, apiKey)
gg := groups.New(p.InternalIPAddress, apiKey)
watchMessage := &lutronlib.LutronMsg{}
watcher, _ := lutron.Watch(watchMessage)
for msg := range watcher {
if targets, ok := mirrorMapping[msg.Id]; ok {
targetState := lights.State{}
switch msg.Cmd {
case lutronlib.Output:
y := lutronBrightnessToHue(int64(msg.Value))
if y == 0 {
targetState.On = false
// lutron fade to off is slower than Hue
// but this depends on who is turning this off
// value of 19 works well for pico remote, but is too slow for
// apple home app
// targetState.TransitionTime = 19
} else {
targetState.On = true
targetState.Bri = uint8(y)
// remove hue effects when mirroring lutron
// but not when turning off lights
targetState.Sat = 121
targetState.Hue = 15339
}
case lutronlib.Device:
// Buttons:
// 2: top/on
// 3: center/default
// 4: bottom/off
// 5: up/brighten
// 6: down/dim
if msg.Value != 3 {
// only respond to button down event
continue
}
switch msg.Action {
case 4: // off
targetState.On = false
case 2: // reading
targetState.On = true
targetState.Bri = 255
targetState.Sat = 121
targetState.Hue = 15339
case 3:
targetState.On = true
targetState.Bri = 1
targetState.Sat = 251
targetState.CT = 500
targetState.Hue = 10777
case 5:
targetState.On = true
targetState.Hue = 13547
targetState.Bri = 144
targetState.Sat = 199
targetState.CT = 443
}
}
for _, g := range targets.groups {
go gg.SetGroupState(g, targetState)
}
for _, l := range targets.lights {
go ll.SetLightState(l, targetState)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment