-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathchange-lock-code-multi-user.smartapp.groovy
157 lines (146 loc) · 5.55 KB
/
change-lock-code-multi-user.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
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
/**
* Change or Delete Lock Codes
*
* Author: bigpunk6
*/
definition(
name: "Change or Delete Door Lock Codes - Multi User",
namespace: "bigpunk6",
author: "bigpunk6",
description: "This app alows you to change or delete the user codes for your smart door locks",
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 {
page(name: "page1", title: "Select Locks and Users", nextPage: "page2", uninstall: true) {
section("What Locks") {
input "locks","capability.lockCodes", title: "Locks", multiple: true
}
section( "Notifications" ) {
input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required:false
input "phone", "phone", title: "Send a Text Message?", required: false
}
}
page(name: "page2", title: "Set user preferences", nextPage: "page3", uninstall: true) {
section("User 1") {
input "username1", "text", title: "Name for User", required:false
input "code1", "text", title: "Code (4 to 8 digits) or X to Delete", required:false
input "sendCode1", "enum", title: "Send notification when users code is used", metadata:[values:["Yes","No"]], required:false
}
section("User 2") {
input "username2", "text", title: "Name for User", required:false
input "code2", "text", title: "Code (4 to 8 digits) or X to Delete", required:false
input "sendCode2", "enum", title: "Send notification when users code is used", metadata:[values:["Yes","No"]], required:false
}
}
page(name: "page3", title: "Set user preferences", nextPage: "page4", uninstall: true) {
section("User 3") {
input "username3", "text", title: "Name for User", required:false
input "code3", "text", title: "Code (4 to 8 digits) or X to Delete", required:false
input "sendCode3", "enum", title: "Send notification when users code is used", metadata:[values:["Yes","No"]], required:false
}
section("User 4") {
input "username4", "text", title: "Name for User", required:false
input "code4", "text", title: "Code (4 to 8 digits) or X to Delete", required:false
input "sendCode4", "enum", title: "Send notification when users code is used", metadata:[values:["Yes","No"]], required:false
}
}
page(name: "page4", title: "Set user preferences", nextPage: "page4", install: true, uninstall: true) {
section("User 5") {
input "username5", "text", title: "Name for User", required:false
input "code5", "text", title: "Code (4 to 8 digits) or X to Delete", required:false
input "sendCode5", "enum", title: "Send notification when users code is used", metadata:[values:["Yes","No"]], required:false
}
section("User 6") {
input "username6", "text", title: "Name for User", required:false
input "code6", "text", title: "Code (4 to 8 digits) or X to Delete", required:false
input "sendCode6", "enum", title: "Send notification when users code is used", metadata:[values:["Yes","No"]], required:false
}
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
subscribe(locks, "codeReport", codereturn)
subscribe(locks, "lock", codeUsed)
checkUsers()
}
private checkUsers() {
for ( i in 1..30 ) {
updateCode(i)
}
}
def updateCode(userslot) {
def username
def code = null
settings.each {
if( it.key == "username${userslot}" ) {
username = "$it.value"
}
if ( it.key == "code${userslot}" ) {
code = "$it.value".toString()
}
}
if (code == null) {
log.info "no code set for slot $userslot"
} else {
if (code.equalsIgnoreCase("X")) {
log.info "Deleting user in slot $userslot"
locks.deleteCode(userslot)
} else {
log.info "Set code for $username in slot $userslot to $code"
locks.setCode(userslot, code)
}
}
}
def codereturn(evt) {
log.debug "codereturn"
def username = "$evt.value"
def code = evt.data.replaceAll("\\D+","")
settings.each {
if( it.key == "username${evt.value}" ) {
username = "$it.value"
}
}
if (code == "") {
def message = "User in slot $evt.value was deleted from $evt.displayName"
send(message)
} else {
def message = "Code for user $username in user slot $evt.value was set to $code on $evt.displayName"
send(message)
}
}
def codeUsed(evt) {
if(evt.value == "unlocked" && evt.data) {
def username
def sendCode
def codeData = new JsonSlurper().parseText(evt.data)
settings.each {
if( it.key == "username${codeData.usedCode}" ) {
username = "$it.value"
}
if ( it.key == "sendCode${codeData.usedCode}" ) {
sendCode = "$it.value"
}
}
def message = "$evt.displayName was unlocked by $username in user slot $codeData.usedCode"
if(sendCode == "Yes") {
send(message)
}
}
}
private send(msg) {
if (sendPushMessage == "Yes") {
sendPush(msg)
}
if (phone) {
sendSms(phone, msg)
}
log.info msg
}