forked from alecm/SmartThingsAlecM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xiaomi-humidity-temperature-sensor.src
270 lines (236 loc) · 10 KB
/
xiaomi-humidity-temperature-sensor.src
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Copyright 2017 A4refillpad
*
* 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.
*
* 2017-03 First release of the Xiaomi Temp/Humidity Device Handler
* 2017-03 Includes battery level (hope it works, I've only had access to a device for a limited period, time will tell!)
* 2017-03 Last checkin activity to help monitor health of device and multiattribute tile
* 2017-03 Changed temperature to update on .1° changes - much more useful
* 2017-03-08 Changed the way the battery level is being measured. Very different to other Xiaomi sensors.
* 2017-03-23 Added Fahrenheit support
* 2017-03-25 Minor update to display unknown battery as "--", added fahrenheit colours to main and device tiles
* 2017-03-29 Temperature offset preference added to handler
*
* known issue: these devices do not seem to respond to refresh requests left in place in case things change
* known issue: tile formatting on ios and android devices vary a little due to smartthings app - again, nothing I can do about this
* known issue: there's nothing I can do about the pairing process with smartthings. it is indeed non standard, please refer to community forum for details
*
* AlecM 2017-07-03 -Modification of A4refillpads excellent Xiaomi Temperature Humidity Sensor to put Humidity in Main Tile
* - Changed name to Xiaomi Humidity Temperature Sensor to indicate Humidity is primary
* - Adjusted background colors for main tile (now Humidity) based on ASHRAE Standard 62-2001
* " Ventilation for Acceptable Indoor Air Quality"
* //https://buildingscience.com/documents/reports/rr-0203-relative-humidity/view
* - Made temperature2 tile 2 x 2
* - Set Humidity to Main on line 122 and put in temperature2 tile
*
*/
metadata {
definition (name: "Xiaomi Humidity Temperature Sensor", namespace: "a4refillpad", author: "a4refillpad") {
capability "Temperature Measurement"
capability "Relative Humidity Measurement"
capability "Sensor"
capability "Battery"
capability "Refresh"
attribute "lastCheckin", "String"
fingerprint profileId: "0104", deviceId: "0302", inClusters: "0000,0001,0003,0009,0402,0405"
}
// simulator metadata
simulator {
for (int i = 0; i <= 100; i += 10) {
status "${i}F": "temperature: $i F"
}
for (int i = 0; i <= 100; i += 10) {
status "${i}%": "humidity: ${i}%"
}
}
preferences {
section {
input title: "Temperature Offset", description: "This feature allows you to correct any temperature variations by selecting an offset. Ex: If your sensor consistently reports a temp that's 5 degrees too warm, you'd enter '-5'. If 3 degrees too cold, enter '+3'. Please note, any changes will take effect only on the NEXT temperature change.", displayDuringSetup: false, type: "paragraph", element: "paragraph"
input "tempOffset", "number", title: "Degrees", description: "Adjust temperature by this many degrees", range: "*..*", displayDuringSetup: false
}
}
// UI tile definitions
tiles(scale: 2) {
multiAttributeTile(name:"humidity", type:"generic", width:6, height:4) {
tileAttribute("device.humidity", key:"PRIMARY_CONTROL"){
attributeState("default", label:'${currentValue}%',
//AlecM 2017-07-04 - Adjusted background colors based on ASHRAE Standard 62-2001 "Ventilation for Acceptable Indoor Air Quality"
backgroundColors:[
[value: 0, color: "#FFFCDF"],
[value: 4, color: "#FDF789"],
[value: 20, color: "#A5CF63"],
[value: 23, color: "#6FBD7F"],
[value: 56, color: "#4CA98C"],
[value: 59, color: "#0072BB"],
[value: 76, color: "#085396"]
]
)
}
tileAttribute("device.lastCheckin", key: "SECONDARY_CONTROL") {
attributeState("default", label:'Last Update: ${currentValue}', icon: "st.Health & Wellness.health9")
}
}
standardTile("temperature", "device.temperature", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", label:'${currentValue}°', icon:"st.Weather.weather12"
}
valueTile("battery", "device.battery", decoration: "flat", inactiveLabel: false, width: 2, height: 2) {
state "default", label:'${currentValue}% battery', unit:""
}
//AlecM - 2017-07-04 Added width and height of 2 to the temperature 2 tile
valueTile("temperature2", "device.temperature", decoration: "flat", inactiveLabel: false, width: 2, height: 2) {
state "default", label:'${currentValue}°', icon: "st.Weather.weather2",
backgroundColors:[
[value: 0, color: "#153591"],
[value: 5, color: "#1e9cbb"],
[value: 10, color: "#90d2a7"],
[value: 15, color: "#44b621"],
[value: 20, color: "#f1d801"],
[value: 25, color: "#d04e00"],
[value: 30, color: "#bc2323"],
[value: 44, color: "#1e9cbb"],
[value: 59, color: "#90d2a7"],
[value: 74, color: "#44b621"],
[value: 84, color: "#f1d801"],
[value: 95, color: "#d04e00"],
[value: 96, color: "#bc2323"]
]
}
standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", action:"refresh.refresh", icon:"st.secondary.refresh"
}
//AlecM 2017-07-04 changed main tile to humidity - use temperature2
main(["humidity"])
details(["humidity", "battery", "temperature2","refresh"])
}
}
// Parse incoming device messages to generate events
def parse(String description) {
log.debug "RAW: $description"
def name = parseName(description)
log.debug "Parsename: $name"
def value = parseValue(description)
log.debug "Parsevalue: $value"
def unit = name == "temperature" ? getTemperatureScale() : (name == "humidity" ? "%" : null)
def result = createEvent(name: name, value: value, unit: unit)
log.debug "Evencreated: $name, $value, $unit"
log.debug "Parse returned ${result?.descriptionText}"
def now = new Date().format("yyyy MMM dd EEE h:mm:ss a", location.timeZone)
sendEvent(name: "lastCheckin", value: now)
return result
}
private String parseName(String description) {
if (description?.startsWith("temperature: ")) {
return "temperature"
} else if (description?.startsWith("humidity: ")) {
return "humidity"
} else if (description?.startsWith("catchall: ")) {
return "battery"
}
null
}
private String parseValue(String description) {
if (description?.startsWith("temperature: ")) {
def value = ((description - "temperature: ").trim()) as Float
if (getTemperatureScale() == "C") {
if (tempOffset) {
return (Math.round(value * 10))/ 10 + tempOffset as Float
} else {
return (Math.round(value * 10))/ 10 as Float
}
} else {
if (tempOffset) {
return (Math.round(value * 90/5))/10 + 32 + offset as Float
} else {
return (Math.round(value * 90/5))/10 + 32 as Float
}
}
} else if (description?.startsWith("humidity: ")) {
def pct = (description - "humidity: " - "%").trim()
if (pct.isNumber()) {
return Math.round(new BigDecimal(pct)).toString()
}
} else if (description?.startsWith("catchall: ")) {
return parseCatchAllMessage(description)
} else {
log.debug "unknown: $description"
sendEvent(name: "unknown", value: description)
}
null
}
private String parseCatchAllMessage(String description) {
def result = '--'
def cluster = zigbee.parse(description)
log.debug cluster
if (cluster) {
switch(cluster.clusterId) {
case 0x0000:
result = getBatteryResult(cluster.data.get(6))
break
}
}
return result
}
private String getBatteryResult(rawValue) {
log.debug 'Battery'
def linkText = getLinkText(device)
log.debug rawValue
def result = '--'
def maxBatt = 100
def battLevel = Math.round(rawValue * 100 / 255)
if (battLevel > maxBatt) {
battLevel = maxBatt
}
return battLevel
}
def refresh() {
log.debug "refresh called"
def refreshCmds = [
"st rattr 0x${device.deviceNetworkId} 1 1 0x00", "delay 2000",
"st rattr 0x${device.deviceNetworkId} 1 1 0x20", "delay 2000"
]
return refreshCmds + enrollResponse()
}
def configure() {
// Device-Watch allows 2 check-in misses from device + ping (plus 1 min lag time)
// enrolls with default periodic reporting until newer 5 min interval is confirmed
sendEvent(name: "checkInterval", value: 2 * 60 * 60 + 1 * 60, displayed: false, data: [protocol: "zigbee", hubHardwareId: device.hub.hardwareID])
// temperature minReportTime 30 seconds, maxReportTime 5 min. Reporting interval if no activity
// battery minReport 30 seconds, maxReportTime 6 hrs by default
return refresh() + zigbee.batteryConfig() + zigbee.temperatureConfig(30, 900) // send refresh cmds as part of config
}
def enrollResponse() {
log.debug "Sending enroll response"
String zigbeeEui = swapEndianHex(device.hub.zigbeeEui)
[
//Resending the CIE in case the enroll request is sent before CIE is written
"zcl global write 0x500 0x10 0xf0 {${zigbeeEui}}", "delay 200",
"send 0x${device.deviceNetworkId} 1 ${endpointId}", "delay 2000",
//Enroll Response
"raw 0x500 {01 23 00 00 00}", "delay 200",
"send 0x${device.deviceNetworkId} 1 1", "delay 2000"
]
}
private String swapEndianHex(String hex) {
reverseArray(hex.decodeHex()).encodeHex()
}
private byte[] reverseArray(byte[] array) {
int i = 0;
int j = array.length - 1;
byte tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
return array
}