From fb3d44f6146e8b328b40edc04ff8f42b3baf6af8 Mon Sep 17 00:00:00 2001
From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr>
Date: Sat, 16 Nov 2024 21:28:54 +0100
Subject: [PATCH] Fix brightness / white_temperature

brightness and white_temperature are sometimes binded to Float64 (don't
ask why). So I used convertToInt()
---
 xaal.go | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/xaal.go b/xaal.go
index 6aebc58..d5e770c 100644
--- a/xaal.go
+++ b/xaal.go
@@ -297,10 +297,10 @@ func (dev *Lamp) update(payload map[string]interface{}) {
 	}
 
 	// brightness
-	brightness, exists := payload["brightness"].(int)
-	if exists {
-		brightness = brightness / 255 * 100
-		dev.GetAttribute("brightness").SetValue(brightness)
+	brightness, err := convertToInt(payload["brightness"])
+	if err == nil {
+		brightness = brightness * 100 / 255
+		dev.GetAttribute("brightness").SetValue(int(brightness))
 	}
 
 	// color_temp
@@ -308,10 +308,9 @@ func (dev *Lamp) update(payload map[string]interface{}) {
 		// color_temp change when we are in color mode (looks like a z2m bug)
 		// so we have to only update when we are in white mode. Without this check
 		// the color_temp is wrong when you change the mode w/ setMode
-		color_temp, exists := payload["color_temp"].(int)
-		if exists {
-			color_temp = convertMired(color_temp)
-			dev.GetAttribute("white_temperature").SetValue(color_temp)
+		color_temp, err := convertToInt(payload["color_temp"])
+		if err == nil {
+			dev.GetAttribute("white_temperature").SetValue(convertMired(color_temp))
 		}
 	}
 
-- 
GitLab