Update Switches.groovy
[smartapps.git] / third-party / Securify-KeyFob.groovy
1 /**
2  *  Securify Key Fob
3  *
4  *      Author: Kevin Tierney based on code from Gilbert Chan; numButtons added by obycode
5  *      Date Created: 2014-12-18
6  *  Last Updated: 2015-05-13
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9  *  in compliance with the License. You may obtain a copy of the License at:
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
14  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
15  *  for the specific language governing permissions and limitations under the License.
16  *
17  */
18
19 metadata {
20         definition (name: "Securifi Key Fob", namespace: "tierneykev", author: "Kevin Tierney") {
21
22                 capability "Configuration"
23                 capability "Refresh"
24                 capability "Button"
25
26                 attribute "button2","ENUM",["released","pressed"]
27                 attribute "button3","ENUM",["released","pressed"]
28                 attribute "numButtons", "STRING"
29
30                 fingerprint profileId: "0104", deviceId: "0401", inClusters: "0000,0003,0500", outClusters: "0003,0501"
31         }
32
33         tiles {
34
35                 standardTile("button1", "device.button", width: 1, height: 1) {
36                         state("released", label:'${name}', icon:"st.button.button.released", backgroundColor:"#ffa81e")
37                         state("pressed", label:'${name}', icon:"st.button.button.pressed", backgroundColor:"#79b821")
38                 }
39
40                 standardTile("button2", "device.button2", width: 1, height: 1) {
41                         state("released", label:'${name}', icon:"st.button.button.released", backgroundColor:"#ffa81e")
42                         state("pressed", label:'${name}', icon:"st.button.button.pressed", backgroundColor:"#79b821")
43                 }
44
45                 standardTile("button3", "device.button3", width: 1, height: 1) {
46                         state("released", label:'${name}', icon:"st.button.button.released", backgroundColor:"#ffa81e")
47                         state("pressed", label:'${name}', icon:"st.button.button.pressed", backgroundColor:"#79b821")
48                 }
49
50                 main (["button3", "button2", "button1"])
51                 details (["button3", "button2", "button1"])
52         }
53 }
54
55
56
57
58 def parse(String description) {
59
60         if (description?.startsWith('enroll request')) {
61
62                 List cmds = enrollResponse()
63                 log.debug "enroll response: ${cmds}"
64                 def result = cmds?.collect { new physicalgraph.device.HubAction(it) }
65                 return result
66         }
67         else if (description?.startsWith('catchall:')) {
68                 def msg = zigbee.parse(description)
69                 log.debug msg
70                 buttonPush(msg.data[0])
71         }
72         else {
73                 log.debug "parse description: $description"
74         }
75
76 }
77
78 def buttonPush(button){
79         def name = null
80         if (button == 0) {
81                 name = "1"
82                 def currentST = device.currentState("button")?.value
83                 log.debug "Unlock button Pushed"
84         }
85         else if (button == 2) {
86                 name = "2"
87                 def currentST = device.currentState("button2")?.value
88                 log.debug "Home button pushed"
89         }
90         else if (button == 3) {
91                 name = "3"
92                 def currentST = device.currentState("button3")?.value
93                 log.debug "Lock Button pushed"
94         }
95
96         def result = createEvent(name: "button", value: "pushed", data: [buttonNumber: name], descriptionText: "$device.displayName button $name was pushed", isStateChange: true)
97         log.debug "Parse returned ${result?.descriptionText}"
98         return result
99 }
100
101
102 def enrollResponse() {
103         log.debug "Sending enroll response"
104         [
105         "raw 0x500 {01 23 00 00 00}", "delay 200",
106         "send 0x${device.deviceNetworkId} 0x08 1"
107         ]
108 }
109
110
111
112 def configure(){
113         log.debug "Config Called"
114
115         // Set the number of buttons to 3
116         sendEvent(name: "numButtons", value: "3", displayed: false)
117
118         def configCmds = [
119         "zcl global write 0x500 0x10 0xf0 {${device.zigbeeId}}", "delay 200",
120         "send 0x${device.deviceNetworkId} 0x08 1", "delay 1500",
121         "zdo bind 0x${device.deviceNetworkId} 0x08 0x01 0x0501 {${device.zigbeeId}} {}", "delay 500",
122         "zdo bind 0x${device.deviceNetworkId} 0x08 1 1 {${device.zigbeeId}} {}"
123         ]
124         return configCmds
125 }