Skip to content

Commit

Permalink
Updates scene struct. Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amimof committed Mar 2, 2022
1 parent 1cdb78e commit 4fc420b
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 129 deletions.
106 changes: 0 additions & 106 deletions cmd/main.go

This file was deleted.

47 changes: 47 additions & 0 deletions v2/clientv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,50 @@ func (c *ClientV2) SetLightContext(ctx context.Context, id string, light *Light)
}
return res, nil
}

func (c *ClientV2) GetScenesContext(ctx context.Context) ([]*Scene, error) {
res, err :=
NewRequest(c.Clip).
Verb(http.MethodGet).
Resource(TypeScene).
OnError(errorHandler).
Do(ctx)
fmt.Println(res.Response.Request.URL.String())
//fmt.Println(string(res.BodyRaw))
if err != nil {
return nil, err
}
var s []*Scene
return s, unmarshal(res.BodyRaw, &s)
}

func (c *ClientV2) GetScenes() ([]*Scene, error) {
return c.GetScenesContext(context.Background())
}

func (c *ClientV2) GetSceneContext(ctx context.Context, id string) (*Scene, error) {
res, err :=
NewRequest(c.Clip).
Verb(http.MethodGet).
Resource(TypeScene).
OnError(errorHandler).
ID(id).
Do(ctx)
if err != nil {
return nil, err
}
var s []*Scene
err = unmarshal(res.BodyRaw, &s)
if err != nil {
return nil, err
}
if len(s) <= 0 {
return nil, fmt.Errorf("scene %s not found", id)
}
scene := s[0]
return scene, nil
}

func (c *ClientV2) GetScene(id string) (*Scene, error) {
return c.GetSceneContext(context.Background(), id)
}
1 change: 1 addition & 0 deletions v2/huego.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func unmarshal(data []byte, obj interface{}) error {
}
err = a.Into(obj)
if err != nil {
fmt.Println(err)
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion v2/light_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestGetLight(t *testing.T) {
Type: ptrString(TypeLight),
ID: ptrString("63939343-2449-48b7-aa7d-5daea11dc546"),
IDv1: ptrString("/lights/45"),
Metadata: map[string]string{
Metadata: map[string]interface{}{
"archetype": "spot_bulb",
"name": "Hue color spot 3",
},
Expand Down
2 changes: 1 addition & 1 deletion v2/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type BaseResource struct {
// +optional
IDv1 *string `json:"id_v1,omitempty"`
// +required
Metadata map[string]string `json:"metadata,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
// +required
Owner *Owner `json:"owner,omitempty"`
}
Expand Down
46 changes: 25 additions & 21 deletions v2/scene.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
package huego

// Scene is a resource of type scene https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_scene_get
type Scene struct {
Group *Owner `json:"group,omitempty"`
Actions []*ActionList `json:"actions,omitempty"`
Palette *ColorPalette `json:"palette,omitempty"`
Speed *float32 `json:"speed,omitempty"`
Actions []*ActionGet `json:"actions,omitempty"`
Group *Owner `json:"group,omitempty"`
Palette *ColorPalette `json:"palette,omitempty"`
Speed float64 `json:"speed,omitempty"`
BaseResource
}

// ActionList is a list of actions to be executed synchronously on recall
type ActionList struct {
Target *Owner `json:"target,omitempty"`
Action *Action `json:"action,omitempty"`
// Action is the action to be executed on recall
type ActionGet struct {
Action *Action`json:"action,omitempty"`
Target *Owner `json:"target,omitempty"`
}

// Action is the action to be executed on recall
type Action struct {
On *On `json:"on,omitempty"`
Dimming *Dimming `json:"dimming,omitempty"`
Color *Color `json:"color,omitempty"`
Dimming *Dimming `json:"dimming,omitempty"`
On *On `json:"on,omitempty"`
Color *Xy
ColorTemperature *ColorTemperature `json:"color_temperature,omitempty"`
Gradient *Gradient `json:"gradient,omitempty"`
}

// Palette is a group of colors that describe the palette of colors to be used when playing dynamics
type Palette struct {
Color *ColorPalette `json:"color,omitempty"`
Gradient *Gradient `json:"gradient,omitempty"`
}

// ColorPalette describes the p
type ColorPalette struct {
Color *Xy `json:"color,omitempty"`
Dimming *Dimming `json:"dimming,omitempty"`
Color []*Xy `json:"color,omitempty"`
Dimming []*Dimming `json:"dimming,omitempty"`
ColorTemperature []*ColorTemperature `json:"color_temperature,omitempty"`
}

// type Gradient struct {
// Points []*GradientPoint `json:"points,omitempty"`
// }

// type GradientPoint struct {
// Color *Xy `json:"color,omitempty"`
// }
73 changes: 73 additions & 0 deletions v2/scene_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
package huego

import (
"bytes"
"net/http"
"reflect"
"testing"
)

func TestGetScene(t *testing.T) {

tests := []struct {
Body string
StatusCode int
Error error
Expect *Scene
}{
// Return one scene
{
Body: `{"errors":[],"data":[{"actions":[{"action":{"color_temperature":{"mirek":387},"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"66618474-95d7-4413-92e7-4eae71a80f1c","rtype":"light"}},{"action":{"color_temperature":{"mirek":387},"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"b368ea65-482e-4466-b717-266baac2a616","rtype":"light"}},{"action":{"color_temperature":{"mirek":387},"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"131ff5cb-2d46-4837-9a0e-bc59cea44170","rtype":"light"}},{"action":{"color_temperature":{"mirek":387},"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"1298a8c0-0775-4107-96e7-a5cae27aeef5","rtype":"light"}},{"action":{"on":{"on":true}},"target":{"rid":"31dbd6a4-5555-4dff-9b4c-ab5b7434f903","rtype":"light"}},{"action":{"color":{"xy":{"x":0.5897,"y":0.3536}},"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"466af769-8a20-420d-ad79-f92ffb1e72e5","rtype":"light"}},{"action":{"color":{"xy":{"x":0.5897,"y":0.3536}},"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"fbe3d0b3-bf5c-4c79-a429-84571c1dc8fc","rtype":"light"}},{"action":{"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"c63f05d9-2a1f-4918-b602-82f798954599","rtype":"light"}},{"action":{"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"d61d13a9-b252-4209-96b7-a27b31c41bfb","rtype":"light"}},{"action":{"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"54bf9070-cf34-4556-a010-298b35c969bd","rtype":"light"}},{"action":{"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"2e65cf17-6afc-4a84-9de1-9527319b9fb7","rtype":"light"}},{"action":{"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"01544696-dd85-4ff7-b250-626a3599dd9a","rtype":"light"}},{"action":{"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"acce9b3e-b02c-463f-9e63-45f5907b1dfb","rtype":"light"}},{"action":{"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"4d080a60-1b02-40ad-bd8c-a542c8299c69","rtype":"light"}},{"action":{"dimming":{"brightness":77.95},"on":{"on":true}},"target":{"rid":"3dc5a20f-c80b-4abc-a933-876b70f746cd","rtype":"light"}}],"group":{"rid":"17853064-4cd0-431e-8f7c-f19640b046fb","rtype":"zone"},"id":"2f9ca1a6-7b96-4ac3-98cd-7a4c5be5bb30","id_v1":"/scenes/lXY9hGKn1-9dxlO","metadata":{"image":{"rid":"4f2ed241-5aea-4c9d-8028-55d2b111e06f","rtype":"public_image"},"name":"Savanna sunset"},"palette":{"color":[{"color":{"xy":{"x":0.6563,"y":0.3211}},"dimming":{"brightness":80.71}},{"color":{"xy":{"x":0.5862,"y":0.3575}},"dimming":{"brightness":80.71}},{"color":{"xy":{"x":0.5502,"y":0.3655}},"dimming":{"brightness":80.71}},{"color":{"xy":{"x":0.4577,"y":0.4563}},"dimming":{"brightness":80.71}},{"color":{"xy":{"x":0.4162,"y":0.4341}},"dimming":{"brightness":80.71}}],"color_temperature":[{"color_temperature":{"mirek":373},"dimming":{"brightness":80.71}}],"dimming":[{"brightness":80.71}]},"speed":0.6190476190476191,"type":"scene"}]}`,
StatusCode: 200,
Error: nil,
Expect: &Scene{
Actions: []*ActionGet{
&ActionGet{
Action: &Action{
ColorTemperature: &ColorTemperature{
Mirek: ptrUint16(387),
},
Dimming: &Dimming{
Brightness: ptrFloat32(77.95),
},
On: &On{
On: ptrBool(true),
},
},
},
},
Group: &Owner{
Rid: ptrString("66618474-95d7-4413-92e7-4eae71a80f1c"),
Rtype: ptrString("light"),
},
},
},
}

httpClient := http.DefaultClient
httpClient.Transport = testTransport
client, err := NewClientV2("127.0.0.1", "f0")
if err != nil {
t.Fatal(err)
}
client.SetClient(httpClient)

for _, test := range tests {
testTransport.DoFunc = func(*http.Request) (*http.Response, error) {
return &http.Response{
Body: nopCloser(bytes.NewBufferString(test.Body)),
StatusCode: test.StatusCode,
}, nil
}
light, err := client.GetScene("2f9ca1a6-7b96-4ac3-98cd-7a4c5be5bb30")
if err != nil {
t.Fatalf("Unexpected error: %s\n", err.Error())
}
if err != nil && err.Error() != test.Error.Error() {
t.Fatalf("want: %+v\n, got: %+v", test.Error, err)
}
if !reflect.DeepEqual(light, test.Expect) {
t.Fatalf("want: %+v\n, got: %+v", test.Expect, light)
}
}

}

0 comments on commit 4fc420b

Please sign in to comment.