Skip to content

Commit

Permalink
Bring adjustable transition time back
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanwichmann committed Mar 24, 2019
1 parent ff71e6c commit 15c0782
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
19 changes: 11 additions & 8 deletions huelight.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
// SOFTWARE.
package main

import log "github.com/Sirupsen/logrus"
import hue "github.com/stefanwichmann/go.hue"
import "strconv"
import (
"errors"
"strconv"
"time"

import "errors"
log "github.com/Sirupsen/logrus"
hue "github.com/stefanwichmann/go.hue"
)

var lightsSupportingDimming = []string{"Dimmable Light", "Color Temperature Light", "Color Light", "Extended Color Light"}
var lightsSupportingColorTemperature = []string{"Color Temperature Light", "Extended Color Light"}
Expand Down Expand Up @@ -107,7 +110,7 @@ func (light *HueLight) updateCurrentLightState(attr hue.LightAttributes) {
}
}

func (light *HueLight) setLightState(colorTemperature int, brightness int) error {
func (light *HueLight) setLightState(colorTemperature int, brightness int, transitionTime time.Duration) error {
if colorTemperature != -1 && (colorTemperature < 2000 || colorTemperature > 6500) {
log.Warningf("💡 Light %s - Invalid color temperature %d", light.Name, colorTemperature)
}
Expand All @@ -130,7 +133,7 @@ func (light *HueLight) setLightState(colorTemperature int, brightness int) error

// Send new state to light bulb
var hueLightState hue.SetLightState
hueLightState.TransitionTime = "0"
hueLightState.TransitionTime = strconv.Itoa(int(transitionTime / time.Millisecond / 100))

if colorTemperature != -1 {
// Set supported colormodes. If both are, the brigde will prefer xy colors
Expand All @@ -152,14 +155,14 @@ func (light *HueLight) setLightState(colorTemperature int, brightness int) error
}

// Send new state to the light
log.Debugf("💡 HueLight %s - Setting light state to %dK and %d%% brightness (TargetColorTemperature: %d, CurrentColorTemperature: %d, TargetColor: %v, CurrentColor: %v, TargetBrightness: %d, CurrentBrightness: %d)", light.Name, colorTemperature, brightness, light.TargetColorTemperature, light.CurrentColorTemperature, light.TargetColor, light.CurrentColor, light.TargetBrightness, light.CurrentBrightness)
log.Debugf("💡 HueLight %s - Setting light state to %dK and %d%% brightness (TargetColorTemperature: %d, CurrentColorTemperature: %d, TargetColor: %v, CurrentColor: %v, TargetBrightness: %d, CurrentBrightness: %d, TransitionTime: %s)", light.Name, colorTemperature, brightness, light.TargetColorTemperature, light.CurrentColorTemperature, light.TargetColor, light.CurrentColor, light.TargetBrightness, light.CurrentBrightness, hueLightState.TransitionTime)
result, err := light.HueLight.SetState(hueLightState)
if err != nil {
log.Warningf("💡 HueLight %s - Setting light state failed: %v (Result: %v)", light.Name, err, result)
return err
}

log.Debugf("💡 HueLight %s - Light was successfully updated (TargetColorTemperature: %d, CurrentColorTemperature: %d, TargetColor: %v, CurrentColor: %v, TargetBrightness: %d, CurrentBrightness: %d)", light.Name, light.TargetColorTemperature, light.CurrentColorTemperature, light.TargetColor, light.CurrentColor, light.TargetBrightness, light.CurrentBrightness)
log.Debugf("💡 HueLight %s - Light was successfully updated (TargetColorTemperature: %d, CurrentColorTemperature: %d, TargetColor: %v, CurrentColor: %v, TargetBrightness: %d, CurrentBrightness: %d, TransitionTime: %s)", light.Name, light.TargetColorTemperature, light.CurrentColorTemperature, light.TargetColor, light.CurrentColor, light.TargetBrightness, light.CurrentBrightness, hueLightState.TransitionTime)
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion kelvin.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var lights []*Light
const lightUpdateInterval = 1 * time.Second
const stateUpdateInterval = 1 * time.Minute
const timeBetweenCalls = 200 * time.Millisecond // see https://developers.meethue.com/develop/application-design-guidance/hue-system-performance/
const lightTransistionTime = 400 * time.Millisecond

func main() {
flag.Parse()
Expand Down Expand Up @@ -150,7 +151,7 @@ func main() {
currentLightState, found := states[light.ID]
if found {
light.updateCurrentLightState(currentLightState)
updated, err := light.update()
updated, err := light.update(lightTransistionTime)
if err != nil {
log.Warningf("🤖 Light %s - Failed to update light: %v", light.Name, err)
}
Expand Down
12 changes: 6 additions & 6 deletions light.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
hue "github.com/stefanwichmann/go.hue"
)

const initializationDuration = 10 * time.Second
const initializationDuration = 5 * time.Second

// Light represents a light kelvin can automate in your system.
type Light struct {
Expand All @@ -54,7 +54,7 @@ func (light *Light) updateCurrentLightState(attr hue.LightAttributes) error {
return nil
}

func (light *Light) update() (bool, error) {
func (light *Light) update(transistionTime time.Duration) (bool, error) {
// Is the light associated to any schedule?
if !light.Scheduled {
return false, nil
Expand Down Expand Up @@ -98,7 +98,7 @@ func (light *Light) update() (bool, error) {
if light.Schedule.enableWhenLightsAppear {
log.Printf("💡 Light %s - Initializing state to %vK at %v%% brightness.", light.Name, light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)

err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness, transistionTime)
if err != nil {
log.Debugf("💡 Light %s - Could not initialize light after %v", light.Name, time.Since(light.Appearance))
return true, err
Expand All @@ -125,7 +125,7 @@ func (light *Light) update() (bool, error) {
light.Initializing = true

// set correct target lightstate on HueLight
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness, transistionTime)
if err != nil {
return true, err
}
Expand All @@ -148,7 +148,7 @@ func (light *Light) update() (bool, error) {
}

if hasChanged {
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness, transistionTime)
if err != nil {
return true, err
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (light *Light) update() (bool, error) {
}

// Light is turned on and in automatic state. Set target lightstate.
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness)
err := light.HueLight.setLightState(light.TargetLightState.ColorTemperature, light.TargetLightState.Brightness, transistionTime)
if err != nil {
return true, err
}
Expand Down
2 changes: 1 addition & 1 deletion webinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func activateLightHandler(w http.ResponseWriter, r *http.Request) {
if l.ID == lightID {
log.Printf("💡 Light %s - Activating light state %+v as requested by %s", l.Name, t, r.RemoteAddr)
l.Automatic = false
l.HueLight.setLightState(t.ColorTemperature, t.Brightness)
l.HueLight.setLightState(t.ColorTemperature, t.Brightness, 0)
}
}
w.Write([]byte("success"))
Expand Down

0 comments on commit 15c0782

Please sign in to comment.