diff options
-rw-r--r-- | light.go | 60 | ||||
-rw-r--r-- | light_test.go | 10 |
2 files changed, 48 insertions, 22 deletions
@@ -9,6 +9,7 @@ import ( "errors" ) +// Light struct defines attributes of a light. type Light struct { State struct { On bool `json:"on"` // On or Off state of the light ("true" or "false") @@ -32,7 +33,7 @@ type Light struct { Bridge *Bridge } -// LightState used in SetLightState to ammend light attributes. +// LightState used in SetLightState to amend light attributes. type LightState struct { On bool `json:"on"` Bri uint8 `json:"bri,omitempty"` @@ -50,23 +51,51 @@ type LightState struct { XYIncrement *[2]float32 `json:"xy_inc,omitempty"` } -// light.TurnOff will change the light state to the "Off" mode. -func (self *Light) TurnOff() { - SetLightState(self, LightState{On: false}) +func (self *Light) SetName(name string) error { + uri := fmt.Sprintf("/api/%s/lights/%s", self.Bridge.Username, self.Index) + body := make(map[string]string) + body["name"] = name + _, _, err := self.Bridge.Put(uri, body) + return err } -// light.TurnOn will change the light state to the "On" mode. -func (self *Light) TurnOn() { - SetLightState(self, LightState{On: true}) +// Light.Off will turn the light source off +func (self *Light) Off() error { + return SetLightState(self, LightState{On: false}) } -// light.Toggle will change the light state to "On" if -// the light is off or "Off" if the light is on. -func (self *Light) Toggle() { +// Light.Off will turn the light source on +func (self *Light) On() error { + return SetLightState(self, LightState{On: true}) +} + +// Light.Toggle will toggle the light source on and off +func (self *Light) Toggle() error { if self.State.On { - self.TurnOff() + return self.Off() } else { - self.TurnOn() + return self.On() + } +} + +// Light.ColorLoopOn will turn the light on and set the effect to "colorloop" +func (self *Light) ColorLoopOn() error { + return SetLightState(self, LightState{On: true, Effect: "colorloop"}) +} + +// Light.ColorLoopOn will turn the light on and set the effect to "none" +func (self *Light) ColorLoopOff() error { + return SetLightState(self, LightState{On: true, Effect: "none"}) +} + +// Light.ColorLoop will set the light state to a colorloop if there is no +// current effect in place or if the state is in colorloop then it will +// set it to "none". +func (self *Light) ColorLoop() error { + if self.State.Effect == "colorloop" { + return self.ColorLoopOff() + } else { + return self.ColorLoopOn() } } @@ -74,11 +103,8 @@ func (self *Light) Toggle() { // brightness, and more. See `SetLightState` struct. 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 - } - return nil + _, _, err := light.Bridge.Put(uri, newState) + return err } // GetAllLights retreives the state of all lights that the bridge is aware of. diff --git a/light_test.go b/light_test.go index 6c85eff..20ab4b8 100644 --- a/light_test.go +++ b/light_test.go @@ -22,13 +22,13 @@ func TestSetLightState(t *testing.T) { lights, _ := GetAllLights(bridge) selectedLight := lights[0] - selectedLight.TurnOn() + selectedLight.On() time.Sleep(time.Second) - selectedLight.TurnOff() - time.Sleep(time.Second) - selectedLight.TurnOn() + selectedLight.Off() time.Sleep(time.Second) selectedLight.Toggle() time.Sleep(time.Second) - selectedLight.Toggle() + selectedLight.ColorLoop() + + selectedLight.SetName("testing") } |