mirror of
https://github.com/nikdoof/hapz2m.git
synced 2026-01-30 02:18:22 +00:00
Add support for dimmers and dimmable bulbs
The exposes entry looks similar to a switch, except the type is a "light" and it has `state` and `brightness`. Tested only on single channel dimmers. Also added a PercentageTranslator to translate between the HomeKit `brightness`, which is a percentage, to/from an arbitrary numeric value in Z2M.
This commit is contained in:
50
s_light.go
Normal file
50
s_light.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package hapz2m
|
||||
|
||||
import (
|
||||
"github.com/brutella/hap/accessory"
|
||||
"github.com/brutella/hap/characteristic"
|
||||
"github.com/brutella/hap/service"
|
||||
)
|
||||
|
||||
func createLightServices(dev *Device) (byte, []*service.S, []*ExposeMapping, error) {
|
||||
var svcs []*service.S
|
||||
var exposes []*ExposeMapping
|
||||
|
||||
for _, exp := range dev.Definition.Exposes {
|
||||
if exp.Ignored() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch {
|
||||
case exp.Type == "light" && len(exp.Features) > 0:
|
||||
exp := exp // create a copy
|
||||
|
||||
light := service.NewLightbulb()
|
||||
|
||||
for _, feat := range exp.Features {
|
||||
if !feat.IsStateSetGet() {
|
||||
continue
|
||||
}
|
||||
|
||||
feat := feat
|
||||
|
||||
switch {
|
||||
case feat.Name == "state" && feat.Type == "binary":
|
||||
svcs = append(svcs, light.S)
|
||||
exposes = append(exposes, &ExposeMapping{&feat, light.On.C, nil})
|
||||
|
||||
case feat.Name == "brightness" && feat.Type == "numeric":
|
||||
brightness := characteristic.NewBrightness()
|
||||
light.AddC(brightness.C)
|
||||
exposes = append(exposes, &ExposeMapping{&feat, brightness.C, nil})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return accessory.TypeLightbulb, svcs, exposes, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterCreateServiceHandler(createLightServices)
|
||||
}
|
||||
Reference in New Issue
Block a user