Advertisement
erocm123

Untitled

Oct 26th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 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. results += configurationCmds().collect{ response(it) }
  78. results << response(zwave.wakeUpV1.wakeUpNoMoreInformation().format())
  79. return results
  80. }
  81.  
  82. def buttonEvent(button, held) {
  83. button = button as Integer
  84. def child
  85. Integer buttons
  86.  
  87. if (device.currentState("numberOfButtons")) {
  88. buttons = (device.currentState("numberOfButtons").value).toBigInteger()
  89. }
  90. else {
  91. def zwMap = getZwaveInfo()
  92. buttons = 4 // Default for Key Fob
  93.  
  94. // Only one button for Aeon Panic Button
  95. if (zwMap && zwMap.mfr == "0086" && zwMap.prod == "0001" && zwMap.model == "0026") {
  96. buttons = 1
  97. }
  98. sendEvent(name: "numberOfButtons", value: buttons)
  99. }
  100.  
  101. if(buttons > 1)
  102. {
  103. String childDni = "${device.deviceNetworkId}/${button}"
  104. child = childDevices.find{it.deviceNetworkId == childDni}
  105. if (!child) {
  106. log.error "Child device $childDni not found"
  107. }
  108. }
  109.  
  110. if (held) {
  111. if(buttons > 1) {
  112. child?.sendEvent(name: "held", value: button, descriptionText: "$child.displayName button $button was held", isStateChange: true)
  113. }
  114. createEvent(name: "held", value: button, descriptionText: "$device.displayName button $button was held", isStateChange: true)
  115. } else {
  116. if(buttons > 1) {
  117. child?.sendEvent(name: "pushed", value: button, descriptionText: "$child.displayName button $button was pushed", isStateChange: true)
  118. }
  119. createEvent(name: "pushed", value: button, descriptionText: "$device.displayName button $button was pushed", isStateChange: true)
  120. }
  121. }
  122.  
  123. def zwaveEvent(hubitat.zwave.commands.sceneactivationv1.SceneActivationSet cmd) {
  124. Integer button = ((cmd.sceneId + 1) / 2) as Integer
  125. Boolean held = !(cmd.sceneId % 2)
  126. buttonEvent(button, held)
  127. }
  128.  
  129. def zwaveEvent(hubitat.zwave.commands.batteryv1.BatteryReport cmd) {
  130. def map = [ name: "battery", unit: "%" ]
  131. if (cmd.batteryLevel == 0xFF) {
  132. map.value = 1
  133. map.descriptionText = "${device.displayName} has a low battery"
  134. } else {
  135. map.value = cmd.batteryLevel
  136. }
  137. createEvent(map)
  138. }
  139.  
  140. def zwaveEvent(hubitat.zwave.Command cmd) {
  141. [ descriptionText: "$device.displayName: $cmd", linkText:device.displayName, displayed: false ]
  142. }
  143.  
  144. def configurationCmds() {
  145. [ zwave.configurationV1.configurationSet(parameterNumber: 250, scaledConfigurationValue: 1).format(),
  146. zwave.associationV1.associationSet(groupingIdentifier:1, nodeId:zwaveHubNodeId).format() ]
  147. }
  148.  
  149. def configure() {
  150. def cmd = configurationCmds()
  151. log.debug("Sending configuration: $cmd")
  152. return cmd
  153. }
  154.  
  155.  
  156. def installed() {
  157. initialize()
  158. Integer buttons = (device.currentState("numberOfButtons").value).toBigInteger()
  159. if(buttons > 1) {
  160. createChildDevices()
  161. }
  162. }
  163.  
  164. def updated() {
  165. initialize()
  166. Integer buttons = (device.currentState("numberOfButtons").value).toBigInteger()
  167. if(buttons > 1)
  168. {
  169. if (!childDevices) {
  170. createChildDevices()
  171. }
  172. else if (device.label != state.oldLabel) {
  173. childDevices.each {
  174. def segs = it.deviceNetworkId.split("/")
  175. def newLabel = "${device.displayName} button ${segs[-1]}"
  176. it.setLabel(newLabel)
  177. }
  178. state.oldLabel = device.label
  179. }
  180. }
  181. }
  182.  
  183. def initialize() {
  184. // Device only goes OFFLINE when Hub is off
  185. sendEvent(name: "DeviceWatch-Enroll", value: JsonOutput.toJson([protocol: "zwave", scheme:"untracked"]), displayed: false)
  186. def zwMap = getZwaveInfo()
  187. def buttons = 4 // Default for Key Fob
  188.  
  189. // Only one button for Aeon Panic Button
  190. if (zwMap && zwMap.mfr == "0086" && zwMap.prod == "0001" && zwMap.model == "0026") {
  191. buttons = 1
  192. }
  193. sendEvent(name: "numberOfButtons", value: buttons)
  194. }
  195.  
  196. private void createChildDevices() {
  197. state.oldLabel = device.label
  198. Integer buttons = (device.currentState("numberOfButtons").value).toBigInteger()
  199. for (i in 1..buttons) {
  200. addChildDevice("Child Button", "${device.deviceNetworkId}/${i}",
  201. [completedSetup: true, label: "${device.displayName} button ${i}",
  202. isComponent: true, componentName: "button$i", componentLabel: "Button $i"])
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement