Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package huego
import (
"context"
"errors"
"image/color"
)

// Group represents a bridge group https://developers.meethue.com/documentation/groups-api
Expand Down Expand Up @@ -134,6 +135,7 @@ func (g *Group) BriContext(ctx context.Context, new uint8) error {
return err
}
g.State.Bri = new
g.State.On = true
return nil
}

Expand All @@ -150,6 +152,7 @@ func (g *Group) HueContext(ctx context.Context, new uint16) error {
return err
}
g.State.Hue = new
g.State.On = true
return nil
}

Expand All @@ -166,6 +169,7 @@ func (g *Group) SatContext(ctx context.Context, new uint8) error {
return err
}
g.State.Sat = new
g.State.On = true
return nil
}

Expand All @@ -182,6 +186,7 @@ func (g *Group) XyContext(ctx context.Context, new []float32) error {
return err
}
g.State.Xy = new
g.State.On = true
return nil
}

Expand All @@ -198,6 +203,27 @@ func (g *Group) CtContext(ctx context.Context, new uint16) error {
return err
}
g.State.Ct = new
g.State.On = true
return nil
}

// Ct sets the light color temperature state property
func (g *Group) Col(new color.Color) error {
return g.ColContext(context.Background(), new)
}

// CtContext sets the light color temperature state property
func (g *Group) ColContext(ctx context.Context, new color.Color) error {
xy, bri := ConvertRGBToXy(new)

update := State{On: true, Xy: xy, Bri: bri}
_, err := g.bridge.SetGroupStateContext(ctx, g.ID, update)
if err != nil {
return err
}
g.State.Xy = xy
g.State.Bri = bri
g.State.On = true
return nil
}

Expand All @@ -214,6 +240,7 @@ func (g *Group) SceneContext(ctx context.Context, scene string) error {
return err
}
g.State.Scene = scene
g.State.On = true
return nil
}

Expand Down Expand Up @@ -246,6 +273,7 @@ func (g *Group) EffectContext(ctx context.Context, new string) error {
return err
}
g.State.Effect = new
g.State.On = true
return nil
}

Expand All @@ -268,6 +296,7 @@ func (g *Group) AlertContext(ctx context.Context, new string) error {
return err
}
g.State.Effect = new
g.State.On = true
return nil
}

Expand Down
60 changes: 59 additions & 1 deletion light.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package huego

import "context"
import (
"context"
"image/color"
"math"
)

// Light represents a bridge light https://developers.meethue.com/documentation/lights-api
type Light struct {
Expand Down Expand Up @@ -126,6 +130,7 @@ func (l *Light) BriContext(ctx context.Context, new uint8) error {
return err
}
l.State.Bri = new
l.State.On = true
return nil
}

Expand All @@ -142,6 +147,7 @@ func (l *Light) HueContext(ctx context.Context, new uint16) error {
return err
}
l.State.Hue = new
l.State.On = true
return nil
}

Expand All @@ -158,6 +164,7 @@ func (l *Light) SatContext(ctx context.Context, new uint8) error {
return err
}
l.State.Sat = new
l.State.On = true
return nil
}

Expand All @@ -174,6 +181,7 @@ func (l *Light) XyContext(ctx context.Context, new []float32) error {
return err
}
l.State.Xy = new
l.State.On = true
return nil
}

Expand All @@ -190,6 +198,27 @@ func (l *Light) CtContext(ctx context.Context, new uint16) error {
return err
}
l.State.Ct = new
l.State.On = true
return nil
}

// Ct sets the light color temperature state property
func (l *Light) Col(new color.Color) error {
return l.ColContext(context.Background(), new)
}

// CtContext sets the light color temperature state property
func (l *Light) ColContext(ctx context.Context, new color.Color) error {
xy, bri := ConvertRGBToXy(new)

update := State{On: true, Xy: xy, Bri: bri}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.Xy = xy
l.State.Bri = bri
l.State.On = true
return nil
}

Expand Down Expand Up @@ -222,6 +251,7 @@ func (l *Light) EffectContext(ctx context.Context, new string) error {
return err
}
l.State.Effect = new
l.State.On = true
return nil
}

Expand All @@ -246,3 +276,31 @@ func (l *Light) AlertContext(ctx context.Context, new string) error {
l.State.Effect = new
return nil
}

func ConvertRGBToXy(newcolor color.Color) ([]float32, uint8) {
r, g, b, _ := newcolor.RGBA()
rf := float64(r) / 65536.0
gf := float64(g) / 65536.0
bf := float64(b) / 65536.0

rf = gammaCorrect(rf)
gf = gammaCorrect(gf)
bf = gammaCorrect(bf)

X := float32(rf*0.649926 + gf*0.103455 + bf*0.197109)
Y := float32(rf*0.234327 + gf*0.743075 + bf*0.022598)
Z := float32(rf*0.0000000 + gf*0.053077 + bf*1.035763)

x := X / (X + Y + Z)
y := Y / (X + Y + Z)

xy := []float32{x, y}
return xy, uint8(Y * 254)
}

func gammaCorrect(value float64) float64 {
if value > 0.04045 {
return math.Pow((value+0.055)/(1.0+0.055), 2.4)
}
return (value / 12.92)
}
12 changes: 11 additions & 1 deletion light_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package huego

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetLights(t *testing.T) {
Expand Down Expand Up @@ -149,6 +150,7 @@ func TestTurnOffLight(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.False(t, light.IsOn())
t.Logf("Turned off light with id %d", light.ID)

b.Host = badHostname
Expand All @@ -167,6 +169,7 @@ func TestTurnOnLight(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.True(t, light.IsOn())
t.Logf("Turned on light with id %d", light.ID)

b.Host = badHostname
Expand Down Expand Up @@ -214,6 +217,7 @@ func TestSetLightBri(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.True(t, light.IsOn())
t.Logf("Brightness of light %d set to %d", light.ID, light.State.Bri)

b.Host = badHostname
Expand All @@ -232,6 +236,7 @@ func TestSetLightHue(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.True(t, light.IsOn())
t.Logf("Hue of light %d set to %d", light.ID, light.State.Hue)

b.Host = badHostname
Expand All @@ -250,6 +255,7 @@ func TestSetLightSat(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.True(t, light.IsOn())
t.Logf("Sat of light %d set to %d", light.ID, light.State.Sat)

b.Host = badHostname
Expand All @@ -269,6 +275,7 @@ func TestSetLightXy(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.True(t, light.IsOn())
t.Logf("Xy of light %d set to %+v", light.ID, light.State.Xy)

b.Host = badHostname
Expand All @@ -287,6 +294,7 @@ func TestSetLightCt(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.True(t, light.IsOn())
t.Logf("Ct of light %d set to %d", light.ID, light.State.Ct)

b.Host = badHostname
Expand Down Expand Up @@ -323,6 +331,7 @@ func TestSetLightEffect(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.True(t, light.IsOn())
t.Logf("Effect of light %d set to %s", light.ID, light.State.Effect)

b.Host = badHostname
Expand All @@ -341,6 +350,7 @@ func TestSetLightAlert(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.True(t, light.IsOn())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is why tests fails. It doesn't receive expected result. Can you have a look?

t.Logf("Alert of light %d set to %s", light.ID, light.State.Alert)

b.Host = badHostname
Expand Down