-
Notifications
You must be signed in to change notification settings - Fork 25
/
dualwiredwallswitch.go
100 lines (89 loc) · 2.5 KB
/
dualwiredwallswitch.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
88
89
90
91
92
93
94
95
96
97
98
99
100
package migateway
const (
MODEL_DUALWIREDSWITCH = "ctrl_neutral2"
FIELD_CHANNEL0 = "channel_0"
FIELD_CHANNEL1 = "channel_1"
)
// DualWiredWallSwitch is a wired wall switch with 2 switches to control to power-lines.
// The switches can be controlled by the gateway using the zigbee protocol.
type DualWiredWallSwitch struct {
*Device
State DualWiredWallSwitchState
}
type DualWiredWallSwitchState struct {
Channel0On bool
Channel1On bool
}
type DualWiredWallSwitchStateChange struct {
ID string
From DualWiredWallSwitchState
To DualWiredWallSwitchState
}
func (d DualWiredWallSwitchStateChange) IsChanged() bool {
return d.From.Channel0On != d.To.Channel0On || d.From.Channel1On != d.To.Channel1On
}
func NewDualWiredSwitch(dev *Device) *DualWiredWallSwitch {
return &DualWiredWallSwitch{
Device: dev,
State: DualWiredWallSwitchState{Channel0On: false, Channel1On: false},
}
}
func (s *DualWiredWallSwitch) Set(dev *Device) {
change := &DualWiredWallSwitchStateChange{ID: s.Sid, From: s.State, To: s.State}
if dev.hasField(FIELD_CHANNEL0) {
channel0 := dev.GetDataAsBool(FIELD_CHANNEL0)
s.State.Channel0On = channel0
change.To = s.State
//LOGGER.Warn("%s", status)
}
if dev.hasField(FIELD_CHANNEL1) {
channel1 := dev.GetDataAsBool(FIELD_CHANNEL1)
s.State.Channel1On = channel1
change.To = s.State
//LOGGER.Warn("%s", status)
}
if change.IsChanged() || s.shouldPushUpdates() {
s.Aqara.StateMessages <- change
}
if dev.Token != "" {
s.Token = dev.Token
}
}
func (s *DualWiredWallSwitch) TurnOn() error {
data := map[string]interface{}{
"channel_0": "on",
"channel_1": "on",
}
return s.GatewayConnection.Control(s.Device, data)
}
func (s *DualWiredWallSwitch) TurnOnChannel0() error {
data := map[string]interface{}{
"channel_0": "on",
}
return s.GatewayConnection.Control(s.Device, data)
}
func (s *DualWiredWallSwitch) TurnOnChannel1() error {
data := map[string]interface{}{
"channel_1": "on",
}
return s.GatewayConnection.Control(s.Device, data)
}
func (s *DualWiredWallSwitch) TurnOff() error {
data := map[string]interface{}{
"channel_0": "off",
"channel_1": "off",
}
return s.GatewayConnection.Control(s.Device, data)
}
func (s *DualWiredWallSwitch) TurnOffChannel0() error {
data := map[string]interface{}{
"channel_0": "off",
}
return s.GatewayConnection.Control(s.Device, data)
}
func (s *DualWiredWallSwitch) TurnOffChannel1() error {
data := map[string]interface{}{
"channel_1": "off",
}
return s.GatewayConnection.Control(s.Device, data)
}