-
Notifications
You must be signed in to change notification settings - Fork 3
/
light_dimmer.go
87 lines (73 loc) · 1.76 KB
/
light_dimmer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"strconv"
log "github.com/sirupsen/logrus"
"github.com/brutella/hap/accessory"
"github.com/brutella/hap/characteristic"
)
type LightDimmer struct {
*accessory.Lightbulb
Brightness *characteristic.Brightness
Name *characteristic.Name
}
func NewLightDimmer(cio CalaosIO, id uint64) *LightDimmer {
acc := LightDimmer{}
info := accessory.Info{
Name: cio.Name,
SerialNumber: cio.ID,
Manufacturer: "Calaos",
Model: cio.IoType,
}
acc.Lightbulb = accessory.NewLightbulb(info)
acc.Lightbulb.Id = id
acc.Brightness = characteristic.NewBrightness()
acc.Name = characteristic.NewName()
acc.Lightbulb.Lightbulb.AddC(acc.Brightness.C)
acc.Lightbulb.Lightbulb.AddC(acc.Name.C)
acc.Update(&cio)
acc.Lightbulb.Lightbulb.On.OnValueRemoteUpdate(func(on bool) {
if on == true {
log.Debug("Switch is on")
cio.State = "true"
CalaosUpdate(cio)
} else {
log.Debug("Switch is off")
cio.State = "false"
CalaosUpdate(cio)
}
})
acc.Brightness.OnValueRemoteUpdate(func(val int) {
cio.State = "set " + strconv.Itoa(val)
CalaosUpdate(cio)
})
return &acc
}
func (acc *LightDimmer) Update(cio *CalaosIO) error {
log.Debug("try to update val ", cio.State)
if cio.GuiType == "light_dimmer" {
v, err := strconv.Atoi(cio.State)
if err == nil {
acc.Brightness.SetValue(v)
if v == 0 {
acc.Lightbulb.Lightbulb.On.SetValue(false)
} else {
acc.Lightbulb.Lightbulb.On.SetValue(true)
}
}
return err
} else {
v, err := strconv.ParseBool(cio.State)
if err == nil {
acc.Lightbulb.Lightbulb.On.SetValue(v)
if v {
acc.Brightness.SetValue(100)
} else {
acc.Brightness.SetValue(0)
}
}
return err
}
}
func (acc *LightDimmer) AccessoryGet() *accessory.A {
return acc.Lightbulb.A
}