forked from bigpunk6/smartapp.change-lock-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange-lock-code.smartapp.groovy
89 lines (78 loc) · 2.55 KB
/
change-lock-code.smartapp.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
/**
* Change or Delete Lock Codes + Notify
*
* Author: bigpunk6
*/
definition(
name: "Change or Delete Door Lock Codes - Notify",
namespace: "bigpunk6",
author: "bigpunk6",
description: "This app alows you to change or delete the user codes for your smart door lock",
category: "Safety & Security",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png")
import groovy.json.JsonSlurper
preferences {
section("What Lock") {
input "lock","capability.lock", title: "Lock"
}
section("User") {
input "username", "text", title: "Name for User"
input "user", "number", title: "User Slot (From 1 to 30) "
input "code", "text", title: "Code (4 to 8 digits) or X to Delete"
}
section( "Notifications" ) {
input "sendCode", "enum", title: "Send notification when users code is used", metadata:[values:["Yes","No"]], required:false
input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required:false
input "phone", "phone", title: "Send a Text Message?", required: false
}
}
def installed()
{
subscribe(app, appTouch)
subscribe(lock, "codeReport", codereturn)
subscribe(lock, "lock", codeUsed)
}
def updated()
{
unsubscribe()
subscribe(app, appTouch)
subscribe(lock, "codeReport", codereturn)
subscribe(lock, "lock", codeUsed)
}
def appTouch(evt) {
if (code.equalsIgnoreCase("X")) {
lock.deleteCode(user)
} else {
lock.setCode(user, code)
}
}
def codereturn(evt) {
def codenumber = evt.data.replaceAll("\\D+","");
if (evt.value == user) {
if (codenumber == "") {
def message = "User $username in user slot $evt.value code is not set or was deleted on $lock"
send(message)
} else {
def message = "Code for user $username in user slot $evt.value was set to $codenumber on $lock"
send(message)
}
}
}
def codeUsed(evt) {
if(evt.value == "unlocked" && evt.data) {
def codeData = new JsonSlurper().parseText(evt.data)
def message = "$lock was unlocked by $username in user slot $codeData.usedCode"
if(codeData.usedCode == user && sendCode == "Yes") {
send(message)
}
}
}
private send(msg) {
if (sendPushMessage == "Yes") {
sendPush(msg)
}
if (phone) {
sendSms(phone, msg)
}
}