From 6b57429cb95f0b49abc874e1ff3a80c6a093b320 Mon Sep 17 00:00:00 2001
From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr>
Date: Mon, 11 Nov 2024 19:50:22 +0100
Subject: [PATCH] Support for mode

The color/white mode was missing. All should be fine now
---
 xaal.go | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/xaal.go b/xaal.go
index 3dd55b1..83dd7f0 100644
--- a/xaal.go
+++ b/xaal.go
@@ -51,11 +51,6 @@ type Lamp struct {
 	Z2MDevice Z2MDevice
 }
 
-type LampDimmer struct {
-	XAALDevice
-	Z2MDevice Z2MDevice
-}
-
 type ButtonRemote struct {
 	XAALDevice
 	Z2MDevice Z2MDevice
@@ -282,6 +277,20 @@ func (dev *Lamp) update(payload map[string]interface{}) {
 		brightness = brightness / 255 * 100
 		dev.GetAttribute("brightness").SetValue(int(brightness))
 	}
+	color_mode, exists := payload["color_mode"].(string)
+	if exists {
+		mode := dev.GetAttribute("mode")
+		// only color lamp have a mode
+		if mode != nil {
+			switch color_mode {
+			case "xy":
+				mode.SetValue("color")
+			case "color_temp":
+				mode.SetValue("white")
+			}
+		}
+	}
+
 	// color
 	color, exists := payload["color"].(map[string]interface{})
 	if exists {
@@ -292,13 +301,10 @@ func (dev *Lamp) update(payload map[string]interface{}) {
 		slog.Warn("color", "color", value.Hex(), "x", x, "y", y, "tmp", brigthness)
 		hue, sat, val := value.Hsv()
 		hue = roundToDecimal(hue, 1)
-		sat = roundToDecimal(sat, 2)
-		val = roundToDecimal(val, 2)
-		slog.Warn("color", "hue", hue, "sat", sat, "val", val)
+		sat = roundToDecimal(sat, 3)
+		val = roundToDecimal(val, 3)
 		dev.GetAttribute("hsv").SetValue([]float64{hue, sat, val})
-		return
 	}
-
 	// color_temp
 	color_temp, exists := payload["color_temp"].(float64)
 	if exists {
@@ -336,7 +342,7 @@ func (dev *Lamp) setWhiteTemperature(body xaal.MessageBody) *xaal.MessageBody {
 	value, exists := body["white_temperature"].(uint64)
 	if exists {
 		value = 1000000 / value
-		dev.Z2MDevice.Set(fmt.Sprintf(`{"color_temp": %d}`, value))
+		dev.Z2MDevice.Set(fmt.Sprintf(`{"color_temp": %d}`, int(value)))
 	}
 	return nil
 }
-- 
GitLab