Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
}

// Col sets the light color as RGB (will be converted to xy)
func (g *Group) Col(new color.Color) error {
return g.ColContext(context.Background(), new)
}

// ColContext sets the light color as RGB (will be converted to xy)
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
20 changes: 20 additions & 0 deletions group_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package huego

import (
"image/color"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -349,6 +350,25 @@ func TestSetGroupXy(t *testing.T) {
assert.NotNil(t, err)
}

func TestSetGroupCol(t *testing.T) {
b := New(hostname, username)
id := 1
group, err := b.GetGroup(id)
if err != nil {
t.Fatal(err)
}
color := color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0xFF}
err = group.Col(color)
if err != nil {
t.Fatal(err)
}
t.Logf("Col of group %d set to xy: %v", group.ID, group.State.Xy)

b.Host = badHostname
err = group.Col(color)
assert.NotNil(t, err)
}

func TestSetGroupCt(t *testing.T) {
b := New(hostname, username)
id := 1
Expand Down
62 changes: 61 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
}

// Col sets the light color as RGB (will be converted to xy)
func (l *Light) Col(new color.Color) error {
return l.ColContext(context.Background(), new)
}

// ColContext sets the light color as RGB (will be converted to xy)
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,33 @@ func (l *Light) AlertContext(ctx context.Context, new string) error {
l.State.Effect = new
return nil
}

// ConvertRGBToXy converts a given RGB color to the xy color of the ligth.
// implemented as in https://developers.meethue.com/develop/application-design-guidance/color-conversion-formulas-rgb-to-xy-and-back/
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)
}
44 changes: 43 additions & 1 deletion light_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package huego

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

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

func TestGetLights(t *testing.T) {
Expand Down Expand Up @@ -149,6 +151,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 +170,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 +218,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 +237,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 +256,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 +276,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,13 +295,34 @@ 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
err = light.Ct(16)
assert.NotNil(t, err)
}

func TestSetLightColor(t *testing.T) {
b := New(hostname, username)
id := 1
light, err := b.GetLight(id)
if err != nil {
t.Fatal(err)
}
color := color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0xFF}
err = light.Col(color)
if err != nil {
t.Fatal(err)
}
assert.True(t, light.IsOn())
t.Logf("Col of light %d set to xy: %+v", light.ID, light.State.Xy)

b.Host = badHostname
err = light.Col(color)
assert.NotNil(t, err)
}

func TestSetLightTransitionTime(t *testing.T) {
b := New(hostname, username)
id := 1
Expand Down Expand Up @@ -323,6 +352,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 +371,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 Expand Up @@ -369,3 +400,14 @@ func TestSetStateLight(t *testing.T) {
err = light.SetState(state)
assert.NotNil(t, err)
}

func TestConvertRGBToXY(t *testing.T) {
color := color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0xFF}
xy, brightness := ConvertRGBToXy(color)

assert.Greater(t, xy[0], float32(0))
assert.Greater(t, xy[1], float32(0))
assert.Greater(t, brightness, uint8(0))

t.Logf("Xy of light %+v set to xy: %+v, bright: %d ", color, xy, brightness)
}