Guest User

Untitled

a guest
Dec 29th, 2018
237
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Monoprice/Vision Dual Relay Parent Driver
  3. */
  4. metadata {
  5. definition (name: "Triple Relay Driver", namespace: "hubitat", author: "hubitat") {
  6. capability "Refresh"
  7. capability "Actuator"
  8.  
  9. command "childOn"
  10. command "childOff"
  11. command "recreateChildDevices"
  12. command "deleteChildren"
  13.  
  14. //fingerprint manufacturer: "015D", prod: "0651", model: "F51C", deviceJoinName: "Zooz ZEN20 Power Strip"
  15. //fingerprint deviceId: "0x1004", inClusters: "0x5E,0x85,0x59,0x5A,0x72,0x60,0x8E,0x73,0x27,0x25,0x86"
  16. }
  17. }
  18.  
  19. def installed() {
  20. log.debug "installed"
  21. createChildDevices()
  22. configure()
  23. }
  24.  
  25. def updated() {
  26. log.debug "updated"
  27.  
  28. if (!childDevices) {
  29. createChildDevices()
  30. }
  31. else if (device.label != state.oldLabel) {
  32. childDevices.each {
  33. def newLabel = "$device.displayName (CH${channelNumber(it.deviceNetworkId)})"
  34. it.setLabel(newLabel)
  35. }
  36.  
  37. state.oldLabel = device.label
  38. }
  39.  
  40. configure()
  41. }
  42.  
  43. def configure() {
  44. log.debug "configure"
  45. def cmds = [
  46. zwave.versionV1.versionGet().format(),
  47. zwave.manufacturerSpecificV2.manufacturerSpecificGet().format(),
  48. zwave.firmwareUpdateMdV2.firmwareMdGet().format()
  49. ]
  50. response(delayBetween(cmds, 1000))
  51. }
  52.  
  53. def refresh() {
  54. log.debug "refresh"
  55. def cmds = []
  56. cmds << zwave.basicV1.basicGet().format()
  57. cmds << zwave.switchBinaryV1.switchBinaryGet().format()
  58.  
  59. (1..3).each { endpoint ->
  60. cmds << encap(zwave.basicV1.basicGet(), endpoint)
  61. cmds << encap(zwave.switchBinaryV1.switchBinaryGet(), endpoint)
  62. }
  63.  
  64. delayBetween(cmds, 100)
  65. }
  66.  
  67. def recreateChildDevices() {
  68. log.debug "recreateChildDevices"
  69. deleteChildren()
  70. createChildDevices()
  71. }
  72.  
  73. def deleteChildren() {
  74. log.debug "deleteChildren"
  75. def children = getChildDevices()
  76.  
  77. children.each {child->
  78. deleteChildDevice(child.deviceNetworkId)
  79. }
  80. }
  81.  
  82.  
  83. def parse(String description) {
  84. log.debug "parse $description"
  85. def result = null
  86.  
  87. if (description.startsWith("Err")) {
  88. result = createEvent(descriptionText:description, isStateChange:true)
  89. } else {
  90. def cmd = zwave.parse(description, [0x60: 3, 0x25: 1, 0x20: 1])
  91. log.debug "Command: ${cmd}"
  92.  
  93. if (cmd) {
  94. result = zwaveEvent(cmd)
  95. log.debug "parsed '${description}' to ${result.inspect()}"
  96. } else {
  97. log.debug "Unparsed description $description"
  98. }
  99. }
  100.  
  101. result
  102. }
  103.  
  104.  
  105. def zwaveEvent(hubitat.zwave.commands.multichannelv3.MultiChannelCmdEncap cmd) {
  106. log.debug "multichannelv3.MultiChannelCmdEncap $cmd"
  107. def encapsulatedCommand = cmd.encapsulatedCommand([0x25: 1, 0x20: 1])
  108. log.debug "encapsulatedCommand: $encapsulatedCommand"
  109.  
  110. if (encapsulatedCommand) {
  111. return zwaveEvent(encapsulatedCommand, cmd.sourceEndPoint as Integer)
  112. } else {
  113. log.debug "Unable to get encapsulated command: $encapsulatedCommand"
  114. return []
  115. }
  116. }
  117.  
  118. def zwaveEvent(hubitat.zwave.commands.basicv1.BasicReport cmd, endpoint = null) {
  119. log.debug "basicv1.BasicReport $cmd, $endpoint"
  120. zwaveBinaryEvent(cmd, endpoint, "digital")
  121. }
  122.  
  123. def zwaveEvent(hubitat.zwave.commands.switchbinaryv1.SwitchBinaryReport cmd, endpoint = null) {
  124. log.debug "switchbinaryv1.SwitchBinaryReport $cmd, $endpoint"
  125. zwaveBinaryEvent(cmd, endpoint, "physical")
  126. }
  127.  
  128. def zwaveBinaryEvent(cmd, endpoint, type) {
  129. log.debug "zwaveBinaryEvent cmd $cmd, endpoint $endpoint, type $type"
  130. def childDevice = childDevices.find{it.deviceNetworkId.endsWith("$endpoint")}
  131. def result = null
  132.  
  133. if (childDevice) {
  134. log.debug "childDevice.sendEvent $cmd.value"
  135. childDevice.sendEvent(name: "switch", value: cmd.value ? "on" : "off", type: type)
  136. } else {
  137. result = createEvent(name: "switch", value: cmd.value ? "on" : "off", type: type)
  138. }
  139.  
  140. result
  141. }
  142.  
  143. def zwaveEvent(hubitat.zwave.commands.manufacturerspecificv2.ManufacturerSpecificReport cmd) {
  144. log.debug "manufacturerspecificv2.ManufacturerSpecificReport cmd $cmd"
  145. updateDataValue("MSR", String.format("%04X-%04X-%04X", cmd.manufacturerId, cmd.productTypeId, cmd.productId))
  146. }
  147.  
  148. def zwaveEvent(hubitat.zwave.commands.configurationv2.ConfigurationReport cmd) {
  149. log.debug "configurationv2.ConfigurationReport: parameter ${cmd.parameterNumber} with a byte size of ${cmd.size} is set to ${cmd.configurationValue}"
  150. }
  151.  
  152. def zwaveEvent(hubitat.zwave.commands.configurationv1.ConfigurationReport cmd) {
  153. log.debug "configurationv2.ConfigurationReport: parameter ${cmd.parameterNumber} with a byte size of ${cmd.size} is set to ${cmd.configurationValue}"
  154. }
  155.  
  156. def zwaveEvent(hubitat.zwave.commands.versionv1.VersionReport cmd, endpoint) {
  157. log.debug "versionv1.VersionReport, applicationVersion $cmd.applicationVersion, cmd $cmd, endpoint $endpoint"
  158. }
  159.  
  160. def zwaveEvent(hubitat.zwave.Command cmd, endpoint) {
  161. log.debug "${device.displayName}: Unhandled ${cmd}" + (endpoint ? " from endpoint $endpoint" : "")
  162. }
  163.  
  164. def zwaveEvent(hubitat.zwave.Command cmd) {
  165. log.debug "${device.displayName}: Unhandled ${cmd}"
  166. // My Code
  167. runIn( 1, refresh )
  168. ///My Code
  169. }
  170.  
  171. def on() {
  172. log.debug "on"
  173. def cmds = []
  174. cmds << zwave.basicV1.basicSet(value: 0xFF).format()
  175. cmds << zwave.basicV1.basicGet().format()
  176.  
  177. (1..3).each { endpoint ->
  178. cmds << encap(zwave.basicV1.basicSet(value: 0xFF), endpoint)
  179. cmds << encap(zwave.basicV1.basicGet(), endpoint)
  180. }
  181.  
  182. return delayBetween(cmds, 1000)
  183. }
  184.  
  185. def off() {
  186. log.debug "off"
  187. def cmds = []
  188. cmds << zwave.basicV1.basicSet(value: 0x00).format()
  189. cmds << zwave.basicV1.basicGet().format()
  190.  
  191. (1..3).each { endpoint ->
  192. cmds << encap(zwave.basicV1.basicSet(value: 0x00), endpoint)
  193. cmds << encap(zwave.basicV1.basicGet(), endpoint)
  194. }
  195.  
  196. return delayBetween(cmds, 1000)
  197. }
  198.  
  199. def childOn(String dni) {
  200. onOffCmd(0xFF, channelNumber(dni))
  201. }
  202.  
  203. def childOff(String dni) {
  204. onOffCmd(0, channelNumber(dni))
  205. }
  206.  
  207. private onOffCmd(value, endpoint) {
  208. log.debug "onOffCmd, value: $value, endpoint: $endpoint"
  209.  
  210. def cmds = []
  211. cmds << encap(zwave.basicV1.basicSet(value: value), endpoint)
  212. cmds << encap(zwave.basicV1.basicGet(), endpoint)
  213.  
  214. return delayBetween(cmds, 1000)
  215. }
  216.  
  217. private channelNumber(String dni) {
  218. def ch = dni.split("-")[-1] as Integer
  219. return ch
  220. }
  221.  
  222. private void createChildDevices() {
  223. log.debug "createChildDevices"
  224.  
  225. for (i in 1..3) {
  226. addChildDevice("hubitat", "Dual Relay Driver (Child)", "$device.deviceNetworkId-$i", [name: "ch$i", label: "$device.displayName $i", isComponent: true])
  227. }
  228. }
  229.  
  230. private encap(cmd, endpoint) {
  231. if (endpoint) {
  232. zwave.multiChannelV3.multiChannelCmdEncap(destinationEndPoint:endpoint).encapsulate(cmd).format()
  233. } else {
  234. cmd.format()
  235. }
  236. }
RAW Paste Data