From 7df767996b0b8d7daab32cf43585a97437261df8 Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Sun, 7 Feb 2016 17:42:26 -0500 Subject: Added light.Toggle func and documentation for On, Off, Toggle. --- light.go | 12 +++++++++++- light_test.go | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/light.go b/light.go index 2672171..d1908e2 100644 --- a/light.go +++ b/light.go @@ -50,15 +50,25 @@ 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}) } +// light.TurnOn will change the light state to the "On" mode. func (self *Light) TurnOn() { SetLightState(self, LightState{On: true}) } - +// 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() { + if self.State.On { + self.TurnOff() + } else { + self.TurnOn() + } +} // SetLightState will modify light attributes such as on/off, saturation, // brightness, and more. See `SetLightState` struct. diff --git a/light_test.go b/light_test.go index c9a16a5..6c85eff 100644 --- a/light_test.go +++ b/light_test.go @@ -27,4 +27,8 @@ func TestSetLightState(t *testing.T) { selectedLight.TurnOff() time.Sleep(time.Second) selectedLight.TurnOn() + time.Sleep(time.Second) + selectedLight.Toggle() + time.Sleep(time.Second) + selectedLight.Toggle() } -- cgit v1.2.3