aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 6253cbb27933fc9bc57ab3a9b93382c3d0bc78bc (plain)
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
package main

import (
	"log"
	"os"
	"time"

	hue "github.com/benburwell/gohue"
)

const username = "phlux"

func main() {
	var config PhluxConfig
	config.Read()
	log.Println("Config:", config)

	bridges, err := hue.FindBridges()
	if err != nil {
		log.Fatalf("Error finding bridges: %s\n", err.Error())
	}
	log.Printf("Found %d bridge(s)\n", len(bridges))
	desiredColorTemp := getDesiredColorTemperature(time.Now(), config.Latitude, config.Longitude)
	for _, bridge := range bridges {
		log.Printf("Bridge: %s\n", bridge.IPAddress)
		updateBridge(bridge, desiredColorTemp)
	}
}

func updateBridge(bridge hue.Bridge, ct ColorTemperature) {
	//username, err := bridge.CreateUser(username)
	//if err != nil {
	//	panic("Could not create user on bridge")
	//}
	//fmt.Printf("Made user %s for bridge %s\n", username, bridge.IPAddress)
	err := bridge.Login(os.Getenv("HUE_LOGIN"))
	if err != nil {
		log.Fatalf("Could not log in to bridge: %s\n", err.Error())
	}
	log.Println("Logged in to bridge")

	config, err := bridge.GetConfig()
	if err != nil {
		log.Fatalf("Could not get bridge info: %s\n", err.Error())
	}
	log.Printf("Bridge name: %s\n", config.Name)
	log.Printf("Bridge ID: %s\n", config.BridgeID)

	lights, err := bridge.GetAllLights()
	if err != nil {
		log.Fatalf("Error getting lights: %s\n", err.Error())
	}
	log.Printf("Found %d lights\n", len(lights))
	for _, light := range lights {
		updateLight(light, ct)
	}
}

func updateLight(light hue.Light, ct ColorTemperature) {
	log.Printf("Light %d: %s (%s)\n", light.Index, light.Name, light.Type)
	if supportsColorTemp(light) {
		log.Printf("  CT range: %d-%d\n", light.Capabilities.Control.CT.Min, light.Capabilities.Control.CT.Max)
		newCt := ct.TranslateForLight(light)
		log.Printf("  Setting CT to %d\n", newCt)
		light.SetState(hue.LightState{
			On: light.State.On,
			CT: newCt,
		})
	}
}