Skip to content

Commit ae58e27

Browse files
committed
Merge branch 'release/1.0.0-alpha.5'
2 parents 9398038 + 8a6fbc6 commit ae58e27

19 files changed

+470
-159
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 1.0.0-alpha.5
4+
* Added `SetState` receivers to `Group` and `Light`.
5+
* Renamed `SetLight` to `SetLightState` for a more consistent naming convention.
6+
* Implemented `Capabilities`
7+
8+
## 1.0.0-alpha.4
9+
* Huego now has a logo
10+
* Changes to fulfill `golint`, `govet` and `gofmt`
11+
312
## 1.0.0-alpha.3
413
* Added `Group` receivers: `Alert()`, `Bri()`, `Ct()`, `Effect()`, `Hue()`, `IsOn()`, `Off()`, `On()`, `Rename()`, `Sat()`, `Scene()`, `TransitionTime()` and `Xy()`
514
* Added `Light` receivers: `Alert()`, `Effect()` and `TransitionTime()`

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ This project is currently in **ALPHA** and still under heavy development. Curren
6464
| Scenes | `Complete` | `Complete` |
6565
| Rules | `Complete` | `Complete` |
6666
| Resourcelinks | `Complete` | `Complete` |
67-
| Configuration | `Complete` | `Complete`
68-
| Capabilities | `Not Started` | `Not Started`
67+
| Configuration | `Complete` | `Complete` |
68+
| Capabilities | `Complete` | `Complete` |
6969

7070
Other than above core modules, each module needs additional *helper* methods for conveniance and flavour. The goal is to keep it simple, and not to bloat the library with functionality that developers might want to write on their own.
7171

@@ -76,7 +76,7 @@ The goal of this project is to provide an easy to use, stable and extensive libr
7676
## To-Do
7777

7878
* Add helper methods on each module
79-
* Add `SetSceneLightState`
80-
* Add `RecallScene`
81-
* Finish `Capabilities`
79+
* ~~Add `SetSceneLightState`~~
80+
* ~~Add `RecallScene`~~
81+
* ~~Finish `Capabilities`~~
8282
* More tests

bridge.go

Lines changed: 135 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ func (b *Bridge) CreateUser(n string) (string, error) {
8181
body := struct {
8282
DeviceType string `json:"devicetype,omitempty"`
8383
GenerateClientKey bool `json:"generateclientkey,omitempty"`
84-
}{
85-
n,
86-
true,
87-
}
84+
}{n, true}
8885

8986
url, err := b.getAPIPath("/")
9087
if err != nil {
@@ -469,8 +466,8 @@ func (b *Bridge) GetLight(i int) (*Light, error) {
469466
return light, nil
470467
}
471468

472-
// SetLight allows for controlling one light's state
473-
func (b *Bridge) SetLight(i int, l State) (*Response, error) {
469+
// SetLightState allows for controlling one light's state
470+
func (b *Bridge) SetLightState(i int, l State) (*Response, error) {
474471

475472
var a []*APIResponse
476473

@@ -676,7 +673,9 @@ func (b *Bridge) GetResourcelinks() ([]*Resourcelink, error) {
676673
// GetResourcelink returns one resourcelink by its id defined by i
677674
func (b *Bridge) GetResourcelink(i int) (*Resourcelink, error) {
678675

679-
var resourcelink *Resourcelink
676+
g := &Resourcelink{
677+
ID: i,
678+
}
680679

681680
url, err := b.getAPIPath("/resourcelinks/", strconv.Itoa(i))
682681

@@ -685,12 +684,12 @@ func (b *Bridge) GetResourcelink(i int) (*Resourcelink, error) {
685684
return nil, err
686685
}
687686

688-
err = json.Unmarshal(res, &resourcelink)
687+
err = json.Unmarshal(res, &g)
689688
if err != nil {
690689
return nil, err
691690
}
692691

693-
return resourcelink, nil
692+
return g, nil
694693

695694
}
696695

@@ -829,7 +828,9 @@ func (b *Bridge) GetRules() ([]*Rule, error) {
829828
// GetRule returns one rule by its id of i
830829
func (b *Bridge) GetRule(i int) (*Rule, error) {
831830

832-
var rule *Rule
831+
g := &Rule{
832+
ID: i,
833+
}
833834

834835
url, err := b.getAPIPath("/rules/", strconv.Itoa(i))
835836
if err != nil {
@@ -841,12 +842,12 @@ func (b *Bridge) GetRule(i int) (*Rule, error) {
841842
return nil, err
842843
}
843844

844-
err = json.Unmarshal(res, &rule)
845+
err = json.Unmarshal(res, &g)
845846
if err != nil {
846847
return nil, err
847848
}
848849

849-
return rule, nil
850+
return g, nil
850851

851852
}
852853

@@ -969,6 +970,7 @@ func (b *Bridge) GetScenes() ([]Scene, error) {
969970

970971
for i, g := range m {
971972
g.ID = i
973+
g.bridge = b
972974
scenes = append(scenes, g)
973975
}
974976

@@ -979,7 +981,10 @@ func (b *Bridge) GetScenes() ([]Scene, error) {
979981
// GetScene returns one scene by its id of i
980982
func (b *Bridge) GetScene(i string) (*Scene, error) {
981983

982-
var g *Scene
984+
g := &Scene{ID: i}
985+
l := struct {
986+
LightStates map[int]State `json:"lightstates"`
987+
}{}
983988

984989
url, err := b.getAPIPath("/scenes/", i)
985990
if err != nil {
@@ -991,20 +996,26 @@ func (b *Bridge) GetScene(i string) (*Scene, error) {
991996
return nil, err
992997
}
993998

999+
err = json.Unmarshal(res, &l)
1000+
if err != nil {
1001+
return nil, err
1002+
}
1003+
9941004
err = json.Unmarshal(res, &g)
9951005
if err != nil {
9961006
return nil, err
9971007
}
9981008

1009+
g.bridge = b
1010+
9991011
return g, nil
10001012
}
10011013

10021014
// UpdateScene updates one scene and its attributes by id of i
1003-
func (b *Bridge) UpdateScene(i int, s *Scene) (*Response, error) {
1015+
func (b *Bridge) UpdateScene(id string, s *Scene) (*Response, error) {
10041016

10051017
var a []*APIResponse
10061018

1007-
id := strconv.Itoa(i)
10081019
url, err := b.getAPIPath("/scenes/", id)
10091020
if err != nil {
10101021
return nil, err
@@ -1033,6 +1044,77 @@ func (b *Bridge) UpdateScene(i int, s *Scene) (*Response, error) {
10331044
return resp, nil
10341045
}
10351046

1047+
// SetSceneLightState allows for setting the state of a light in a scene.
1048+
// SetSceneLightState accepts the id of the scene, the id of a light associated with the scene and the state object.
1049+
func (b *Bridge) SetSceneLightState(id string, iid int, l *State) (*Response, error) {
1050+
1051+
var a []*APIResponse
1052+
1053+
lightid := strconv.Itoa(iid)
1054+
url, err := b.getAPIPath("scenes", id, "lightstates", lightid)
1055+
if err != nil {
1056+
return nil, err
1057+
}
1058+
1059+
data, err := json.Marshal(&l)
1060+
if err != nil {
1061+
return nil, err
1062+
}
1063+
1064+
res, err := put(url, data)
1065+
if err != nil {
1066+
return nil, err
1067+
}
1068+
1069+
err = json.Unmarshal(res, &a)
1070+
if err != nil {
1071+
return nil, err
1072+
}
1073+
1074+
resp, err := handleResponse(a)
1075+
if err != nil {
1076+
return nil, err
1077+
}
1078+
1079+
return resp, nil
1080+
}
1081+
1082+
// RecallScene will recall a scene in a group identified by both scene and group identifiers
1083+
func (b *Bridge) RecallScene(id string, gid int) (*Response, error) {
1084+
1085+
var a []*APIResponse
1086+
1087+
data, err := json.Marshal(struct {
1088+
Scene string `json:"scene"`
1089+
}{id})
1090+
1091+
if err != nil {
1092+
return nil, err
1093+
}
1094+
1095+
url, err := b.getAPIPath("/groups/", strconv.Itoa(gid), "/action")
1096+
if err != nil {
1097+
return nil, err
1098+
}
1099+
1100+
res, err := put(url, data)
1101+
if err != nil {
1102+
return nil, err
1103+
}
1104+
1105+
err = json.Unmarshal(res, &a)
1106+
if err != nil {
1107+
return nil, err
1108+
}
1109+
1110+
resp, err := handleResponse(a)
1111+
if err != nil {
1112+
return nil, err
1113+
}
1114+
1115+
return resp, err
1116+
}
1117+
10361118
// CreateScene creates one new scene with its attributes defined in s
10371119
func (b *Bridge) CreateScene(s *Scene) (*Response, error) {
10381120

@@ -1067,11 +1149,10 @@ func (b *Bridge) CreateScene(s *Scene) (*Response, error) {
10671149
}
10681150

10691151
// DeleteScene deletes one scene from the bridge
1070-
func (b *Bridge) DeleteScene(i int) error {
1152+
func (b *Bridge) DeleteScene(id string) error {
10711153

10721154
var a []*APIResponse
10731155

1074-
id := strconv.Itoa(i)
10751156
url, err := b.getAPIPath("/scenes/", id)
10761157
if err != nil {
10771158
return err
@@ -1135,7 +1216,9 @@ func (b *Bridge) GetSchedules() ([]*Schedule, error) {
11351216
// GetSchedule returns one schedule by id defined in i
11361217
func (b *Bridge) GetSchedule(i int) (*Schedule, error) {
11371218

1138-
var schedule *Schedule
1219+
g := &Schedule{
1220+
ID: i,
1221+
}
11391222

11401223
url, err := b.getAPIPath("/schedules/", strconv.Itoa(i))
11411224
if err != nil {
@@ -1147,12 +1230,12 @@ func (b *Bridge) GetSchedule(i int) (*Schedule, error) {
11471230
return nil, err
11481231
}
11491232

1150-
err = json.Unmarshal(res, &schedule)
1233+
err = json.Unmarshal(res, &g)
11511234
if err != nil {
11521235
return nil, err
11531236
}
11541237

1155-
return schedule, nil
1238+
return g, nil
11561239

11571240
}
11581241

@@ -1290,7 +1373,9 @@ func (b *Bridge) GetSensors() ([]Sensor, error) {
12901373
// GetSensor returns one sensor by its id of i
12911374
func (b *Bridge) GetSensor(i int) (*Sensor, error) {
12921375

1293-
var r *Sensor
1376+
r := &Sensor{
1377+
ID: i,
1378+
}
12941379

12951380
id := strconv.Itoa(i)
12961381
url, err := b.getAPIPath("/sensors/", id)
@@ -1506,3 +1591,32 @@ func (b *Bridge) UpdateSensorConfig(i int, config *SensorConfig) (*Response, err
15061591

15071592
return resp, nil
15081593
}
1594+
1595+
/*
1596+
1597+
CAPABILITIES API
1598+
1599+
*/
1600+
1601+
// GetCapabilities returns a list of capabilities of resources supported in the bridge.
1602+
func (b *Bridge) GetCapabilities() (*Capabilities, error) {
1603+
1604+
s := &Capabilities{}
1605+
1606+
url, err := b.getAPIPath("/capabilities/")
1607+
if err != nil {
1608+
return nil, err
1609+
}
1610+
1611+
res, err := get(url)
1612+
if err != nil {
1613+
return nil, err
1614+
}
1615+
1616+
err = json.Unmarshal(res, &s)
1617+
if err != nil {
1618+
return nil, err
1619+
}
1620+
1621+
return s, err
1622+
}

capabilities.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package huego
2+
3+
// Capabilities holds a combined model of resource capabilities on the bridge: https://developers.meethue.com/documentation/lights-api
4+
type Capabilities struct {
5+
Groups *Capability `json:"groups,omitempty"`
6+
Lights *Capability `json:"lights,omitempty"`
7+
Resourcelinks *Capability `json:"resourcelinks,omitempty"`
8+
Schedules *Capability `json:"schedules,omitempty"`
9+
Rules *Capability `json:"rules,omitempty"`
10+
Scenes *Capability `json:"scenes,omitempty"`
11+
Sensors *Capability `json:"sensors,omitempty"`
12+
Streaming *Capability `json:"streaming,omitempty"`
13+
}
14+
15+
// Capability defines the resource and subresource capabilities.
16+
type Capability struct {
17+
Available int `json:"available,omitempty"`
18+
}

capabilities_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package huego_test
2+
3+
import (
4+
"github.com/amimof/huego"
5+
"os"
6+
"testing"
7+
)
8+
9+
func TestGetCapabilities(t *testing.T) {
10+
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
11+
c, err := b.GetCapabilities()
12+
if err != nil {
13+
t.Fatal(c)
14+
}
15+
t.Log("Capabilities:")
16+
t.Log(" Groups")
17+
t.Logf(" Available: %d", c.Groups.Available)
18+
t.Log(" Lights")
19+
t.Logf(" Available: %d", c.Lights.Available)
20+
t.Log(" Resourcelinks")
21+
t.Logf(" Available: %d", c.Resourcelinks.Available)
22+
t.Log(" Schedules")
23+
t.Logf(" Available: %d", c.Schedules.Available)
24+
t.Log(" Rules")
25+
t.Logf(" Available: %d", c.Rules.Available)
26+
t.Log(" Scenes")
27+
t.Logf(" Available: %d", c.Scenes.Available)
28+
t.Log(" Sensors")
29+
t.Logf(" Available: %d", c.Sensors.Available)
30+
t.Log(" Streaming")
31+
t.Logf(" Available: %d", c.Streaming.Available)
32+
}

capability.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

capability_test.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)