Repository navigation
Updating Devices
Ohlin Arellano edited this page May 25, 2025
·
1 revision
You can control your LIFX devices using the lifxlan client in two primary ways:
- By Device Object – directly call methods on each device after loading them
- By Label – control devices using their label (case-insensitive and whitespace-trimmed)
After loading devices from JSON, you can loop through the devices and set colors individually.
package main
import (
"fmt"
"os"
"time"
"github.com/lucasb-eyer/go-colorful"
"github.com/murphy28/lifxlan"
)
func main() {
lifx, err := lifxlan.NewClient()
if err != nil {
fmt.Println("Error creating LIFX client:", err)
return
}
defer lifx.Close()
content, err := os.ReadFile("devices.json")
if err != nil {
fmt.Println("Error reading devices.json:", err)
return
}
err = lifx.LoadDevices(string(content))
if err != nil {
fmt.Println("Error loading devices:", err)
return
}
// Create colors
red := lifxlan.NewColorFromRGB(255, 0, 0)
green := lifxlan.NewColorFromHSV(120, 100, 100)
blue, _ := lifxlan.NewColorFromHex("#0000FF")
purple := lifxlan.ColorfulToLifxColor(colorful.Color{R: 0.313725, G: 0.478431, B: 0.721569})
// Set colors based on index
for i, device := range lifx.GetDevices() {
switch i {
case 0:
device.SetColor(red, 1*time.Second)
case 1:
device.SetColor(green, 1*time.Second)
case 2:
device.SetColor(blue, 1*time.Second)
case 3:
device.SetColor(purple, 1*time.Second)
}
}
}You can also control lights using their labels. Labels are case-insensitive and trimmed of spaces automatically.
package main
import (
"fmt"
"os"
"time"
"github.com/lucasb-eyer/go-colorful"
"github.com/murphy28/lifxlan"
)
func main() {
lifx, err := lifxlan.NewClient()
if err != nil {
fmt.Println("Error creating LIFX client:", err)
return
}
defer lifx.Close()
content, err := os.ReadFile("devices.json")
if err != nil {
fmt.Println("Error reading devices.json:", err)
return
}
err = lifx.LoadDevices(string(content))
if err != nil {
fmt.Println("Error loading devices:", err)
return
}
// Create colors
red := lifxlan.NewColorFromRGB(255, 0, 0)
green := lifxlan.NewColorFromHSV(120, 100, 100)
blue, _ := lifxlan.NewColorFromHex("#0000FF")
purple := lifxlan.ColorfulToLifxColor(colorful.Color{R: 0.313725, G: 0.478431, B: 0.721569})
// Set color by label
lifx.SetColor("Living Room", red, 1*time.Second)
lifx.SetColor("Bedroom", green, 1*time.Second)
lifx.SetColor("Desk Lamp", blue, 1*time.Second)
lifx.SetColor("Corner Light", purple, 1*time.Second)
}You can turn devices on or off either via their object or by label:
// Using device object
device.TurnOn()
device.TurnOff()
// Using device label
lifx.TurnOn("Living Room")
lifx.TurnOff("Desk Lamp")You can also change the label (name) of a device:
// Using device object
device.SetLabel("New Label")
// Using label reference
lifx.SetLabel("Old Label", "New Label")All client methods that take a label (e.g., SetColor, TurnOn, SetLabel) are case-insensitive and automatically trim whitespace from both ends.