diff --git a/utils.go b/utils.go index 780b786d6b079ea4070f964320e0841f7566d8b3..011a9246bff18e0f0253df77d59bbcf5d033e2bc 100644 --- a/utils.go +++ b/utils.go @@ -77,8 +77,8 @@ func roundToDecimal(value float64, places int) float64 { // convert Mired to Kelvin and Kelvin to Mired // Mired = 1,000,000 / Temperature in Kelvin -func convertMired(value float64) float64 { - return 1e6 / float64(value) +func convertMired(value int) int { + return 1e6 / int(value) } func convertToFloat64(value interface{}) (float64, error) { @@ -111,3 +111,34 @@ func convertToFloat64(value interface{}) (float64, error) { return 0.0, fmt.Errorf("Unable to converts %v (%T)", value, value) } } + +func convertToInt(value interface{}) (int, error) { + switch v := value.(type) { + case float32: + return int(v), nil + case float64: + return int(v), nil + case int: + return v, nil + case uint: + return int(v), nil + case int8: + return int(v), nil + case uint8: + return int(v), nil + case int16: + return int(v), nil + case uint16: + return int(v), nil + case int32: + return int(v), nil + case uint32: + return int(v), nil + case int64: + return int(v), nil + case uint64: + return int(v), nil + default: + return 0, fmt.Errorf("Unable to converts %v (%T)", value, value) + } +} diff --git a/xaal.go b/xaal.go index 08aff389c658da0263b9f89509d3b7b72c29e8a9..e572fb580cb3ac51bf189db695cb0c7f8206b14c 100644 --- a/xaal.go +++ b/xaal.go @@ -294,10 +294,10 @@ func (dev *Lamp) update(payload map[string]interface{}) { } // brightness - brightness, exists := payload["brightness"].(float64) + brightness, exists := payload["brightness"].(int) if exists { brightness = brightness / 255 * 100 - dev.GetAttribute("brightness").SetValue(int(brightness)) + dev.GetAttribute("brightness").SetValue(brightness) } // color_temp @@ -305,10 +305,10 @@ 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"].(float64) + color_temp, exists := payload["color_temp"].(int) if exists { color_temp = convertMired(color_temp) - dev.GetAttribute("white_temperature").SetValue(int(color_temp)) + dev.GetAttribute("white_temperature").SetValue(color_temp) } } @@ -348,8 +348,8 @@ func (dev *Lamp) toggle(xaal.MessageBody) *xaal.MessageBody { func (dev *Lamp) setBrightness(body xaal.MessageBody) *xaal.MessageBody { value, exists := body["brightness"] if exists { - target, _ := convertToFloat64(value) - dev.Z2MDevice.Set(fmt.Sprintf(`{"brightness": %d}`, int(target*255/100))) + target, _ := convertToInt(value) + dev.Z2MDevice.Set(fmt.Sprintf(`{"brightness": %d}`, target*255/100)) } return nil } @@ -357,9 +357,9 @@ func (dev *Lamp) setBrightness(body xaal.MessageBody) *xaal.MessageBody { func (dev *Lamp) setWhiteTemperature(body xaal.MessageBody) *xaal.MessageBody { value, exists := body["white_temperature"] if exists { - target, _ := convertToFloat64(value) + target, _ := convertToInt(value) target = convertMired(target) - dev.Z2MDevice.Set(fmt.Sprintf(`{"color_temp": %d}`, int(target))) + dev.Z2MDevice.Set(fmt.Sprintf(`{"color_temp": %d}`, target)) } return nil } @@ -392,13 +392,13 @@ func (dev *Lamp) setMode(body xaal.MessageBody) *xaal.MessageBody { color := colorful.Hsv(hsv[0], hsv[1], hsv[2]) dev.Z2MDevice.Set(fmt.Sprintf(`{"color": { "hex": "%s" }}`, color.Hex())) case "white": - value := dev.GetAttribute("white_temperature").Value.(float64) + value := dev.GetAttribute("white_temperature").Value.(int) // if booted in color mode, while_temperature is 0 => divide crash if value == 0 { value = 3000 } value = convertMired(value) - dev.Z2MDevice.Set(fmt.Sprintf(`{"color_temp": %d}`, int(value))) + dev.Z2MDevice.Set(fmt.Sprintf(`{"color_temp": %d}`, value)) } } return nil