aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--light.go14
-rw-r--r--light_test.go2
2 files changed, 10 insertions, 6 deletions
diff --git a/light.go b/light.go
index 2d72502..2672171 100644
--- a/light.go
+++ b/light.go
@@ -29,6 +29,7 @@ type Light struct {
UniqueID string `json:"uniqueid"`
SWVersion string `json:"swversion"`
Index int // Set by index of light array response // TODO: change to smaller int
+ Bridge *Bridge
}
// LightState used in SetLightState to ammend light attributes.
@@ -50,18 +51,20 @@ type LightState struct {
}
func (self *Light) TurnOff() {
- SetLightState(self.Index, LightState{On: false})
+ SetLightState(self, LightState{On: false})
}
func (self *Light) TurnOn() {
- SetLightState(self.Index, LightState{On: true})
+ SetLightState(self, LightState{On: true})
}
+
+
// SetLightState will modify light attributes such as on/off, saturation,
// brightness, and more. See `SetLightState` struct.
-func SetLightState(bridge *Bridge, index int, newState LightState) error {
- uri := fmt.Sprintf("/api/%s/lights/%d/state", bridge.Username, index)
- _, _, err := bridge.Put(uri, newState) // TODO: change to PUT
+func SetLightState(light *Light, newState LightState) error {
+ uri := fmt.Sprintf("/api/%s/lights/%d/state", light.Bridge.Username, light.Index)
+ _, _, err := light.Bridge.Put(uri, newState) // TODO: change to PUT
if err != nil {
return err
}
@@ -94,6 +97,7 @@ func GetAllLights(bridge *Bridge) ([]Light, error) {
trace("", err)
}
data.Index = index
+ data.Bridge = bridge
lights = append(lights, data)
}
return lights, nil
diff --git a/light_test.go b/light_test.go
index 0a63bf8..c9a16a5 100644
--- a/light_test.go
+++ b/light_test.go
@@ -24,7 +24,7 @@ func TestSetLightState(t *testing.T) {
selectedLight.TurnOn()
time.Sleep(time.Second)
- selectedLight.TurnOf()
+ selectedLight.TurnOff()
time.Sleep(time.Second)
selectedLight.TurnOn()
}