-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathfoobot.groovy
218 lines (177 loc) · 7.74 KB
/
foobot.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
210
211
212
213
214
215
216
217
218
/**
*
* V2. Updated 6/2/2017
* - Updated Region so it works in UK & US
*
*
*/
preferences {
input("username", "text", title: "Username", description: "Your Foobot username (usually an email address)")
//input("password", "password", title: "Password", description: "Your Foobot password")
input("uuid", "text", title: "UUID", description: "The UUID of the exact Foobot that you would like information for")
//input("APIKey", "text", title: "API Key", description: "The API Key that Foobot gave you")
def myOptions = ["EU", "US"]
input "region",
"enum",
title: "Select your region",
defaultValue: "EU",
required: true,
options: myOptions,
displayDuringSetup: true
}
metadata {
definition (name: "Foobot New API", namespace: "AdamV", author: "AdamV") {
capability "Polling"
capability "Refresh"
capability "Sensor"
capability "Thermostat"
capability "relativeHumidityMeasurement"
capability "temperatureMeasurement"
capability "carbonDioxideMeasurement"
attribute "pollution", "number"
attribute "particle", "number"
attribute "voc", "number"
attribute "GPIState", "String"
}
simulator {
// TODO: define status and reply messages here
}
tiles (scale: 2){
multiAttributeTile(name:"pollution", type:"generic", width:6, height:4) {
tileAttribute("device.pollution", key: "PRIMARY_CONTROL") {
attributeState("default", label:'${currentValue}% GPI', unit:"%", icon:"st.Weather.weather13", backgroundColors:[
[value: 24, color: "#1c71ff"],
[value: 49, color: "#5c93ee"],
[value: 74, color: "#ff4040"],
[value: 100, color: "#d62d20"]
])
}
// valueTile("pollution", "device.pollution", inactiveLabel: false, decoration: "flat") {
// state "default", label:'${currentValue}% GPI', unit:"%"
tileAttribute("device.GPIState", key: "SECONDARY_CONTROL") {
attributeState("default", label:'${currentValue}')
}
}
valueTile("co2", "device.co2", inactiveLabel: false, width: 2, height: 2, decoration: "flat") {
state "default", label:'${currentValue} CO2 ppm', unit:"ppm"
}
valueTile("voc", "device.voc", inactiveLabel: false, width: 2, height: 2, decoration: "flat") {
state "default", label:'${currentValue} VOC ppb', unit:"ppb"
}
valueTile("particle", "device.particle", inactiveLabel: false, width: 2, height: 2, decoration: "flat") {
state "default", label:'${currentValue} µg/m³', unit:"µg/m³ PM2.5"
}
valueTile("humidity", "device.humidity", inactiveLabel: false, width: 2, height: 2, decoration: "flat") {
state "default", label:'${currentValue}% humidty', unit:"%"
}
valueTile("temperature", "device.temperature", inactiveLabel: false, width: 2, height: 2, decoration: "flat") {
state "default", label:'${currentValue}°', unit:"°"
}
standardTile("refresh", "device.refresh", inactiveLabel: false, width: 2, height: 2, decoration: "flat") {
state "default", action:"refresh.refresh", icon:"st.secondary.refresh"
}
standardTile("spacerlastUpdatedLeft", "spacerTile", decoration: "flat", width: 1, height: 1) {
}
valueTile("lastUpdated", "device.lastUpdated", inactiveLabel: false, decoration: "flat", width: 4, height: 1) {
state "default", label:'Last updated:\n${currentValue}'
}
standardTile("spacerlastUpdatedRight", "spacerTile", decoration: "flat", width: 1, height: 1) {
}
main "pollution"
details(["pollution","co2","voc","particle","humidity", "temperature", "refresh", "spacerlastUpdatedLeft", "lastUpdated", "spacerlastUpdatedRight"])
}
}
private getAPIKey() {
return "ENTER YOUR API KEY HERE (KEEP THE QUOTATION MARKS)";
}
// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
}
private test(){
return "#ffa81e"
}
def refresh() {
poll()
}
// handle commands
def poll() {
def start = new Date(Calendar.instance.time.time-1800000).format("yyyy-MM-dd'T'HH:MM':00'");
def stop = new Date(Calendar.instance.time.time+1800000).format("yyyy-MM-dd'T'HH:MM':00'");
def accessToken = getAPIKey()
def regionVar = ""
def params = ""
if (region){
regionVar = region
if (regionVar == "EU"){
params = "https://api.foobot.io/v2/device/${settings.uuid}/datapoint/0/last/0/?api_key=${accessToken}"
}
if (regionVar == "US"){
params = "https://api-us-east-1.foobot.io/v2/device/${settings.uuid}/datapoint/0/last/0/?api_key=${accessToken}"
}
}
try {
httpGet(params) {resp ->
resp.headers.each {
log.debug "${it.name} : ${it.value}"
}
// get an array of all headers with the specified key
def theHeaders = resp.getHeaders("Content-Length")
// get the contentType of the response
log.debug "response contentType: ${resp.contentType}"
// get the status code of the response
log.debug "response status code: ${resp.status}"
// get the data from the response body
log.debug "response data: ${resp.data}"
log.debug "pm: ${resp.data.datapoints[-1][1]}"
sendEvent(name: "particle", value: sprintf("%.2f",resp.data.datapoints[-1][1]), unit: "µg/m³ PM2.5")
log.debug "tmp: ${resp.data.datapoints[-1][2]}"
BigDecimal tmp = resp.data.datapoints[-1][2]
def tmpround = String.format("%5.2f",tmp)
//def tmpround = tmp.round(2)
log.debug ("tmpround: $tmpround")
sendEvent(name: "temperature", value: tmpround, unit: "°C")
log.debug "hum: ${resp.data.datapoints[-1][3]}"
sendEvent(name: "humidity", value: resp.data.datapoints[-1][3] as Integer, unit: "%")
log.debug "co2: ${resp.data.datapoints[-1][4]}"
sendEvent(name: "co2", value: resp.data.datapoints[-1][4] as Integer, unit: "ppm")
log.debug "voc: ${resp.data.datapoints[-1][5]}"
sendEvent(name: "voc", value: resp.data.datapoints[-1][5] as Integer, unit: "ppb")
log.debug "allpollu: ${resp.data.datapoints[-1][6]}"
def allpollu = resp.data.datapoints[-1][6]
sendEvent(name: "pollution", value: resp.data.datapoints[-1][6] as Integer, unit: "%")
if (allpollu < 25){
sendEvent(name: "GPIState", value: "great", isStateChange: true)
}
else if (allpollu < 50){
sendEvent(name: "GPIState", value: "good", isStateChange: true)
}
else if (allpollu < 75){
sendEvent(name: "GPIState", value: "fair", isStateChange: true)
}
else if (allpollu > 75){
sendEvent(name: "GPIState", value: "poor", isStateChange: true)
}
def now = new Date().format("EEE, d MMM yyyy HH:mm:ss",location.timeZone)
sendEvent(name:"lastUpdated", value: now, displayed: false)
// def tmp = resp.data.datapoints[-1][2]
// if(getTemperatureScale() == "C") {
// tmp = Integer.tmp
// }
// else {
// tmp = celsiusToFahrenheit(tmp) as Integer
// sendEvent(name: "temperature", value: tmp, unit: "°F")
//}
}
} catch (e) {
log.error "error: $e"
}
//sendEvent(name: "GPIState", value: "poor", isStateChange: true)
def map = [:]
map.value = "great"
map.name = "thermostatOperatingState"
map
log.debug "Http Get params 1"
}
def getApiAuth() {
}