Fixed light flicker at start and lights do not get turned of when they are already on
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dreaded_X 2022-05-24 18:14:23 +02:00
parent e9e26b9623
commit dc0a309b41
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9

25
main.go
View File

@ -165,13 +165,18 @@ func main() {
if !ok { if !ok {
pass = "test" pass = "test"
} }
login, ok := os.LookupEnv("HUE_BRIDGE") login, _ := os.LookupEnv("HUE_BRIDGE")
halt := make(chan os.Signal, 1) halt := make(chan os.Signal, 1)
signal.Notify(halt, os.Interrupt, syscall.SIGTERM) signal.Notify(halt, os.Interrupt, syscall.SIGTERM)
bridge, _ := huego.Discover() // bridge, _ := huego.Discover()
bridge = bridge.Login(login) // 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) livingRoom, _ := bridge.GetGroup(groupId)
opts := MQTT.NewClientOptions().AddBroker(fmt.Sprintf("%s:%s", host, port)) opts := MQTT.NewClientOptions().AddBroker(fmt.Sprintf("%s:%s", host, port))
@ -200,7 +205,7 @@ func main() {
// Create the ticker, but stop it // Create the ticker, but stop it
ticker := time.NewTicker(time.Second) ticker := time.NewTicker(time.Second)
ticker.Stop() // ticker.Stop()
var brightness uint8 = 1 var brightness uint8 = 1
@ -245,7 +250,8 @@ events:
fmt.Println("\tGradually turning on lights in the living room") fmt.Println("\tGradually turning on lights in the living room")
// Start the ticker to gradually turn on the living room lights // Start the ticker to gradually turn on the living room lights
ticker.Reset(1200 * time.Millisecond) 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.Bri(brightness)
livingRoom.Ct(Temperature) livingRoom.Ct(Temperature)
} }
@ -256,10 +262,13 @@ events:
sunsetTimer.Reset(sunset.Sub(time.Now())) sunsetTimer.Reset(sunset.Sub(time.Now()))
case <-ticker.C: case <-ticker.C:
fmt.Println("Setting brightness:", brightness)
livingRoom.Bri(brightness)
brightness++ brightness++
if !livingRoom.IsOn() || livingRoom.State.Bri < brightness {
fmt.Println("Setting brightness:", brightness)
livingRoom.Bri(brightness)
livingRoom.Ct(Temperature)
}
if brightness == 0xff { if brightness == 0xff {
fmt.Println("Lights are now on, stopping ticker") fmt.Println("Lights are now on, stopping ticker")
ticker.Stop() ticker.Stop()