mirror of
https://github.com/nikdoof/hapz2m.git
synced 2026-01-30 02:18:22 +00:00
"Initial" working version, after cleanup.
This commit is contained in:
51
s_switch.go
Normal file
51
s_switch.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package hapz2m
|
||||
|
||||
import (
|
||||
"github.com/brutella/hap/accessory"
|
||||
"github.com/brutella/hap/characteristic"
|
||||
"github.com/brutella/hap/service"
|
||||
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var outletRegex = regexp.MustCompile(`(?i)\boutlet\b`)
|
||||
|
||||
func createSwitchServices(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 == "switch" && len(exp.Features) > 0:
|
||||
exp := exp // create a copy
|
||||
|
||||
f := exp.Features[0]
|
||||
if f.Name == "state" && f.Type == "binary" && f.IsStateSetGet() {
|
||||
sw := service.NewSwitch()
|
||||
|
||||
// add a name/label for this switch
|
||||
n := characteristic.NewName()
|
||||
n.SetValue(f.Endpoint)
|
||||
sw.AddC(n.C)
|
||||
|
||||
svcs = append(svcs, sw.S)
|
||||
exposes = append(exposes, &ExposeMapping{&f, sw.On.C, nil})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
accTyp := accessory.TypeSwitch
|
||||
if outletRegex.MatchString(dev.Definition.Description) {
|
||||
accTyp = accessory.TypeOutlet
|
||||
}
|
||||
|
||||
return accTyp, svcs, exposes, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterCreateServiceHandler(createSwitchServices)
|
||||
}
|
||||
Reference in New Issue
Block a user