From dc0a309b412221f3fb474c66e73b2977b33d3c3d Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Tue, 24 May 2022 18:14:23 +0200 Subject: [PATCH] Fixed light flicker at start and lights do not get turned of when they are already on --- main.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index a84ddd7..1bce713 100644 --- a/main.go +++ b/main.go @@ -165,13 +165,18 @@ func main() { if !ok { pass = "test" } - login, ok := os.LookupEnv("HUE_BRIDGE") + login, _ := os.LookupEnv("HUE_BRIDGE") halt := make(chan os.Signal, 1) signal.Notify(halt, os.Interrupt, syscall.SIGTERM) - bridge, _ := huego.Discover() - bridge = bridge.Login(login) + // bridge, _ := huego.Discover() + // bridge = bridge.Login(login) + // @TODO Let's hope the IP does not change, should probably set a static IP + bridge := huego.New("10.0.0.146", login) + if bridge == nil { + panic("Bridge is nil") + } livingRoom, _ := bridge.GetGroup(groupId) opts := MQTT.NewClientOptions().AddBroker(fmt.Sprintf("%s:%s", host, port)) @@ -200,7 +205,7 @@ func main() { // Create the ticker, but stop it ticker := time.NewTicker(time.Second) - ticker.Stop() + // ticker.Stop() var brightness uint8 = 1 @@ -245,7 +250,8 @@ events: fmt.Println("\tGradually turning on lights in the living room") // Start the ticker to gradually turn on the living room lights ticker.Reset(1200 * time.Millisecond) - if (livingRoom.State.Bri < brightness) { + if !livingRoom.IsOn() || livingRoom.State.Bri < brightness { + fmt.Println("Setting brightness:", brightness) livingRoom.Bri(brightness) livingRoom.Ct(Temperature) } @@ -256,10 +262,13 @@ events: sunsetTimer.Reset(sunset.Sub(time.Now())) case <-ticker.C: - fmt.Println("Setting brightness:", brightness) - livingRoom.Bri(brightness) - brightness++ + if !livingRoom.IsOn() || livingRoom.State.Bri < brightness { + fmt.Println("Setting brightness:", brightness) + livingRoom.Bri(brightness) + livingRoom.Ct(Temperature) + } + if brightness == 0xff { fmt.Println("Lights are now on, stopping ticker") ticker.Stop()