forked from fison67/mi_connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_xiaomi_air_monitor.groovy
209 lines (179 loc) · 6.13 KB
/
old_xiaomi_air_monitor.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
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
/**
* Xiaomi Air Monitor (v.0.0.1)
*
* MIT License
*
* Copyright (c) 2018 fison67@nate.com
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import groovy.json.JsonSlurper
metadata {
definition (name: "Xiaomi Air Monitor", namespace: "fison67", author: "fison67") {
capability "Switch" //"on", "off"
attribute "switch", "string"
attribute "pm25", "string"
attribute "battery", "string"
attribute "usb_state", "string"
attribute "lastCheckin", "Date"
command "localOn"
command "localOff"
}
simulator {
}
tiles {
multiAttributeTile(name:"switch", type: "generic", width: 6, height: 4, canChangeIcon: true){
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
attributeState "on", label:'${name}', action:"localOff", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
attributeState "off", label:'${name}', action:"localOn", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
attributeState "turningOn", label:'${name}', action:"localOff", icon:"st.switches.light.on", backgroundColor:"#00a0dc", nextState:"turningOff"
attributeState "turningOff", label:'${name}', action:"localOn", icon:"st.switches.light.off", backgroundColor:"#ffffff", nextState:"turningOn"
}
tileAttribute("device.lastCheckin", key: "SECONDARY_CONTROL") {
attributeState("default", label:'Updated: ${currentValue}',icon: "st.Health & Wellness.health9")
}
}
valueTile("pm25", "device.pm25", width: 2, height: 2, unit: "") {
state("val", label:'${currentValue}', defaultState: true,
backgroundColors:[
[value: 31, color: "#153591"],
[value: 44, color: "#1e9cbb"],
[value: 59, color: "#90d2a7"],
[value: 74, color: "#44b621"],
[value: 84, color: "#f1d801"],
[value: 95, color: "#d04e00"],
[value: 96, color: "#bc2323"]
]
)
}
valueTile("battery", "device.battery", width: 2, height: 2) {
state("val", label:'${currentValue}', defaultState: true, backgroundColor:"#00a0dc")
}
valueTile("usb_state", "device.usb_state", width: 2, height: 2) {
state("val", label:'${currentValue}', defaultState: true, backgroundColor:"#00a0dc")
}
}
}
// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
}
def setInfo(String app_url, String id) {
log.debug "${app_url}, ${id}"
state.app_url = app_url
state.id = id
}
def setStatus(params){
log.debug "${params.key} : ${params.data}"
switch(params.key){
case "pm2.5":
sendEvent(name:"pm25", value: params.data + "㎍/㎥")
break;
case "aqi":
sendEvent(name:"pm25", value: params.data + "㎍/㎥")
break;
case "relativeHumidity":
sendEvent(name:"humidity", value: params.data + "%")
break;
case "power":
sendEvent(name:"switch", value: (params.data == "true" ? "on" : "off"))
break;
case "temperature":
sendEvent(name:"temperature", value: params.data)
break;
case "battery":
sendEvent(name:"battery", value: params.data)
break;
case "usb_state":
sendEvent(name:"usb_state", value: params.data)
break;
}
def now = new Date().format("yyyy-MM-dd HH:mm:ss", location.timeZone)
sendEvent(name: "lastCheckin", value: now)
}
def refresh(){
log.debug "Refresh"
def options = [
"method": "GET",
"path": "/get?id=${state.id}",
"headers": [
"HOST": state.app_url,
"Content-Type": "application/json"
]
]
sendCommand(options, callback)
}
def localOn(){
log.debug "Off >> ${state.id}"
def body = [
"id": state.id,
"cmd": "power",
"data": "on"
]
def options = makeCommand(body)
sendCommand(options, null)
}
def localOff(){
log.debug "Off >> ${state.id}"
def body = [
"id": state.id,
"cmd": "power",
"data": "off"
]
def options = makeCommand(body)
sendCommand(options, null)
}
def on(){
localOn()
}
def off(){
localOff()
}
def callback(physicalgraph.device.HubResponse hubResponse){
def msg
try {
msg = parseLanMessage(hubResponse.description)
def jsonObj = new JsonSlurper().parseText(msg.body)
log.debug jsonObj
// setStatus(jsonObj.state)
} catch (e) {
log.error "Exception caught while parsing data: "+e;
}
}
def updated() {
}
def sendCommand(options, _callback){
def myhubAction = new physicalgraph.device.HubAction(options, null, [callback: _callback])
sendHubCommand(myhubAction)
}
def makeCommand(body){
def options = [
"method": "POST",
"path": "/control",
"headers": [
"HOST": state.app_url,
"Content-Type": "application/json"
],
"body":body
]
return options
}