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

Fix setMode

Done w/ setMode. This was tricky because setting color_mode doesn't
work.
parent 6b57429c
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"log/slog" "log/slog"
"math" "math"
"github.com/davecgh/go-spew/spew"
"github.com/lucasb-eyer/go-colorful" "github.com/lucasb-eyer/go-colorful"
"gitlab.imt-atlantique.fr/xaal/code/go/core/schemas" "gitlab.imt-atlantique.fr/xaal/code/go/core/schemas"
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid" "gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
...@@ -242,7 +241,7 @@ func NewLamp(addr uuid.UUID, zDev *Z2MDevice, expose *Expose) XAALDeviceInterfac ...@@ -242,7 +241,7 @@ func NewLamp(addr uuid.UUID, zDev *Z2MDevice, expose *Expose) XAALDeviceInterfac
dev.AddMethod("set_brightness", dev.setBrightness) dev.AddMethod("set_brightness", dev.setBrightness)
} }
if expose.GetFeature("color_temp") != nil { if expose.GetFeature("color_temp") != nil {
// All bulb w/ color temp are dimmer dev.DevType = "lamp.dimmer"
dev.AddAttribute("white_temperature", 0) dev.AddAttribute("white_temperature", 0)
dev.AddMethod("set_white_temperature", dev.setWhiteTemperature) dev.AddMethod("set_white_temperature", dev.setWhiteTemperature)
} }
...@@ -254,11 +253,18 @@ func NewLamp(addr uuid.UUID, zDev *Z2MDevice, expose *Expose) XAALDeviceInterfac ...@@ -254,11 +253,18 @@ func NewLamp(addr uuid.UUID, zDev *Z2MDevice, expose *Expose) XAALDeviceInterfac
dev.AddMethod("set_hsv", dev.setHSV) dev.AddMethod("set_hsv", dev.setHSV)
dev.AddMethod("set_mode", dev.setMode) dev.AddMethod("set_mode", dev.setMode)
} }
dev.setup(zDev) dev.setup(zDev)
return dev return dev
} }
func (dev *Lamp) getMode() string {
mode := dev.GetAttribute("mode")
if mode != nil {
return mode.Value.(string)
}
return "white"
}
func (dev *Lamp) update(payload map[string]interface{}) { func (dev *Lamp) update(payload map[string]interface{}) {
// state // state
state, exists := payload["state"].(string) state, exists := payload["state"].(string)
...@@ -271,12 +277,8 @@ func (dev *Lamp) update(payload map[string]interface{}) { ...@@ -271,12 +277,8 @@ func (dev *Lamp) update(payload map[string]interface{}) {
light.SetValue(false) light.SetValue(false)
} }
} }
// brightness
brightness, exists := payload["brightness"].(float64) // color_mode
if exists {
brightness = brightness / 255 * 100
dev.GetAttribute("brightness").SetValue(int(brightness))
}
color_mode, exists := payload["color_mode"].(string) color_mode, exists := payload["color_mode"].(string)
if exists { if exists {
mode := dev.GetAttribute("mode") mode := dev.GetAttribute("mode")
...@@ -291,26 +293,39 @@ func (dev *Lamp) update(payload map[string]interface{}) { ...@@ -291,26 +293,39 @@ func (dev *Lamp) update(payload map[string]interface{}) {
} }
} }
// color // brightness
color, exists := payload["color"].(map[string]interface{}) brightness, exists := payload["brightness"].(float64)
if exists { if exists {
x, _ := color["x"].(float64) brightness = brightness / 255 * 100
y, _ := color["y"].(float64) dev.GetAttribute("brightness").SetValue(int(brightness))
brigthness, _ := payload["brightness"].(float64)
value := xyToColor(x, y, brigthness)
slog.Warn("color", "color", value.Hex(), "x", x, "y", y, "tmp", brigthness)
hue, sat, val := value.Hsv()
hue = roundToDecimal(hue, 1)
sat = roundToDecimal(sat, 3)
val = roundToDecimal(val, 3)
dev.GetAttribute("hsv").SetValue([]float64{hue, sat, val})
} }
// color_temp // color_temp
color_temp, exists := payload["color_temp"].(float64) if dev.getMode() == "white" {
if exists { // color_temp change when we are in color mode (looks like a z2m bug)
// Mired = 1,000,000 / Temperature in Kelvin color_temp, exists := payload["color_temp"].(float64)
color_temp = 1000000 / color_temp if exists {
dev.GetAttribute("white_temperature").SetValue(int(color_temp)) // Mired = 1,000,000 / Temperature in Kelvin
color_temp = 1000000 / color_temp
dev.GetAttribute("white_temperature").SetValue(int(color_temp))
}
}
// colors
if dev.getMode() == "color" {
color, exists := payload["color"].(map[string]interface{})
if exists {
x, _ := color["x"].(float64)
y, _ := color["y"].(float64)
brigthness, _ := payload["brightness"].(float64)
value := xyToColor(x, y, brigthness)
slog.Debug("color", "color", value.Hex(), "x", x, "y", y, "tmp", brigthness)
hue, sat, val := value.Hsv()
hue = roundToDecimal(hue, 1)
sat = roundToDecimal(sat, 3)
val = roundToDecimal(val, 3)
dev.GetAttribute("hsv").SetValue([]float64{hue, sat, val})
}
} }
} }
...@@ -361,7 +376,28 @@ func (dev *Lamp) setHSV(body xaal.MessageBody) *xaal.MessageBody { ...@@ -361,7 +376,28 @@ func (dev *Lamp) setHSV(body xaal.MessageBody) *xaal.MessageBody {
} }
func (dev *Lamp) setMode(body xaal.MessageBody) *xaal.MessageBody { func (dev *Lamp) setMode(body xaal.MessageBody) *xaal.MessageBody {
spew.Dump(body) value, exist := body["mode"].(string)
if exist {
// for a unknown reason, we are unable to set the color_mode
// z2m doesn't give an error, but the color_mode doesn't change
// so we set old color or temperature
switch value {
case "color":
// dev.Z2MDevice.Set(`{"color_mode": "xy"}`)
hsv := dev.GetAttribute("hsv").Value.([]float64)
color := colorful.Hsv(hsv[0], hsv[1], hsv[2])
dev.Z2MDevice.Set(fmt.Sprintf(`{"color": { "hex": "%s" }}`, color.Hex()))
case "white":
// dev.Z2MDevice.Set(`{"color_mode": "color_temp"}`)
value := dev.GetAttribute("white_temperature").Value.(int)
// if booted in color mode, while_temperature is 0 => divide crash
if value == 0 {
value = 3000
}
value = 1000000 / value
dev.Z2MDevice.Set(fmt.Sprintf(`{"color_temp": %d}`, value))
}
}
return nil return nil
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment