Advertisement
Guest User

zwave-door-window-sensor-door-bell.groovy

a guest
Apr 11th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 9.40 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("contact", "device.contact", 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 "contact"
  49.     details(["contact", "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.     createEvent(name: "contact", value: "closed", descriptionText: "$device.displayName was pressed")
  105.     sendEvent( name : "contact", value : "closed", descriptionText: "$device.displayName was pressed")
  106.     runIn(10, "releaseButton")
  107.   }
  108. }
  109.  
  110. // "Release" the button
  111. void releaseButton() {
  112.   log.debug("Releasing button")
  113.   createEvent(name: "contact", value: "open", descriptionText: "$device.displayName was released")
  114.   sendEvent( name : "contact", value: "open", descriptionText: "$device.displayName was released")
  115. }
  116.  
  117. def zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicReport cmd)
  118. {
  119.   sensorValueEvent(cmd.value)
  120. }
  121.  
  122. def zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicSet cmd)
  123. {
  124.   sensorValueEvent(cmd.value)
  125. }
  126.  
  127. def zwaveEvent(physicalgraph.zwave.commands.switchbinaryv1.SwitchBinaryReport cmd)
  128. {
  129.   sensorValueEvent(cmd.value)
  130. }
  131.  
  132. def zwaveEvent(physicalgraph.zwave.commands.sensorbinaryv1.SensorBinaryReport cmd)
  133. {
  134.   sensorValueEvent(cmd.sensorValue)
  135. }
  136.  
  137. def zwaveEvent(physicalgraph.zwave.commands.sensoralarmv1.SensorAlarmReport cmd)
  138. {
  139.   sensorValueEvent(cmd.sensorState)
  140. }
  141.  
  142. def zwaveEvent(physicalgraph.zwave.commands.notificationv3.NotificationReport cmd)
  143. {
  144.   def result = []
  145.   if (cmd.notificationType == 0x06 && cmd.event == 0x16) {
  146.     result << sensorValueEvent(1)
  147.   } else if (cmd.notificationType == 0x06 && cmd.event == 0x17) {
  148.     result << sensorValueEvent(0)
  149.   } else if (cmd.notificationType == 0x07) {
  150.     if (cmd.v1AlarmType == 0x07) {  // special case for nonstandard messages from Monoprice door/window sensors
  151.       result << sensorValueEvent(cmd.v1AlarmLevel)
  152.     } else if (cmd.event == 0x01 || cmd.event == 0x02) {
  153.       result << sensorValueEvent(1)
  154.     } else if (cmd.event == 0x03) {
  155.       result << createEvent(descriptionText: "$device.displayName covering was removed", isStateChange: true)
  156.       result << response(zwave.wakeUpV1.wakeUpIntervalSet(seconds:4*3600, nodeid:zwaveHubNodeId))
  157.       if(!state.MSR) result << response(zwave.manufacturerSpecificV2.manufacturerSpecificGet())
  158.     } else if (cmd.event == 0x05 || cmd.event == 0x06) {
  159.       result << createEvent(descriptionText: "$device.displayName detected glass breakage", isStateChange: true)
  160.     } else if (cmd.event == 0x07) {
  161.       if(!state.MSR) result << response(zwave.manufacturerSpecificV2.manufacturerSpecificGet())
  162.       result << createEvent(name: "motion", value: "active", descriptionText:"$device.displayName detected motion")
  163.     }
  164.   } else if (cmd.notificationType) {
  165.     def text = "Notification $cmd.notificationType: event ${([cmd.event] + cmd.eventParameter).join(", ")}"
  166.     result << createEvent(name: "notification$cmd.notificationType", value: "$cmd.event", descriptionText: text, displayed: false)
  167.   } else {
  168.     def value = cmd.v1AlarmLevel == 255 ? "active" : cmd.v1AlarmLevel ?: "inactive"
  169.     result << createEvent(name: "alarm $cmd.v1AlarmType", value: value, displayed: false)
  170.   }
  171.   result
  172. }
  173.  
  174. def zwaveEvent(physicalgraph.zwave.commands.wakeupv1.WakeUpNotification cmd)
  175. {
  176.   def event = createEvent(descriptionText: "${device.displayName} woke up", isStateChange: false)
  177.   def cmds = []
  178.   if (!state.MSR) {
  179.     cmds << zwave.wakeUpV1.wakeUpIntervalSet(seconds:4*3600, nodeid:zwaveHubNodeId).format()
  180.     cmds << zwave.manufacturerSpecificV2.manufacturerSpecificGet().format()
  181.     cmds << "delay 1200"
  182.   }
  183.   if (!state.lastbat || now() - state.lastbat > 53*60*60*1000) {
  184.     cmds << batteryGetCommand()
  185.   } else {
  186.     cmds << zwave.wakeUpV1.wakeUpNoMoreInformation().format()
  187.   }
  188.   [event, response(cmds)]
  189. }
  190.  
  191. def zwaveEvent(physicalgraph.zwave.commands.batteryv1.BatteryReport cmd) {
  192.   def map = [ name: "battery", unit: "%" ]
  193.   if (cmd.batteryLevel == 0xFF) {
  194.     map.value = 1
  195.     map.descriptionText = "${device.displayName} has a low battery"
  196.     map.isStateChange = true
  197.   } else {
  198.     map.value = cmd.batteryLevel
  199.   }
  200.   state.lastbat = now()
  201.   [createEvent(map), response(zwave.wakeUpV1.wakeUpNoMoreInformation())]
  202. }
  203.  
  204. def zwaveEvent(physicalgraph.zwave.commands.manufacturerspecificv2.ManufacturerSpecificReport cmd) {
  205.   def result = []
  206.  
  207.   def msr = String.format("%04X-%04X-%04X", cmd.manufacturerId, cmd.productTypeId, cmd.productId)
  208.   log.debug "msr: $msr"
  209.   updateDataValue("MSR", msr)
  210.  
  211.   retypeBasedOnMSR()
  212.  
  213.   result << createEvent(descriptionText: "$device.displayName MSR: $msr", isStateChange: false)
  214.  
  215.   if (msr == "011A-0601-0901") {  // Enerwave motion doesn't always get the associationSet that the hub sends on join
  216.     result << response(zwave.associationV1.associationSet(groupingIdentifier:1, nodeId:zwaveHubNodeId))
  217.   } else if (!device.currentState("battery")) {
  218.     if (msr == "0086-0102-0059") {
  219.       result << response(zwave.securityV1.securityMessageEncapsulation().encapsulate(zwave.batteryV1.batteryGet()).format())
  220.     } else {
  221.       result << response(batteryGetCommand())
  222.     }
  223.   }
  224.  
  225.   result
  226. }
  227.  
  228. def zwaveEvent(physicalgraph.zwave.commands.securityv1.SecurityMessageEncapsulation cmd) {
  229.   def encapsulatedCommand = cmd.encapsulatedCommand([0x20: 1, 0x85: 2, 0x70: 1])
  230.   // log.debug "encapsulated: $encapsulatedCommand"
  231.   if (encapsulatedCommand) {
  232.     state.sec = 1
  233.     zwaveEvent(encapsulatedCommand)
  234.   }
  235. }
  236.  
  237. def zwaveEvent(physicalgraph.zwave.Command cmd) {
  238.   createEvent(descriptionText: "$device.displayName: $cmd", displayed: false)
  239. }
  240.  
  241. def batteryGetCommand() {
  242.   def cmd = zwave.batteryV1.batteryGet()
  243.   if (state.sec) {
  244.     cmd = zwave.securityV1.securityMessageEncapsulation().encapsulate(cmd)
  245.   }
  246.   cmd.format()
  247. }
  248.  
  249. def retypeBasedOnMSR() {
  250.   switch (state.MSR) {
  251.     case "0086-0002-002D":
  252.       log.debug("Changing device type to Z-Wave Water Sensor")
  253.       setDeviceType("Z-Wave Water Sensor")
  254.       break
  255.     case "011F-0001-0001":  // Schlage motion
  256.     case "014A-0001-0001":  // Ecolink motion
  257.     case "014A-0004-0001":  // Ecolink motion +
  258.     case "0060-0001-0002":  // Everspring SP814
  259.     case "0060-0001-0003":  // Everspring HSP02
  260.     case "011A-0601-0901":  // Enerwave ZWN-BPC
  261.       log.debug("Changing device type to Z-Wave Motion Sensor")
  262.       setDeviceType("Z-Wave Motion Sensor")
  263.       break
  264.    
  265.   }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement