From 699f80e9f7b1f124148d4ffb7fd60a94ddd54a95 Mon Sep 17 00:00:00 2001 From: Zach Varberg Date: Fri, 28 Oct 2016 09:36:34 -0500 Subject: [PATCH] Treat over voltage as 100% battery level Many DTHs that are generating battery events use the same bit of copy/pasted code and in that code over voltage is sent as a battery event with a value of "--" however, that non-numeric value results in stack traces. Instead we now report over voltage as 100% battery. This resolves: https://smartthings.atlassian.net/browse/DVCSMP-2177 --- .../smartsense-moisture-sensor.groovy | 63 ++++++++----------- .../smartsense-motion-sensor.groovy | 63 ++++++++----------- .../smartsense-motion-temp-sensor.groovy | 31 ++++----- .../smartsense-multi-sensor.groovy | 62 ++++++++---------- ...se-open-closed-accelerometer-sensor.groovy | 44 ++++++------- .../smartsense-open-closed-sensor.groovy | 14 ++--- .../smartsense-temp-humidity-sensor.groovy | 19 +++--- .../tyco-door-window-sensor.groovy | 15 ++--- 8 files changed, 123 insertions(+), 188 deletions(-) diff --git a/devicetypes/smartthings/smartsense-moisture-sensor.src/smartsense-moisture-sensor.groovy b/devicetypes/smartthings/smartsense-moisture-sensor.src/smartsense-moisture-sensor.groovy index 8ff9180..1d6776d 100644 --- a/devicetypes/smartthings/smartsense-moisture-sensor.src/smartsense-moisture-sensor.groovy +++ b/devicetypes/smartthings/smartsense-moisture-sensor.src/smartsense-moisture-sensor.groovy @@ -211,48 +211,37 @@ private Map getBatteryResult(rawValue) { log.debug "Battery rawValue = ${rawValue}" def linkText = getLinkText(device) - def result = [ - name: 'battery', - value: '--', - translatable: true - ] + def result = [:] def volts = rawValue / 10 - if (rawValue == 0 || rawValue == 255) {} - else { - if (volts > 3.5) { - result.descriptionText = "{{ device.displayName }} battery has too much power: (> 3.5) volts." - } - else { - if (device.getDataValue("manufacturer") == "SmartThings") { - volts = rawValue // For the batteryMap to work the key needs to be an int - def batteryMap = [28:100, 27:100, 26:100, 25:90, 24:90, 23:70, - 22:70, 21:50, 20:50, 19:30, 18:30, 17:15, 16:1, 15:0] - def minVolts = 15 - def maxVolts = 28 + if (!(rawValue == 0 || rawValue == 255)) { + result.name = 'battery' + result.translatable = true + result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" + if (device.getDataValue("manufacturer") == "SmartThings") { + volts = rawValue // For the batteryMap to work the key needs to be an int + def batteryMap = [28: 100, 27: 100, 26: 100, 25: 90, 24: 90, 23: 70, + 22: 70, 21: 50, 20: 50, 19: 30, 18: 30, 17: 15, 16: 1, 15: 0] + def minVolts = 15 + def maxVolts = 28 - if (volts < minVolts) - volts = minVolts - else if (volts > maxVolts) - volts = maxVolts - def pct = batteryMap[volts] - if (pct != null) { - result.value = pct - result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" - } - } - else { - def minVolts = 2.1 - def maxVolts = 3.0 - def pct = (volts - minVolts) / (maxVolts - minVolts) - def roundedPct = Math.round(pct * 100) - if (roundedPct <= 0) - roundedPct = 1 - result.value = Math.min(100, roundedPct) - result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" - } + if (volts < minVolts) + volts = minVolts + else if (volts > maxVolts) + volts = maxVolts + def pct = batteryMap[volts] + result.value = pct + } else { + def minVolts = 2.1 + def maxVolts = 3.0 + def pct = (volts - minVolts) / (maxVolts - minVolts) + def roundedPct = Math.round(pct * 100) + if (roundedPct <= 0) + roundedPct = 1 + result.value = Math.min(100, roundedPct) } + } return result diff --git a/devicetypes/smartthings/smartsense-motion-sensor.src/smartsense-motion-sensor.groovy b/devicetypes/smartthings/smartsense-motion-sensor.src/smartsense-motion-sensor.groovy index a9f0f52..fdbdcf6 100644 --- a/devicetypes/smartthings/smartsense-motion-sensor.src/smartsense-motion-sensor.groovy +++ b/devicetypes/smartthings/smartsense-motion-sensor.src/smartsense-motion-sensor.groovy @@ -229,48 +229,35 @@ private Map getBatteryResult(rawValue) { log.debug "Battery rawValue = ${rawValue}" def linkText = getLinkText(device) - def result = [ - name: 'battery', - value: '--', - translatable: true - ] + def result = [:] def volts = rawValue / 10 - if (rawValue == 0 || rawValue == 255) {} - else { - if (volts > 3.5) { - result.descriptionText = "{{ device.displayName }} battery has too much power: (> 3.5) volts." - } - else { - if (device.getDataValue("manufacturer") == "SmartThings") { - volts = rawValue // For the batteryMap to work the key needs to be an int - def batteryMap = [28:100, 27:100, 26:100, 25:90, 24:90, 23:70, - 22:70, 21:50, 20:50, 19:30, 18:30, 17:15, 16:1, 15:0] - def minVolts = 15 - def maxVolts = 28 + if (!(rawValue == 0 || rawValue == 255)) { + result.name = 'battery' + result.translatable = true + result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" + if (device.getDataValue("manufacturer") == "SmartThings") { + volts = rawValue // For the batteryMap to work the key needs to be an int + def batteryMap = [28: 100, 27: 100, 26: 100, 25: 90, 24: 90, 23: 70, + 22: 70, 21: 50, 20: 50, 19: 30, 18: 30, 17: 15, 16: 1, 15: 0] + def minVolts = 15 + def maxVolts = 28 - if (volts < minVolts) - volts = minVolts - else if (volts > maxVolts) - volts = maxVolts - def pct = batteryMap[volts] - if (pct != null) { - result.value = pct - def value = pct - result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" - } - } - else { - def minVolts = 2.1 - def maxVolts = 3.0 - def pct = (volts - minVolts) / (maxVolts - minVolts) - def roundedPct = Math.round(pct * 100) - if (roundedPct <= 0) - roundedPct = 1 - result.value = Math.min(100, roundedPct) - result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" - } + if (volts < minVolts) + volts = minVolts + else if (volts > maxVolts) + volts = maxVolts + def pct = batteryMap[volts] + result.value = pct + } else { + def minVolts = 2.1 + def maxVolts = 3.0 + def pct = (volts - minVolts) / (maxVolts - minVolts) + def roundedPct = Math.round(pct * 100) + if (roundedPct <= 0) + roundedPct = 1 + result.value = Math.min(100, roundedPct) } } diff --git a/devicetypes/smartthings/smartsense-motion-temp-sensor.src/smartsense-motion-temp-sensor.groovy b/devicetypes/smartthings/smartsense-motion-temp-sensor.src/smartsense-motion-temp-sensor.groovy index 6146673..057ec7e 100644 --- a/devicetypes/smartthings/smartsense-motion-temp-sensor.src/smartsense-motion-temp-sensor.groovy +++ b/devicetypes/smartthings/smartsense-motion-temp-sensor.src/smartsense-motion-temp-sensor.groovy @@ -188,29 +188,20 @@ private Map getBatteryResult(rawValue) { log.debug rawValue - def result = [ - name: 'battery', - value: '--' - ] + def result = [:] def volts = rawValue / 10 - def descriptionText - if (rawValue == 0 || rawValue == 255) {} - else { - if (volts > 3.5) { - result.descriptionText = "${linkText} battery has too much power (${volts} volts)." - } - else if (volts > 0){ - def minVolts = 2.1 - def maxVolts = 3.0 - def pct = (volts - minVolts) / (maxVolts - minVolts) - def roundedPct = Math.round(pct * 100) - if (roundedPct <= 0) - roundedPct = 1 - result.value = Math.min(100, roundedPct) - result.descriptionText = "${linkText} battery was ${result.value}%" - } + if (!(rawValue == 0 || rawValue == 255)) { + def minVolts = 2.1 + def maxVolts = 3.0 + def pct = (volts - minVolts) / (maxVolts - minVolts) + def roundedPct = Math.round(pct * 100) + if (roundedPct <= 0) + roundedPct = 1 + result.name = 'battery' + result.value = Math.min(100, roundedPct) + result.descriptionText = "${linkText} battery was ${result.value}%" } return result diff --git a/devicetypes/smartthings/smartsense-multi-sensor.src/smartsense-multi-sensor.groovy b/devicetypes/smartthings/smartsense-multi-sensor.src/smartsense-multi-sensor.groovy index b1c333a..bdde60a 100644 --- a/devicetypes/smartthings/smartsense-multi-sensor.src/smartsense-multi-sensor.groovy +++ b/devicetypes/smartthings/smartsense-multi-sensor.src/smartsense-multi-sensor.groovy @@ -286,47 +286,35 @@ def getTemperature(value) { private Map getBatteryResult(rawValue) { log.debug "Battery rawValue = ${rawValue}" - def result = [ - name: 'battery', - value: '--', - translatable: true - ] + def result = [:] def volts = rawValue / 10 - if (rawValue == 0 || rawValue == 255) {} - else { - if (volts > 3.5) { - result.descriptionText = "{{ device.displayName }} battery has too much power: (> 3.5) volts." - } - else { - if (device.getDataValue("manufacturer") == "SmartThings") { - volts = rawValue // For the batteryMap to work the key needs to be an int - def batteryMap = [28:100, 27:100, 26:100, 25:90, 24:90, 23:70, - 22:70, 21:50, 20:50, 19:30, 18:30, 17:15, 16:1, 15:0] - def minVolts = 15 - def maxVolts = 28 + if (!(rawValue == 0 || rawValue == 255)) { + result.name = 'battery' + result.translatable = true + result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" + if (device.getDataValue("manufacturer") == "SmartThings") { + volts = rawValue // For the batteryMap to work the key needs to be an int + def batteryMap = [28: 100, 27: 100, 26: 100, 25: 90, 24: 90, 23: 70, + 22: 70, 21: 50, 20: 50, 19: 30, 18: 30, 17: 15, 16: 1, 15: 0] + def minVolts = 15 + def maxVolts = 28 - if (volts < minVolts) - volts = minVolts - else if (volts > maxVolts) - volts = maxVolts - def pct = batteryMap[volts] - if (pct != null) { - result.value = pct - result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" - } - } - else { - def minVolts = 2.1 - def maxVolts = 3.0 - def pct = (volts - minVolts) / (maxVolts - minVolts) - def roundedPct = Math.round(pct * 100) - if (roundedPct <= 0) - roundedPct = 1 - result.value = Math.min(100, roundedPct) - result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" - } + if (volts < minVolts) + volts = minVolts + else if (volts > maxVolts) + volts = maxVolts + def pct = batteryMap[volts] + result.value = pct + } else { + def minVolts = 2.1 + def maxVolts = 3.0 + def pct = (volts - minVolts) / (maxVolts - minVolts) + def roundedPct = Math.round(pct * 100) + if (roundedPct <= 0) + roundedPct = 1 + result.value = Math.min(100, roundedPct) } } diff --git a/devicetypes/smartthings/smartsense-open-closed-accelerometer-sensor.src/smartsense-open-closed-accelerometer-sensor.groovy b/devicetypes/smartthings/smartsense-open-closed-accelerometer-sensor.src/smartsense-open-closed-accelerometer-sensor.groovy index 9af4f2d..d7178dd 100644 --- a/devicetypes/smartthings/smartsense-open-closed-accelerometer-sensor.src/smartsense-open-closed-accelerometer-sensor.groovy +++ b/devicetypes/smartthings/smartsense-open-closed-accelerometer-sensor.src/smartsense-open-closed-accelerometer-sensor.groovy @@ -187,35 +187,31 @@ def getTemperature(value) { } } - private Map getBatteryResult(rawValue) { - log.debug 'Battery' - def linkText = getLinkText(device) +private Map getBatteryResult(rawValue) { + log.debug 'Battery' + def linkText = getLinkText(device) - def result = [ - name: 'battery' - ] + def result = [:] - def volts = rawValue / 10 - def descriptionText - if (rawValue == 0 || rawValue == 255) {} - else if (volts > 3.5) { - result.descriptionText = "${linkText} battery has too much power (${volts} volts)." - } - else { - def minVolts = 2.1 - def maxVolts = 3.0 - def pct = (volts - minVolts) / (maxVolts - minVolts) - def roundedPct = Math.round(pct * 100) - if (roundedPct <= 0) - roundedPct = 1 - result.value = Math.min(100, roundedPct) - result.descriptionText = "${linkText} battery was ${result.value}%" - } + def volts = rawValue / 10 + + if (!(rawValue == 0 || rawValue == 255)) { + def minVolts = 2.1 + def maxVolts = 3.0 + def pct = (volts - minVolts) / (maxVolts - minVolts) + def roundedPct = Math.round(pct * 100) + if (roundedPct <= 0) + roundedPct = 1 + result.name = 'battery' + result.value = Math.min(100, roundedPct) + result.descriptionText = "${linkText} battery was ${result.value}%" - return result } - private Map getTemperatureResult(value) { + return result +} + +private Map getTemperatureResult(value) { log.debug 'TEMP' def linkText = getLinkText(device) if (tempOffset) { diff --git a/devicetypes/smartthings/smartsense-open-closed-sensor.src/smartsense-open-closed-sensor.groovy b/devicetypes/smartthings/smartsense-open-closed-sensor.src/smartsense-open-closed-sensor.groovy index 483f31a..659ef52 100644 --- a/devicetypes/smartthings/smartsense-open-closed-sensor.src/smartsense-open-closed-sensor.groovy +++ b/devicetypes/smartthings/smartsense-open-closed-sensor.src/smartsense-open-closed-sensor.groovy @@ -205,25 +205,19 @@ private Map getBatteryResult(rawValue) { log.debug 'Battery' def linkText = getLinkText(device) - def result = [ - name: 'battery' - ] + def result = [:] def volts = rawValue / 10 - def descriptionText - if (rawValue == 0 || rawValue == 255) {} - else if (volts > 3.5) { - result.descriptionText = "${linkText} battery has too much power (${volts} volts)." - } - else { + if (!(rawValue == 0 || rawValue == 255)) { def minVolts = 2.1 - def maxVolts = 3.0 + def maxVolts = 3.0 def pct = (volts - minVolts) / (maxVolts - minVolts) def roundedPct = Math.round(pct * 100) if (roundedPct <= 0) roundedPct = 1 result.value = Math.min(100, roundedPct) result.descriptionText = "${linkText} battery was ${result.value}%" + result.name = 'battery' } return result diff --git a/devicetypes/smartthings/smartsense-temp-humidity-sensor.src/smartsense-temp-humidity-sensor.groovy b/devicetypes/smartthings/smartsense-temp-humidity-sensor.src/smartsense-temp-humidity-sensor.groovy index b7873c7..88b52d7 100644 --- a/devicetypes/smartthings/smartsense-temp-humidity-sensor.src/smartsense-temp-humidity-sensor.groovy +++ b/devicetypes/smartthings/smartsense-temp-humidity-sensor.src/smartsense-temp-humidity-sensor.groovy @@ -216,25 +216,20 @@ private Map getBatteryResult(rawValue) { log.debug 'Battery' def linkText = getLinkText(device) - def result = [ - name: 'battery' - ] + def result = [:] def volts = rawValue / 10 - def descriptionText - if (rawValue == 0 || rawValue == 255) {} - else if (volts > 3.5) { - result.descriptionText = "${linkText} battery has too much power (${volts} volts)." - } - else { + if (!(rawValue == 0 || rawValue == 255)) { def minVolts = 2.1 - def maxVolts = 3.0 + def maxVolts = 3.0 def pct = (volts - minVolts) / (maxVolts - minVolts) def roundedPct = Math.round(pct * 100) - if (roundedPct <= 0) - roundedPct = 1 + if (roundedPct <= 0) + roundedPct = 1 result.value = Math.min(100, roundedPct) result.descriptionText = "${linkText} battery was ${result.value}%" + result.name = 'battery' + } return result diff --git a/devicetypes/smartthings/tyco-door-window-sensor.src/tyco-door-window-sensor.groovy b/devicetypes/smartthings/tyco-door-window-sensor.src/tyco-door-window-sensor.groovy index 98f75a0..95238d4 100644 --- a/devicetypes/smartthings/tyco-door-window-sensor.src/tyco-door-window-sensor.groovy +++ b/devicetypes/smartthings/tyco-door-window-sensor.src/tyco-door-window-sensor.groovy @@ -181,22 +181,17 @@ private Map getBatteryResult(rawValue) { log.debug 'Battery' def linkText = getLinkText(device) - def result = [ - name: 'battery' - ] + def result = [:] - def volts = rawValue / 10 - def descriptionText - if (volts > 3.5) { - result.descriptionText = "${linkText} battery has too much power (${volts} volts)." - } - else { + if (!(rawValue == 0 || rawValue == 255)) { + def volts = rawValue / 10 def minVolts = 2.1 - def maxVolts = 3.0 + def maxVolts = 3.0 def pct = (volts - minVolts) / (maxVolts - minVolts) def roundedPct = Math.round(pct * 100) result.value = Math.min(100, roundedPct) result.descriptionText = "${linkText} battery was ${result.value}%" + result.name = 'battery' } return result