From 15a6d3afe17915ca077696abc58145db27d9601c Mon Sep 17 00:00:00 2001 From: Christian Brunner Date: Tue, 13 Sep 2016 22:02:53 +0200 Subject: add GetSceneByName() --- scene.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/scene.go b/scene.go index 687ca1c..3358a1b 100644 --- a/scene.go +++ b/scene.go @@ -10,6 +10,7 @@ package hue import ( "encoding/json" + "errors" "fmt" ) @@ -71,6 +72,22 @@ func (bridge *Bridge) GetScene(id string) (Scene, error) { return scene, nil } +// Bridge.GetSceneByName gets the attributes for the scene identified by a name +func (bridge *Bridge) GetSceneByName(name string) (Scene, error) { + + scenes, _ := bridge.GetAllScenes() + + // Iterate in reverse, as later entries seem to be the newest + for i := len(scenes) - 1; i >= 0; i-- { + if scenes[i].Name == name { + return scenes[i], nil + } + } + + errOut := fmt.Sprintf("Error: Scene name '%s' not found. ", name) + return Scene{}, errors.New(errOut) +} + // Bridge.CreateScene posts a new scene configuration to the bridge. func (bridge *Bridge) CreateScene(scene Scene) error { uri := fmt.Sprintf("/api/%s/scenes/", bridge.Username) -- cgit v1.2.3 From 8ce825db2570d06a868fd89cb7518cad6169284e Mon Sep 17 00:00:00 2001 From: Christian Brunner Date: Tue, 13 Sep 2016 22:22:27 +0200 Subject: add SetGroupState() --- group.go | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/group.go b/group.go index 4a36346..c842a5a 100644 --- a/group.go +++ b/group.go @@ -13,19 +13,23 @@ import ( "fmt" ) +// Action struct defines the state of a group +type Action struct { + Alert string `json:"alert,omitempty"` + Bri int `json:"bri,omitempty"` + Colormode string `json:"colormode,omitempty"` + Ct int `json:"ct,omitempty"` + Effect string `json:"effect,omitempty"` + Hue int `json:"hue,omitempty"` + On bool `json:"on,omitempty"` + Sat int `json:"sat,omitempty"` + XY []float64 `json:"xy,omitempty"` + Scene string `json:"scene,omitempty"` +} + // Group struct defines the attributes for a group of lights. type Group struct { - Action struct { - Alert string `json:"alert"` - Bri int `json:"bri"` - Colormode string `json:"colormode"` - Ct int `json:"ct"` - Effect string `json:"effect"` - Hue int `json:"hue"` - On bool `json:"on"` - Sat int `json:"sat"` - XY []float64 `json:"xy"` - } `json:"action"` + Action Action `json:"action"` Lights []string `json:"lights"` Name string `json:"name"` Type string `json:"type"` @@ -51,3 +55,13 @@ func (bridge *Bridge) GetGroups() ([]Group, error) { return []Group{}, nil } + +// Bridge.SetGroupState sends an action to group +func (bridge *Bridge) SetGroupState(group int, action *Action) error { + uri := fmt.Sprintf("/api/%s/groups/%d/action", bridge.Username, group) + _, _, err := bridge.Put(uri, action) + if err != nil { + return err + } + return nil +} -- cgit v1.2.3 From de4b7f2603270cdf868e22245924cb51afd57934 Mon Sep 17 00:00:00 2001 From: Christian Brunner Date: Tue, 13 Sep 2016 22:27:43 +0200 Subject: add RecallScene --- scene.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scene.go b/scene.go index 3358a1b..9339893 100644 --- a/scene.go +++ b/scene.go @@ -88,6 +88,12 @@ func (bridge *Bridge) GetSceneByName(name string) (Scene, error) { return Scene{}, errors.New(errOut) } +// Bridge.RecallScene recalls a scene +func (bridge *Bridge) RecallScene(id string) error { + action := &Action{Scene: id} + return bridge.SetGroupState(0, action) +} + // Bridge.CreateScene posts a new scene configuration to the bridge. func (bridge *Bridge) CreateScene(scene Scene) error { uri := fmt.Sprintf("/api/%s/scenes/", bridge.Username) -- cgit v1.2.3 From 717f77eeb3be68ba8328e04f90904070df687d3d Mon Sep 17 00:00:00 2001 From: Christian Brunner Date: Tue, 13 Sep 2016 22:31:07 +0200 Subject: add RecallSceneByName() --- scene.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/scene.go b/scene.go index 9339893..e972de7 100644 --- a/scene.go +++ b/scene.go @@ -94,6 +94,15 @@ func (bridge *Bridge) RecallScene(id string) error { return bridge.SetGroupState(0, action) } +// Bridge.RecallSceneByName recalls a scene +func (bridge *Bridge) RecallSceneByName(name string) error { + scene, err := bridge.GetSceneByName(name) + if err != nil { + return err + } + return bridge.RecallScene(scene.ID) +} + // Bridge.CreateScene posts a new scene configuration to the bridge. func (bridge *Bridge) CreateScene(scene Scene) error { uri := fmt.Sprintf("/api/%s/scenes/", bridge.Username) -- cgit v1.2.3