Advertisement
erocm123

Untitled

Nov 17th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. import groovy.json.JsonOutput
  2. /**
  3. * Copyright 2015 SmartThings
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  6. * in compliance with the License. You may obtain a copy of the License at:
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing permissions and limitations under the License.
  13. *
  14. */
  15. metadata {
  16. definition (name: "Aeon Key Fob", namespace: "smartthings", author: "SmartThings", runLocally: true, minHubCoreVersion: '000.017.0012', executeCommandsLocally: false) {
  17. capability "Actuator"
  18. capability "PushableButton"
  19. capability "HoldableButton"
  20. capability "Configuration"
  21. capability "Sensor"
  22. capability "Battery"
  23. capability "Health Check"
  24.  
  25. fingerprint deviceId: "0x0101", inClusters: "0x86,0x72,0x70,0x80,0x84,0x85"
  26. fingerprint mfr: "0086", prod: "0101", model: "0058", deviceJoinName: "Aeotec Key Fob"
  27. fingerprint mfr: "0086", prod: "0001", model: "0026", deviceJoinName: "Aeotec Panic Button"
  28. }
  29.  
  30. simulator {
  31. status "button 1 pushed": "command: 2001, payload: 01"
  32. status "button 1 held": "command: 2001, payload: 15"
  33. status "button 2 pushed": "command: 2001, payload: 29"
  34. status "button 2 held": "command: 2001, payload: 3D"
  35. status "button 3 pushed": "command: 2001, payload: 51"
  36. status "button 3 held": "command: 2001, payload: 65"
  37. status "button 4 pushed": "command: 2001, payload: 79"
  38. status "button 4 held": "command: 2001, payload: 8D"
  39. status "wakeup": "command: 8407, payload: "
  40. }
  41. tiles {
  42.  
  43. multiAttributeTile(name: "rich-control", type: "generic", width: 6, height: 4, canChangeIcon: true) {
  44. tileAttribute("device.button", key: "PRIMARY_CONTROL") {
  45. attributeState "default", label: ' ', action: "", icon: "st.unknown.zwave.remote-controller", backgroundColor: "#ffffff"
  46. }
  47. }
  48. standardTile("battery", "device.battery", inactiveLabel: false, width: 6, height: 2) {
  49. state "battery", label:'${currentValue}% battery', unit:""
  50. }
  51.  
  52. }
  53.  
  54. }
  55.  
  56.  
  57. def parse(String description) {
  58. def results = []
  59. if (description.startsWith("Err")) {
  60. results = createEvent(descriptionText:description, displayed:true)
  61. } else {
  62. def cmd = zwave.parse(description, [0x2B: 1, 0x80: 1, 0x84: 1])
  63. if(cmd) results += zwaveEvent(cmd)
  64. if(!results) results = [ descriptionText: cmd, displayed: false ]
  65. }
  66. // log.debug("Parsed '$description' to $results")
  67. return results
  68. }
  69.  
  70. def zwaveEvent(hubitat.zwave.commands.wakeupv1.WakeUpNotification cmd) {
  71. def results = [createEvent(descriptionText: "$device.displayName woke up", isStateChange: false)]
  72.  
  73. def prevBattery = device.currentState("battery")
  74. if (!prevBattery || (new Date().time - prevBattery.date.time)/60000 >= 60 * 53) {
  75. results << response(zwave.batteryV1.batteryGet().format())
  76. }
  77. if (!state.msr) results << response(zwave.manufacturerSpecificV2.manufacturerSpecificGet().format())
  78. results += configurationCmds().collect{ response(it) }
  79. results << response(zwave.wakeUpV1.wakeUpNoMoreInformation().format())
  80. return results
  81. }
  82.  
  83. def zwaveEvent(hubitat.zwave.commands.manufacturerspecificv2.ManufacturerSpecificReport cmd) {
  84. def result = []
  85.  
  86. state.msr = String.format("%04X-%04X-%04X", cmd.manufacturerId, cmd.productTypeId, cmd.productId)
  87. log.debug "msr: $msr"
  88.  
  89. result << createEvent(descriptionText: "$device.displayName MSR: $msr", isStateChange: false)
  90. result
  91. }
  92.  
  93. def buttonEvent(button, held) {
  94. button = button as Integer
  95. def child
  96. Integer buttons
  97.  
  98. if (device.currentState("numberOfButtons") != null) {
  99. buttons = (device.currentState("numberOfButtons").value).toBigInteger()
  100. }
  101. else {
  102. buttons = 1 // Default for Key Fob
  103.  
  104. // Only one button for Aeon Panic Button
  105. if (state.msr && state.msr != "0086-0001-0026") {
  106. buttons = 4
  107. }
  108. sendEvent(name: "numberOfButtons", value: buttons)
  109. }
  110.  
  111. if(buttons > 1)
  112. {
  113. String childDni = "${device.deviceNetworkId}/${button}"
  114. child = childDevices.find{it.deviceNetworkId == childDni}
  115. if (!child) {
  116. log.error "Child device $childDni not found"
  117. }
  118. }
  119.  
  120. if (held) {
  121. if(buttons > 1) {
  122. child?.sendEvent(name: "held", value: button, descriptionText: "$child.displayName button $button was held", isStateChange: true)
  123. }
  124. createEvent(name: "held", value: button, descriptionText: "$device.displayName button $button was held", isStateChange: true)
  125. } else {
  126. if(buttons > 1) {
  127. child?.sendEvent(name: "pushed", value: button, descriptionText: "$child.displayName button $button was pushed", isStateChange: true)
  128. }
  129. createEvent(name: "pushed", value: button, descriptionText: "$device.displayName button $button was pushed", isStateChange: true)
  130. }
  131. }
  132.  
  133. def zwaveEvent(hubitat.zwave.commands.sceneactivationv1.SceneActivationSet cmd) {
  134. Integer button = ((cmd.sceneId + 1) / 2) as Integer
  135. Boolean held = !(cmd.sceneId % 2)
  136. buttonEvent(button, held)
  137. }
  138.  
  139. def zwaveEvent(hubitat.zwave.commands.batteryv1.BatteryReport cmd) {
  140. def map = [ name: "battery", unit: "%" ]
  141. if (cmd.batteryLevel == 0xFF) {
  142. map.value = 1
  143. map.descriptionText = "${device.displayName} has a low battery"
  144. } else {
  145. map.value = cmd.batteryLevel
  146. }
  147. createEvent(map)
  148. }
  149.  
  150. def zwaveEvent(hubitat.zwave.Command cmd) {
  151. [ descriptionText: "$device.displayName: $cmd", linkText:device.displayName, displayed: false ]
  152. }
  153.  
  154. def configurationCmds() {
  155. [ zwave.configurationV1.configurationSet(parameterNumber: 250, scaledConfigurationValue: 1).format(),
  156. zwave.associationV1.associationSet(groupingIdentifier:1, nodeId:zwaveHubNodeId).format() ]
  157. }
  158.  
  159. def configure() {
  160. def cmd = configurationCmds()
  161. log.debug("Sending configuration: $cmd")
  162. return cmd
  163. }
  164.  
  165.  
  166. def installed() {
  167. initialize()
  168. Integer buttons = (device.currentState("numberOfButtons").value).toBigInteger()
  169. if(buttons > 1) {
  170. createChildDevices()
  171. }
  172. }
  173.  
  174. def updated() {
  175. initialize()
  176. Integer buttons = (device.currentState("numberOfButtons").value).toBigInteger()
  177.  
  178. if(buttons > 1)
  179. {
  180. if (!childDevices) {
  181. createChildDevices()
  182. }
  183. else if (device.label != state.oldLabel) {
  184. childDevices.each {
  185. def segs = it.deviceNetworkId.split("/")
  186. def newLabel = "${device.displayName} button ${segs[-1]}"
  187. it.setLabel(newLabel)
  188. }
  189. state.oldLabel = device.label
  190. }
  191. }
  192. if (!state.msr) results << response(zwave.manufacturerSpecificV2.manufacturerSpecificGet().format())
  193. }
  194.  
  195. def initialize() {
  196. def buttons = 1
  197.  
  198. if (state.msr && state.msr != "0086-0001-0026") {
  199. buttons = 4
  200. }
  201. sendEvent(name: "numberOfButtons", value: buttons)
  202. }
  203.  
  204. private void createChildDevices() {
  205. state.oldLabel = device.label
  206. Integer buttons = (device.currentState("numberOfButtons").value).toBigInteger()
  207. for (i in 1..buttons) {
  208. addChildDevice("Child Button", "${device.deviceNetworkId}/${i}",
  209. [completedSetup: true, label: "${device.displayName} button ${i}",
  210. isComponent: true, componentName: "button$i", componentLabel: "Button $i"])
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement