-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathXiaomi-Zigbee-Button.groovy
176 lines (151 loc) · 5.81 KB
/
Xiaomi-Zigbee-Button.groovy
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
174
175
176
/**
* Xiaomi Zigbee Button
*
* Modified by RaveTam from Eric Maycock implementation below. Added to support Holdable Button and Battery status reporting
* https://github.com/erocm123/SmartThingsPublic/blob/master/devicetypes/erocm123/xiaomi-smart-button.src/xiaomi-smart-button.groovy
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
metadata {
definition (name: "Xiaomi Zigbee Button", namespace: "lazcad", author: "RaveTam") {
capability "Battery"
capability "Button"
capability "Holdable Button"
capability "Configuration"
capability "Sensor"
capability "Refresh"
attribute "lastPress", "string"
attribute "batterylevel", "string"
}
simulator {
status "button 1 pressed": "on/off: 0"
status "button 1 released": "on/off: 1"
}
preferences{
input ("holdTime", "number", title: "Minimum time in seconds for a press to count as \"held\"",
defaultValue: 4, displayDuringSetup: false)
}
tiles(scale: 2) {
standardTile("button", "device.button", decoration: "flat", width: 2, height: 2) {
state "default", icon: "st.unknown.zwave.remote-controller", backgroundColor: "#ffffff"
}
valueTile("battery", "device.battery", decoration: "flat", inactiveLabel: false, width: 2, height: 2) {
state "battery", label:'${currentValue}% battery', unit:""
}
standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", action:"refresh.refresh", icon:"st.secondary.refresh"
}
standardTile("configure", "device.configure", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "configure", label:'', action:"configuration.configure", icon:"st.secondary.configure"
}
main (["button"])
details(["button", "battery", "refresh", "configure"])
}
}
def parse(String description) {
log.debug "Parsing '${description}'"
def value = zigbee.parse(description)?.text
log.debug "Parse: $value"
def descMap = zigbee.parseDescriptionAsMap(description)
def results = []
if (description?.startsWith('on/off: '))
results = parseCustomMessage(description)
if (description?.startsWith('catchall:'))
results = parseCatchAllMessage(description)
return results;
}
def configure(){
[
"zdo bind 0x${device.deviceNetworkId} 1 2 0 {${device.zigbeeId}} {}", "delay 5000",
"zcl global send-me-a-report 2 0 0x10 1 0 {01}", "delay 500",
"send 0x${device.deviceNetworkId} 1 2"
]
}
def refresh(){
"st rattr 0x${device.deviceNetworkId} 1 2 0"
"st rattr 0x${device.deviceNetworkId} 1 0 0"
log.debug "refreshing"
createEvent([name: 'batterylevel', value: '100', data:[buttonNumber: 1], displayed: false])
}
private Map parseCatchAllMessage(String description) {
Map resultMap = [:]
def cluster = zigbee.parse(description)
log.debug cluster
if (cluster) {
switch(cluster.clusterId) {
case 0x0000:
resultMap = getBatteryResult(cluster.data.last())
break
case 0xFC02:
log.debug 'ACCELERATION'
break
case 0x0402:
log.debug 'TEMP'
// temp is last 2 data values. reverse to swap endian
String temp = cluster.data[-2..-1].reverse().collect { cluster.hex1(it) }.join()
def value = getTemperature(temp)
resultMap = getTemperatureResult(value)
break
}
}
return resultMap
}
private Map getBatteryResult(rawValue) {
log.debug 'Battery'
def linkText = getLinkText(device)
log.debug rawValue
int battValue = rawValue
def result = [
name: 'battery',
value: battValue,
unit: "%",
isStateChange:true,
descriptionText : "${linkText} battery was ${rawValue}%"
]
log.debug result.descriptionText
state.lastbatt = new Date().time
return createEvent(result)
}
private Map parseCustomMessage(String description) {
if (description?.startsWith('on/off: ')) {
if (description == 'on/off: 0') //button pressed
return createPressEvent(1)
else if (description == 'on/off: 1') //button released
return createButtonEvent(1)
}
}
//this method determines if a press should count as a push or a hold and returns the relevant event type
private createButtonEvent(button) {
def currentTime = now()
def startOfPress = device.latestState('lastPress').date.getTime()
def timeDif = currentTime - startOfPress
def holdTimeMillisec = (settings.holdTime?:3).toInteger() * 1000
if (timeDif < 0)
return [] //likely a message sequence issue. Drop this press and wait for another. Probably won't happen...
else if (timeDif < holdTimeMillisec)
return createEvent(name: "button", value: "pushed", data: [buttonNumber: button], descriptionText: "$device.displayName button $button was pushed", isStateChange: true)
else
return createEvent(name: "button", value: "held", data: [buttonNumber: button], descriptionText: "$device.displayName button $button was held", isStateChange: true)
}
private createPressEvent(button) {
return createEvent([name: 'lastPress', value: now(), data:[buttonNumber: button], displayed: false])
}
//Need to reverse array of size 2
private byte[] reverseArray(byte[] array) {
byte tmp;
tmp = array[1];
array[1] = array[0];
array[0] = tmp;
return array
}
private String swapEndianHex(String hex) {
reverseArray(hex.decodeHex()).encodeHex()
}