Update the-big-switch.groovy
[smartapps.git] / third-party / SmartenIt-ZBWS3B.groovy
1 /**
2  *  3 Button Remote Zigbee ZBWS3
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Original code by GilbertChan, modified by obycode to add "numButtons"
14  *  Thanks to Seth Jansen @sjansen for original contributions
15  */
16 metadata {
17   definition (name: "3 Button Remote (ZBWS3)", namespace: "thegilbertchan", author: "Gilbert Chan") {
18     capability "Button"
19     capability "Configuration"
20
21     attribute "button2","ENUM",["released","pressed"]
22     attribute "button3","ENUM",["released","pressed"]
23     attribute "numButtons", "STRING"
24
25     fingerprint endpointId: "03", profileId: "0104", deviceId: "0000", deviceVersion: "00", inClusters: "03 0000 0003 0007", outClusters: "01 0006"
26
27   }
28   // Contributors
29
30   // simulator metadata
31   simulator {
32   }
33
34   // UI tile definitions
35   tiles {
36
37     standardTile("button1", "device.button", width: 1, height: 1) {
38       state("released", label:'${name}', icon:"st.button.button.released", backgroundColor:"#ffa81e")
39       state("pressed", label:'${name}', icon:"st.button.button.pressed", backgroundColor:"#79b821")
40     }
41
42     standardTile("button2", "device.button2", width: 1, height: 1) {
43       state("released", label:'${name}', icon:"st.button.button.released", backgroundColor:"#ffa81e")
44       state("pressed", label:'${name}', icon:"st.button.button.pressed", backgroundColor:"#79b821")
45     }
46
47     standardTile("button3", "device.button3", width: 1, height: 1) {
48       state("released", label:'${name}', icon:"st.button.button.released", backgroundColor:"#ffa81e")
49       state("pressed", label:'${name}', icon:"st.button.button.pressed", backgroundColor:"#79b821")
50     }
51
52     main (["button3", "button2", "button1"])
53     details (["button3", "button2", "button1"])
54   }
55 }
56
57 // Parse incoming device messages to generate events
58 def parse(String description) {
59   log.debug "Parse description $description"
60   def name = null
61   def value = null
62   if (description?.startsWith("catchall: 0104 0006 01")) {
63     name = "1"
64     def currentST = device.currentState("button")?.value
65     log.debug "Button 1 pushed"
66
67   }
68   else if (description?.startsWith("catchall: 0104 0006 02")) {
69     name = "2"
70     def currentST = device.currentState("button2")?.value
71     log.debug "Button 2 pushed"
72
73   }
74   else if (description?.startsWith("catchall: 0104 0006 03")) {
75     name = "3"
76     def currentST = device.currentState("button3")?.value
77     log.debug "Button 3 pushed"
78   }
79
80   def result = createEvent(name: "button", value: "pushed", data: [buttonNumber: name], descriptionText: "$device.displayName button $name was pushed", isStateChange: true)
81   log.debug "Parse returned ${result?.descriptionText}"
82
83
84   return result
85 }
86
87
88 def parseDescriptionAsMap(description) {
89   (description - "read attr - ").split(",").inject([:]) { map, param ->
90     def nameAndValue = param.split(":")
91     map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
92   }
93 }
94
95 private getFPoint(String FPointHex){                   // Parsh out hex string from Value: 4089999a
96   Long i = Long.parseLong(FPointHex, 16)         // Convert Hex String to Long
97   Float f = Float.intBitsToFloat(i.intValue())   // Convert IEEE 754 Single-Precison floating point
98   log.debug "converted floating point value: ${f}"
99   def result = f
100
101   return result
102 }
103
104
105 // Commands to device
106
107 def configure() {
108   // Set the number of buttons to 3
109   updateState("numButtons", "3")
110
111   log.debug "Binding SEP 0x01 DEP 0x01 Cluster 0x0006 On/Off cluster to hub"
112   def configCmds = [
113
114   "zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0006 {${device.zigbeeId}} {}", "delay 500",
115   "zdo bind 0x${device.deviceNetworkId} 0x02 0x01 0x0006 {${device.zigbeeId}} {}", "delay 500",
116   "zdo bind 0x${device.deviceNetworkId} 0x03 0x01 0x0006 {${device.zigbeeId}} {}", "delay 1500",
117   ]
118   log.info "Sending ZigBee Bind commands to 3 Button Switch"
119
120   return configCmds
121 }
122
123 // Update State
124 // Store mode and settings
125 def updateState(String name, String value) {
126   state[name] = value
127   device.updateDataValue(name, value)
128 }