aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-01-31 12:02:14 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-01-31 12:02:14 -0500
commit1e2656275225d15c9ed5f722a8958ea08badf109 (patch)
tree2b923ef4f397061c4c642ce20204c2de2ac27e04
parentcd035e828c499dd354e6c4cc1de676af0b47610a (diff)
Rough implementation of SetLightState with unit tests.
-rw-r--r--light.go28
-rw-r--r--light_test.go9
2 files changed, 34 insertions, 3 deletions
diff --git a/light.go b/light.go
index da137f2..c667bc6 100644
--- a/light.go
+++ b/light.go
@@ -9,6 +9,7 @@ import (
"encoding/json"
"strings"
"errors"
+ "bytes"
)
type Light struct {
@@ -17,8 +18,8 @@ type Light struct {
Bri int `json:"bri"` // Brightness value 1-254
Hue int `json:"hue"` // Hue value 1-65535
Saturation int `json:"sat"` // Saturation value 0-254
- Effect string `json:"effect"`
- XY [2]float32 `json:"xy"` // Coordinates of color in CIE color space
+ Effect string `json:"effect"` // "None" or "Colorloop"
+ XY [2]float32 `json:"xy"` // Coordinates of color in CIE color space
CT int `json:"ct"` // Mired Color Temperature (google it)
Alert string `json:"alert"`
ColorMode string `json:"colormode"`
@@ -52,8 +53,29 @@ type LightState struct {
// SetLightState will modify light attributes such as on/off, saturation,
// brightness, and more. See `SetLightState` struct.
-func SetLightState(bridge *Bridge, newState LightState) {
+func SetLightState(bridge *Bridge, lightID string, newState LightState) {
+ // Construct the http POST
+ req, err := json.Marshal(newState)
+ if err != nil {
+ trace("", err)
+ }
+
+ // Send the request and read the response
+ uri := fmt.Sprintf("http://%s/api/%s/lights/%s/state",
+ bridge.IPAddress, bridge.Username, lightID)
+ resp, err := http.Post(uri, "text/json", bytes.NewReader(req))
+ if err != nil {
+ trace("", err)
+ }
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ trace("", err)
+ }
+
+ // TODO: parse the response
+ fmt.Println(string(body))
}
//http://192.168.1.128/api/319b36233bd2328f3e40731b23479207/lights/
diff --git a/light_test.go b/light_test.go
index 5639630..2481c0f 100644
--- a/light_test.go
+++ b/light_test.go
@@ -2,6 +2,7 @@ package hue
import (
"testing"
+ "fmt"
)
func TestGetAllLights(t *testing.T) {
@@ -13,3 +14,11 @@ func TestGetLight(t *testing.T) {
bridge := NewBridge("192.168.1.128", "319b36233bd2328f3e40731b23479207")
GetLight(bridge, "Bathroom Light")
}
+
+func TestSetLightState(t *testing.T) {
+ bridge := NewBridge("192.168.1.128", "319b36233bd2328f3e40731b23479207")
+ randomLight := GetAllLights(bridge)[0]
+ fmt.Println(randomLight.Name)
+ newState := LightState{On: true}
+ SetLightState(bridge, randomLight.UniqueID, newState)
+}