forked from dacmanj/hubitat-moenflo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoenflodetector.groovy
287 lines (259 loc) · 10.7 KB
/
moenflodetector.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Moen Flo Water Detector for Hubitat
* Based on Moen Flo for Hubitat by David Manuel https://github.com/dacmanj/hubitat-moenflo
* Licensed under CC BY 4.0 see https://creativecommons.org/licenses/by/4.0
* 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.
*
* 2020-09-17 v0.0.1 - Modified v0.1.07-alpha to support Moen Flo Water Detectors - Jeffrey Laughter
*
*/
metadata {
definition (name: "Moen Flo Water Detector", namespace: "jlaughter", author: "Jeffrey Laughter") {
capability "RelativeHumidityMeasurement"
capability "TemperatureMeasurement"
capability "WaterSensor"
capability "Battery"
command "logout"
command "pollMoen"
attribute "temperature", "number"
attribute "humidity", "number"
attribute "battery", "number"
attribute "water", "enum", ["wet", "dry"]
attribute "updated", "string"
attribute "rssi", "number"
attribute "ssid", "string"
attribute "lastEvent", "string"
attribute "lastEventDetail", "string"
attribute "lastEventDateTime", "string"
}
preferences {
input(name: "username", type: "string", title:"User Name", description: "Enter Moen Flo User Name", required: true, displayDuringSetup: true)
input(name: "password", type: "password", title:"Password", description: "Enter Moen Flo Password (to set or change it)", displayDuringSetup: true)
input(name: "mac_address", type: "string", title:"Device Id", description: "Enter Device Id from MeetFlo.com (if you have multiple devices)", required: false, displayDuringSetup: true)
input(name: "revert_mode", type: "enum", title: "Revert Mode (after Sleep)", options: ["home","away","sleep"], defaultValue: "home")
input(name: "polling_interval", type: "number", title: "Polling Interval (in Minutes)", range: 5..59, defaultValue: "10")
input(name: "revert_minutes", type: "number", title: "Revert Time in Minutes (after Sleep)", defaultValue: 120)
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
}
}
def logsOff(){
device.updateSetting("logEnable", false)
log.warn "Debug Logging Disabled..."
}
def logout() {
state.clear()
unschedule()
device.updateDataValue("token","")
device.updateDataValue("device_id","")
device.updateDataValue("location_id","")
device.updateDataValue("user_id","")
device.updateDataValue("encryptedPassword", "")
device.removeSetting("username")
device.removeSetting("mac_address")
}
def updated() {
configure()
if (state.configured) pollMoen()
if (logEnable) runIn(1800,logsOff)
}
def installed() {
runIn(1800,logsOff)
}
def unschedulePolling() {
unschedule(pollMoen)
}
def schedulePolling() {
unschedule(pollMoen)
def pw = device.getDataValue("encryptedPassword")
if (polling_interval != "None" && pw && pw != "") {
schedule("0 0/${polling_interval} * 1/1 * ? *", pollMoen)
}
}
def pollMoen() {
if (logEnable) log.debug("Polling Moen")
getDeviceInfo()
getLastAlerts()
}
def getUserInfo() {
def user_id = device.getDataValue("user_id")
if (mac_address) { log.debug "Getting device id for: ${mac_address}"}
else { log.debug "Defaulting to first device found." }
def uri = "https://api-gw.meetflo.com/api/v2/users/${user_id}?expand=locations,alarmSettings"
def response = make_authenticated_get(uri, "Get User Info")
device.updateDataValue("location_id", response.data.locations[0].id)
response.data.locations[0].devices.each {
if(it.macAddress == mac_address || !mac_address || mac_address == "") {
device.updateDataValue("device_id", it.id)
device.updateSetting("mac_address", it.macAddress)
if(logEnable) log.debug "Found device id: ${it.id}"
}
}
}
def getDeviceInfo() {
def device_id = device.getDataValue("device_id")
if (!device_id || device_id == "") {
log.debug "Cannot complete device info request: No Device Id"
} else {
def uri = "https://api-gw.meetflo.com/api/v2/devices/${device_id}"
def response = make_authenticated_get(uri, "Get Device")
def data = response.data
sendEvent(name: "temperature", value: round(data?.telemetry?.current?.tempF, 0), unit: "F")
sendEvent(name: "humidity", value: round(data?.telemetry?.current?.humidity, 0), unit: "%")
sendEvent(name: "battery", value: round(data?.battery?.level, 0), unit: "%")
def water_state = data?.fwProperties?.telemetry_water
def WATER_STATES = [1: "wet", 2: "dry"]
if (water_state) {
sendEvent(name: "water", value: WATER_STATES[1])}
else { sendEvent(name: "water", value: WATER_STATES[2])}
sendEvent(name: "updated", value: data?.telemetry?.current?.updated)
sendEvent(name: "rssi", value: data?.fwProperties?.telemetry_rssi)
sendEvent(name: "ssid", value: data?.fwProperties?.wifi_sta_ssid)
}
}
def getLastAlerts() {
def device_id = device.getDataValue("device_id")
if (!device_id || device_id == "") {
log.debug "Cannot fetch alerts: No Device Id"
} else {
def uri = "https://api-gw.meetflo.com/api/v2/alerts?isInternalAlarm=false&deviceId=${device_id}"
def response = make_authenticated_get(uri, "Get Alerts")
def data = response.data.items
if (data) {
sendEvent(name: "lastEvent", value: data[0]?.displayTitle)
sendEvent(name: "lastEventDetail", value: data[0].displayMessage)
sendEvent(name: "lastEventDateTime", value: data[0].createAt)
}
}
}
def round(d, places = 2) {
try { return (d as double).round(places) }
catch (Exception e) { return (null) }
}
def make_authenticated_get(uri, request_type, success_status = [200, 202]) {
def token = device.getDataValue("token")
if (!token || token == "") login();
def response = [:];
int max_tries = 2;
int tries = 0;
while (!response?.status && tries < max_tries) {
def headers = [:]
headers.put("Content-Type", "application/json")
headers.put("Authorization", device.getDataValue("token"))
try {
httpGet([headers: headers, uri: uri]) { resp -> def msg = ""
if (logEnable) log.debug("${request_type} Received Response Code: ${resp?.status}")
if (resp?.status in success_status) {
response = resp;
}
else {
log.debug "${request_type} Failed (${response.status}): ${response.data}"
login()
}
}
}
catch (Exception e) {
log.debug "${request_type} Exception: ${e}"
if (e.getMessage().contains("Forbidden") || e.getMessage().contains("Unauthorized")) {
log.debug "Refreshing token..."
login()
}
}
tries++
}
return response
}
def make_authenticated_post(uri, body, request_type, success_status = [200, 202]) {
def token = device.getDataValue("token")
if (!token || token == "") login();
def response = [:];
int max_tries = 2;
int tries = 0;
while (!response?.status && tries < max_tries) {
def headers = [:]
headers.put("Content-Type", "application/json")
headers.put("Authorization", device.getDataValue("token"))
try {
httpPostJson([headers: headers, uri: uri, body: body]) { resp -> def msg = ""
if (logEnable) log.debug("${request_type} Received Response Code: ${resp?.status}")
if (resp?.status in success_status) {
response = resp;
}
else {
log.debug "${request_type} Failed (${resp.status}): ${resp.data}"
}
}
}
catch (Exception e) {
log.debug "${request_type} Exception: ${e}"
if (e.getMessage().contains("Forbidden") || e.getMessage().contains("Unauthorized")) {
log.debug "Refreshing token..."
login()
}
}
tries++
}
return response
}
def configure() {
def token = device.getDataValue("token")
if (password && password != "") {
device.updateDataValue("encryptedPassword", encrypt(password))
device.removeSetting("password")
if (!mac_address || mac_address == "" || !device.getDataValue("device_id") || !device.getDataValue("device_id") == "") {
state.configured = false
}
login()
} else if (!token || token == "") {
log.debug "Unable to configure -- invalid login"
return
}
if (isConfigurationValid()) {
schedulePolling()
state.configured = true
}
}
def isNotBlank(obj) {
return obj && obj != ""
}
def isConfigurationValid() {
def token = device.getDataValue("token")
def device_id = device.getDataValue("device_id")
def location_id = device.getDataValue("location_id")
def pw = device.getDataValue("encryptedPassword")
def user = device.getDataValue("user_id")
return isNotBlank(token) && isNotBlank(device_id) &&
isNotBlank(location_id) && isNotBlank(pw) && isNotBlank(user)
}
def login() {
def uri = "https://api.meetflo.com/api/v1/users/auth"
def pw = decrypt(device.getDataValue("encryptedPassword"))
if (!pw || pw == "") {
log.debug("Login Failed: No password")
} else {
def body = [username:username, password:pw]
def headers = [:]
headers.put("Content-Type", "application/json")
try {
httpPostJson([headers: headers, uri: uri, body: body]) { response -> def msg = response?.status
if (logEnable) log.debug("Login received response code ${response?.status}")
if (response?.status == 200) {
msg = "Success"
device.updateDataValue("token",response.data.token)
device.updateDataValue("user_id", response.data.tokenPayload.user.user_id)
if (!state.configured) { getUserInfo() }
}
else {
log.debug "Login Failed: (${response.status}) ${response.data}"
state.configured = false
}
}
}
catch (Exception e) {
log.debug "Login exception: ${e}"
log.debug "Login Failed: Please confirm your Flo Credentials"
state.configured = false
}
}
}