-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathorvibo-contact-sensor.groovy.old
127 lines (107 loc) · 3.88 KB
/
orvibo-contact-sensor.groovy.old
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
/**
* Orvibo Contact Sensor
*
* Copyright Wayne Man 2016
*
* 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.
*
* Orvibo Contact Sensor Device Type
* Battery levels updates periodically, for an instant update press physical button on sensor once
* 30/04/2016 fixed fingerprint
* 09/05/2016 added heartbeat to help track if sensor is alive (recommend using a devicecheck smartapp)
*/
metadata {
definition (name: "Orvibo Contact Sensor", namespace: "a4refillpad", author: "Wayne Man") {
capability "Contact Sensor"
capability "Sensor"
capability "Battery"
attribute "lastCheckin", "String"
fingerprint inClusters: "0000,0001,0003,0500", manufacturer: "\u6B27\u745E", model: "75a4bfe8ef9c4350830a25d13e3ab068"
}
// simulator metadata
simulator {
// status messages
status "open": "zone report :: type: 19 value: 0031"
status "closed": "zone report :: type: 19 value: 0030"
}
tiles(scale: 2) {
multiAttributeTile(name:"contact", type: "lighting", width: 6, height: 4) {
tileAttribute ("device.contact", key: "PRIMARY_CONTROL") {
attributeState("open", label:'open', icon:"st.contact.contact.open", backgroundColor:"#ffa81e")
attributeState("closed", label:'closed', icon:"st.contact.contact.closed", backgroundColor:"#79b821")
}
}
valueTile("battery", "device.battery", inactiveLabel: false, width: 2, height: 2, decoration: "flat") {
state "battery", label:'${currentValue}% battery', unit:""
}
main "contact"
details(["contact", "battery"])
}
}
// Parse incoming device messages to generate events
def parse(String description) {
def name = null
def value = description
def descriptionText = null
def now = new Date()
log.debug "Parsing: ${description}"
Map map = [:]
List listMap = []
List listResult = []
if (zigbee.isZoneType19(description)) {
name = "contact"
value = zigbee.translateStatusZoneType19(description) ? "open" : "closed"
} else if(description?.startsWith("read attr -")) {
map = parseReportAttributeMessage(description)
}
def result = createEvent(name: name, value: value)
log.debug "Parse returned ${result?.descriptionText}"
// send event for heartbeat
sendEvent(name: "lastCheckin", value: now)
listResult << result
if (listMap) {
for (msg in listMap) {
listResult << createEvent(msg)
}
}
else if (map) {
listResult << createEvent(map)
}
log.debug "Parse returned ${result?.descriptionText}"
return listResult
}
private Map parseReportAttributeMessage(String description) {
Map descMap = (description - "read attr - ").split(",").inject([:]) {
map, param -> def nameAndValue = param.split(":")
map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
}
Map resultMap = [:]
log.info "IN parseReportAttributeMessage()"
log.debug "descMap ${descMap}"
switch(descMap.cluster) {
case "0001":
log.debug "Battery status reported"
if(descMap.attrId == "0021") {
resultMap.name = 'battery'
resultMap.value = (convertHexToInt(descMap.value) / 2)
log.debug "Battery Percentage convert to ${resultMap.value}%"
}
break
default:
log.info descMap.cluster
log.info "cluster1"
break
}
log.info "OUT parseReportAttributeMessage()"
return resultMap
}
private Integer convertHexToInt(hex) {
Integer.parseInt(hex,16)
}