Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 9.21 KB | None | 0 0
  1. /**
  2.  *  Copyright 2015 SmartThings
  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.  *  Z-Wave Door/Window Sensor
  14.  *
  15.  *  Author: SmartThings
  16.  *  Date: 2013-11-3
  17.  */
  18.  
  19. metadata {
  20.   definition (name: "Z-Wave Door/Window Sensor Doorbell", namespace: "smartthings", author: "SmartThings") {
  21.     capability "Contact Sensor"
  22.     capability "Sensor"
  23.     capability "Battery"
  24.     capability "Configuration"
  25.  
  26.     fingerprint deviceId: "0x2001", inClusters: "0x30,0x80,0x84,0x85,0x86,0x72"
  27.     fingerprint deviceId: "0x07", inClusters: "0x30"
  28.     fingerprint deviceId: "0x0701", inClusters: "0x5E,0x86,0x72,0x98", outClusters: "0x5A,0x82"
  29.   }
  30.  
  31.   // simulator metadata
  32.   simulator {
  33.     // status messages
  34.     status "open":  "command: 2001, payload: FF"
  35.     status "closed": "command: 2001, payload: 00"
  36.   }
  37.  
  38.   // UI tile definitions
  39.   tiles {
  40.     standardTile("button", "device.button", width: 2, height: 2) {
  41.       state "open", label: 'Default', icon: "st.Home.home30", backgroundColor: "#B0E0E6"
  42.       state "closed", label: 'Pushed', icon: "st.Home.home30", backgroundColor: "#53a7c0"
  43.     }
  44.     valueTile("battery", "device.battery", inactiveLabel: false, decoration: "flat") {
  45.       state "battery", label:'${currentValue}% battery', unit:""
  46.     }
  47.  
  48.     main "button"
  49.     details(["button", "battery"])
  50.   }
  51. }
  52.  
  53. def parse(String description) {
  54.   def result = null
  55.   if (description.startsWith("Err 106")) {
  56.     if (state.sec) {
  57.       log.debug description
  58.     } else {
  59.       result = createEvent(
  60.         descriptionText: "This sensor failed to complete the network security key exchange. If you are unable to control it via SmartThings, you must remove it from your network and add it again.",
  61.         eventType: "ALERT",
  62.         name: "secureInclusion",
  63.         value: "failed",
  64.         isStateChange: true,
  65.       )
  66.     }
  67.   } else if (description != "updated") {
  68.     def cmd = zwave.parse(description, [0x20: 1, 0x25: 1, 0x30: 1, 0x31: 5, 0x80: 1, 0x84: 1, 0x71: 3, 0x9C: 1])
  69.     if (cmd) {
  70.       result = zwaveEvent(cmd)
  71.     }
  72.   }
  73.   log.debug "parsed '$description' to $result"
  74.   return result
  75. }
  76.  
  77. def updated() {
  78.   def cmds = []
  79.   if (!state.MSR) {
  80.     cmds = [
  81.       zwave.manufacturerSpecificV2.manufacturerSpecificGet().format(),
  82.       "delay 1200",
  83.       zwave.wakeUpV1.wakeUpNoMoreInformation().format()
  84.     ]
  85.   } else if (!state.lastbat) {
  86.     cmds = []
  87.   } else {
  88.     cmds = [zwave.wakeUpV1.wakeUpNoMoreInformation().format()]
  89.   }
  90.   response(cmds)
  91. }
  92.  
  93. def configure() {
  94.   delayBetween([
  95.     zwave.manufacturerSpecificV2.manufacturerSpecificGet().format(),
  96.     batteryGetCommand()
  97.   ], 6000)
  98. }
  99.  
  100. // "Push" the button when the contact closes
  101. def sensorValueEvent(value) {
  102.   log.debug("Pushing button")
  103.   if (device.currentValue("button") != "closed") {
  104.     sendEvent( name : "button", value : "closed", descriptionText: "$device.displayName was pressed", unit : "" )
  105.     runIn(10, "releaseButton")
  106.   }
  107. }
  108.  
  109. // "Release" the button
  110. void releaseButton() {
  111.   log.debug("Releasing button")
  112.   sendEvent( name : "button", value: "open", descriptionText: "$device.displayName was released")
  113. }
  114.  
  115. def zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicReport cmd)
  116. {
  117.   sensorValueEvent(cmd.value)
  118. }
  119.  
  120. def zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicSet cmd)
  121. {
  122.   sensorValueEvent(cmd.value)
  123. }
  124.  
  125. def zwaveEvent(physicalgraph.zwave.commands.switchbinaryv1.SwitchBinaryReport cmd)
  126. {
  127.   sensorValueEvent(cmd.value)
  128. }
  129.  
  130. def zwaveEvent(physicalgraph.zwave.commands.sensorbinaryv1.SensorBinaryReport cmd)
  131. {
  132.   sensorValueEvent(cmd.sensorValue)
  133. }
  134.  
  135. def zwaveEvent(physicalgraph.zwave.commands.sensoralarmv1.SensorAlarmReport cmd)
  136. {
  137.   sensorValueEvent(cmd.sensorState)
  138. }
  139.  
  140. def zwaveEvent(physicalgraph.zwave.commands.notificationv3.NotificationReport cmd)
  141. {
  142.   def result = []
  143.   if (cmd.notificationType == 0x06 && cmd.event == 0x16) {
  144.     result << sensorValueEvent(1)
  145.   } else if (cmd.notificationType == 0x06 && cmd.event == 0x17) {
  146.     result << sensorValueEvent(0)
  147.   } else if (cmd.notificationType == 0x07) {
  148.     if (cmd.v1AlarmType == 0x07) {  // special case for nonstandard messages from Monoprice door/window sensors
  149.       result << sensorValueEvent(cmd.v1AlarmLevel)
  150.     } else if (cmd.event == 0x01 || cmd.event == 0x02) {
  151.       result << sensorValueEvent(1)
  152.     } else if (cmd.event == 0x03) {
  153.       result << createEvent(descriptionText: "$device.displayName covering was removed", isStateChange: true)
  154.       result << response(zwave.wakeUpV1.wakeUpIntervalSet(seconds:4*3600, nodeid:zwaveHubNodeId))
  155.       if(!state.MSR) result << response(zwave.manufacturerSpecificV2.manufacturerSpecificGet())
  156.     } else if (cmd.event == 0x05 || cmd.event == 0x06) {
  157.       result << createEvent(descriptionText: "$device.displayName detected glass breakage", isStateChange: true)
  158.     } else if (cmd.event == 0x07) {
  159.       if(!state.MSR) result << response(zwave.manufacturerSpecificV2.manufacturerSpecificGet())
  160.       result << createEvent(name: "motion", value: "active", descriptionText:"$device.displayName detected motion")
  161.     }
  162.   } else if (cmd.notificationType) {
  163.     def text = "Notification $cmd.notificationType: event ${([cmd.event] + cmd.eventParameter).join(", ")}"
  164.     result << createEvent(name: "notification$cmd.notificationType", value: "$cmd.event", descriptionText: text, displayed: false)
  165.   } else {
  166.     def value = cmd.v1AlarmLevel == 255 ? "active" : cmd.v1AlarmLevel ?: "inactive"
  167.     result << createEvent(name: "alarm $cmd.v1AlarmType", value: value, displayed: false)
  168.   }
  169.   result
  170. }
  171.  
  172. def zwaveEvent(physicalgraph.zwave.commands.wakeupv1.WakeUpNotification cmd)
  173. {
  174.   def event = createEvent(descriptionText: "${device.displayName} woke up", isStateChange: false)
  175.   def cmds = []
  176.   if (!state.MSR) {
  177.     cmds << zwave.wakeUpV1.wakeUpIntervalSet(seconds:4*3600, nodeid:zwaveHubNodeId).format()
  178.     cmds << zwave.manufacturerSpecificV2.manufacturerSpecificGet().format()
  179.     cmds << "delay 1200"
  180.   }
  181.   if (!state.lastbat || now() - state.lastbat > 53*60*60*1000) {
  182.     cmds << batteryGetCommand()
  183.   } else {
  184.     cmds << zwave.wakeUpV1.wakeUpNoMoreInformation().format()
  185.   }
  186.   [event, response(cmds)]
  187. }
  188.  
  189. def zwaveEvent(physicalgraph.zwave.commands.batteryv1.BatteryReport cmd) {
  190.   def map = [ name: "battery", unit: "%" ]
  191.   if (cmd.batteryLevel == 0xFF) {
  192.     map.value = 1
  193.     map.descriptionText = "${device.displayName} has a low battery"
  194.     map.isStateChange = true
  195.   } else {
  196.     map.value = cmd.batteryLevel
  197.   }
  198.   state.lastbat = now()
  199.   [createEvent(map), response(zwave.wakeUpV1.wakeUpNoMoreInformation())]
  200. }
  201.  
  202. def zwaveEvent(physicalgraph.zwave.commands.manufacturerspecificv2.ManufacturerSpecificReport cmd) {
  203.   def result = []
  204.  
  205.   def msr = String.format("%04X-%04X-%04X", cmd.manufacturerId, cmd.productTypeId, cmd.productId)
  206.   log.debug "msr: $msr"
  207.   updateDataValue("MSR", msr)
  208.  
  209.   retypeBasedOnMSR()
  210.  
  211.   result << createEvent(descriptionText: "$device.displayName MSR: $msr", isStateChange: false)
  212.  
  213.   if (msr == "011A-0601-0901") {  // Enerwave motion doesn't always get the associationSet that the hub sends on join
  214.     result << response(zwave.associationV1.associationSet(groupingIdentifier:1, nodeId:zwaveHubNodeId))
  215.   } else if (!device.currentState("battery")) {
  216.     if (msr == "0086-0102-0059") {
  217.       result << response(zwave.securityV1.securityMessageEncapsulation().encapsulate(zwave.batteryV1.batteryGet()).format())
  218.     } else {
  219.       result << response(batteryGetCommand())
  220.     }
  221.   }
  222.  
  223.   result
  224. }
  225.  
  226. def zwaveEvent(physicalgraph.zwave.commands.securityv1.SecurityMessageEncapsulation cmd) {
  227.   def encapsulatedCommand = cmd.encapsulatedCommand([0x20: 1, 0x85: 2, 0x70: 1])
  228.   // log.debug "encapsulated: $encapsulatedCommand"
  229.   if (encapsulatedCommand) {
  230.     state.sec = 1
  231.     zwaveEvent(encapsulatedCommand)
  232.   }
  233. }
  234.  
  235. def zwaveEvent(physicalgraph.zwave.Command cmd) {
  236.   createEvent(descriptionText: "$device.displayName: $cmd", displayed: false)
  237. }
  238.  
  239. def batteryGetCommand() {
  240.   def cmd = zwave.batteryV1.batteryGet()
  241.   if (state.sec) {
  242.     cmd = zwave.securityV1.securityMessageEncapsulation().encapsulate(cmd)
  243.   }
  244.   cmd.format()
  245. }
  246.  
  247. def retypeBasedOnMSR() {
  248.   switch (state.MSR) {
  249.     case "0086-0002-002D":
  250.       log.debug("Changing device type to Z-Wave Water Sensor")
  251.       setDeviceType("Z-Wave Water Sensor")
  252.       break
  253.     case "011F-0001-0001":  // Schlage motion
  254.     case "014A-0001-0001":  // Ecolink motion
  255.     case "014A-0004-0001":  // Ecolink motion +
  256.     case "0060-0001-0002":  // Everspring SP814
  257.     case "0060-0001-0003":  // Everspring HSP02
  258.     case "011A-0601-0901":  // Enerwave ZWN-BPC
  259.       log.debug("Changing device type to Z-Wave Motion Sensor")
  260.       setDeviceType("Z-Wave Motion Sensor")
  261.       break
  262.    
  263.   }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement