Skip to content
Snippets Groups Projects
Commit 37666654 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Switch white temperature and brightness to int

Dropped the convertFloat64 for white_temperature and brightness. The
code is cleaner.
parent 0a1281e3
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
}
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment