@@ -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
677674func (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
830829func (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
980982func (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
10371119func (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
11361217func (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
12911374func (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+ }
0 commit comments