-
Notifications
You must be signed in to change notification settings - Fork 36
/
lumi.sensor_cube.js
173 lines (166 loc) · 4.9 KB
/
lumi.sensor_cube.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const HomeKitDevice = require('../HomeKitDevice')
const { mountXiaomiButteryCharacteristics } = require('../utils/xiaomi')
class AqaraCubeSensor extends HomeKitDevice {
static get description() {
return {
model: 'lumi.sensor_cube',
manufacturer: 'LUMI',
name: 'Aqara Cube Sensor',
}
}
getAvailbleServices() {
return [{
name: 'Shake',
type: 'StatelessProgrammableSwitch',
subtype: 'Shake',
}, {
name: 'Wakeup',
type: 'StatelessProgrammableSwitch',
subtype: 'Wakeup',
}, {
name: 'Fall',
type: 'StatelessProgrammableSwitch',
subtype: 'Fall',
}, {
name: 'Tap',
type: 'StatelessProgrammableSwitch',
subtype: 'Tap',
}, {
name: 'Slide',
type: 'StatelessProgrammableSwitch',
subtype: 'Slide',
}, {
name: 'Flip 180',
type: 'StatelessProgrammableSwitch',
subtype: 'Flip180',
}, {
name: 'Flip 90',
type: 'StatelessProgrammableSwitch',
subtype: 'Flip90',
}, {
name: 'Rotate Left',
type: 'StatelessProgrammableSwitch',
subtype: 'RotateLeft',
}, {
name: 'Rotate Right',
type: 'StatelessProgrammableSwitch',
subtype: 'RotateRight',
}, {
name: 'Battery',
type: 'BatteryService',
}]
}
onDeviceReady() {
this.zigbee.update({ status: 'online', joinTime: Math.floor(Date.now() / 1000) })
const defaultActionProps = { minValue: 0, maxValue: 1, validValues: [0] }
this.updateActionService('Shake', defaultActionProps, 1)
this.updateActionService('Wakeup', defaultActionProps, 2)
this.updateActionService('Fall', defaultActionProps, 3)
this.updateActionService('Tap', defaultActionProps, 4)
this.updateActionService('Slide', defaultActionProps, 5)
this.updateActionService('Flip 180', defaultActionProps, 6)
this.updateActionService('Flip 90', defaultActionProps, 7)
this.updateActionService('Rotate Left', defaultActionProps, 8)
this.updateActionService('Rotate Right', defaultActionProps, 9)
const actionMultistateConfig = {
endpointId: 2,
clusterId: 'genMultistateInput',
attrId: 'presentValue',
minInt: 1,
maxInt: 60,
repChange: null,
}
const actionAnalogConfig = {
endpointId: 3,
clusterId: 'genAnalogInput',
attrId: 'presentValue',
minInt: 1,
maxInt: 60,
repChange: null,
}
this.zigbee.subscribe(
actionMultistateConfig.endpointId,
actionMultistateConfig.clusterId,
actionMultistateConfig.attrId,
actionMultistateConfig.minInt,
actionMultistateConfig.maxInt,
actionMultistateConfig.repChange,
this.handleMultistateActionReport.bind(this)
)
this.zigbee.subscribe(
actionAnalogConfig.endpointId,
actionAnalogConfig.clusterId,
actionAnalogConfig.attrId,
actionAnalogConfig.minInt,
actionAnalogConfig.maxInt,
actionAnalogConfig.repChange,
this.handleAnalogActionReport.bind(this)
)
mountXiaomiButteryCharacteristics(this, {
reportMinInt: 1,
reportMaxInt: 60,
})
}
updateActionService(service, props, index) {
this.getServiceCharacteristic(
service, 'ProgrammableSwitchEvent'
).setProps(props)
this.getServiceCharacteristic(
service, 'ServiceLabelIndex'
).setProps(index)
}
handleMultistateActionReport(value) {
/*
Source: https://github.com/kirovilya/ioBroker.zigbee
+---+
| 2 |
+---+---+---+
| 4 | 0 | 1 |
+---+---+---+
|M5I|
+---+
| 3 |
+---+
Side 5 is with the MI logo, side 3 contains the battery door.
value = 0 = shake
value = 2 = wakeup
value = 3 = fly/fall
value = y + x * 8 + 64 = 90º flip from side x on top to side y on top
value = x + 128 = 180º flip to side x on top
value = x + 256 = push/slide cube while side x is on top
value = x + 512 = double tap while side x is on top
*/
let service
if (value === 0) {
service = 'Shake'
} else if (value === 2) {
service = 'Wakeup'
} else if (value === 3) {
service = 'Fall'
} else if (value >= 512) {
service = 'Tap'
} else if (value >= 256) {
service = 'Slide'
} else if (value >= 128) {
service = 'Flip 180'
} else if (value >= 64) {
service = 'Flip 90'
}
if (service) {
this.getServiceCharacteristic(
service, 'ProgrammableSwitchEvent'
).updateValue(this.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS)
}
}
handleAnalogActionReport(value) {
/*
Source: https://github.com/kirovilya/ioBroker.zigbee
value = rotation angle left < 0, right > 0
*/
const service = value < 0 ? 'Rotate Left' : 'Rotate Right'
this.getServiceCharacteristic(
service, 'ProgrammableSwitchEvent'
).updateValue(this.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS)
}
}
module.exports = AqaraCubeSensor