/
xiaomidoublebutton.go
68 lines (57 loc) · 1.71 KB
/
xiaomidoublebutton.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
package deviceadapters
import (
"fmt"
"github.com/function61/hautomo/pkg/evdevcodes"
"github.com/function61/hautomo/pkg/ezstack/ezhub/hubtypes"
"github.com/function61/hautomo/pkg/ezstack/zcl/cluster"
"github.com/function61/hautomo/pkg/ezstack/zigbee"
)
func init() {
leftButton := evdevcodes.Btn0
rightButton := evdevcodes.Btn1
/* This device uses Zigbee endpoints to model different buttons / combinations
1 => left button
2 => right button
3 => both
We'll translate these to more semantic values. Endpoint 3 to mean "both buttons" is not a
pretty design. If you had three buttons, how many permutations would "two buttons pressed" need?
How about 9 buttons?
*/
endpointToPressedButtons := map[zigbee.EndpointId][]evdevcodes.KeyOrButton{
1: {leftButton},
2: {rightButton},
3: {leftButton, rightButton},
}
defineAdapter(modelAqaraDoubleButtonSensor,
aqaraVoltageEtc,
withBatteryType(BatteryCR2032),
attributeParser("genMultistateInput.presentValue", func(attr *cluster.Attribute, actx *hubtypes.AttrsCtx) error {
pressCount := int(attr.Value.(uint64))
isHold := pressCount == 0
pressedButtons, found := endpointToPressedButtons[actx.Endpoint]
if !found {
return fmt.Errorf("unexpected endpoint: %d", actx.Endpoint)
}
actx.Attrs.Press = &hubtypes.AttrPress{
Key: pressedButtons[0],
KeysAdditional: pressedButtons[1:],
Kind: func() hubtypes.PressKind {
if isHold {
return hubtypes.PressKindHold
} else {
return hubtypes.PressKindUp
}
}(),
CountRaw: func() *int {
if pressCount >= 2 {
return &pressCount
} else {
return nil
}
}(),
LastReport: actx.Reported,
}
return nil
}),
)
}