Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
z2m
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xAAL
Code
Go
z2m
Commits
1f57e71b
Commit
1f57e71b
authored
5 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Fix setMode
Done w/ setMode. This was tricky because setting color_mode doesn't work.
parent
6b57429c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
xaal.go
+63
-27
63 additions, 27 deletions
xaal.go
with
63 additions
and
27 deletions
xaal.go
+
63
−
27
View file @
1f57e71b
...
@@ -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
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment