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
This commit is contained in:
Zach Varberg
2016-10-28 09:36:34 -05:00
parent 687c64d29d
commit 699f80e9f7
8 changed files with 123 additions and 188 deletions

View File

@@ -211,20 +211,14 @@ private Map getBatteryResult(rawValue) {
log.debug "Battery rawValue = ${rawValue}" log.debug "Battery rawValue = ${rawValue}"
def linkText = getLinkText(device) def linkText = getLinkText(device)
def result = [ def result = [:]
name: 'battery',
value: '--',
translatable: true
]
def volts = rawValue / 10 def volts = rawValue / 10
if (rawValue == 0 || rawValue == 255) {} if (!(rawValue == 0 || rawValue == 255)) {
else { result.name = 'battery'
if (volts > 3.5) { result.translatable = true
result.descriptionText = "{{ device.displayName }} battery has too much power: (> 3.5) volts." result.descriptionText = "{{ device.displayName }} battery was {{ value }}%"
}
else {
if (device.getDataValue("manufacturer") == "SmartThings") { if (device.getDataValue("manufacturer") == "SmartThings") {
volts = rawValue // For the batteryMap to work the key needs to be an int 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, def batteryMap = [28: 100, 27: 100, 26: 100, 25: 90, 24: 90, 23: 70,
@@ -237,12 +231,8 @@ private Map getBatteryResult(rawValue) {
else if (volts > maxVolts) else if (volts > maxVolts)
volts = maxVolts volts = maxVolts
def pct = batteryMap[volts] def pct = batteryMap[volts]
if (pct != null) {
result.value = pct result.value = pct
result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" } else {
}
}
else {
def minVolts = 2.1 def minVolts = 2.1
def maxVolts = 3.0 def maxVolts = 3.0
def pct = (volts - minVolts) / (maxVolts - minVolts) def pct = (volts - minVolts) / (maxVolts - minVolts)
@@ -250,9 +240,8 @@ private Map getBatteryResult(rawValue) {
if (roundedPct <= 0) if (roundedPct <= 0)
roundedPct = 1 roundedPct = 1
result.value = Math.min(100, roundedPct) result.value = Math.min(100, roundedPct)
result.descriptionText = "{{ device.displayName }} battery was {{ value }}%"
}
} }
} }
return result return result

View File

@@ -229,20 +229,14 @@ private Map getBatteryResult(rawValue) {
log.debug "Battery rawValue = ${rawValue}" log.debug "Battery rawValue = ${rawValue}"
def linkText = getLinkText(device) def linkText = getLinkText(device)
def result = [ def result = [:]
name: 'battery',
value: '--',
translatable: true
]
def volts = rawValue / 10 def volts = rawValue / 10
if (rawValue == 0 || rawValue == 255) {} if (!(rawValue == 0 || rawValue == 255)) {
else { result.name = 'battery'
if (volts > 3.5) { result.translatable = true
result.descriptionText = "{{ device.displayName }} battery has too much power: (> 3.5) volts." result.descriptionText = "{{ device.displayName }} battery was {{ value }}%"
}
else {
if (device.getDataValue("manufacturer") == "SmartThings") { if (device.getDataValue("manufacturer") == "SmartThings") {
volts = rawValue // For the batteryMap to work the key needs to be an int 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, def batteryMap = [28: 100, 27: 100, 26: 100, 25: 90, 24: 90, 23: 70,
@@ -255,13 +249,8 @@ private Map getBatteryResult(rawValue) {
else if (volts > maxVolts) else if (volts > maxVolts)
volts = maxVolts volts = maxVolts
def pct = batteryMap[volts] def pct = batteryMap[volts]
if (pct != null) {
result.value = pct result.value = pct
def value = pct } else {
result.descriptionText = "{{ device.displayName }} battery was {{ value }}%"
}
}
else {
def minVolts = 2.1 def minVolts = 2.1
def maxVolts = 3.0 def maxVolts = 3.0
def pct = (volts - minVolts) / (maxVolts - minVolts) def pct = (volts - minVolts) / (maxVolts - minVolts)
@@ -269,8 +258,6 @@ private Map getBatteryResult(rawValue) {
if (roundedPct <= 0) if (roundedPct <= 0)
roundedPct = 1 roundedPct = 1
result.value = Math.min(100, roundedPct) result.value = Math.min(100, roundedPct)
result.descriptionText = "{{ device.displayName }} battery was {{ value }}%"
}
} }
} }

View File

@@ -188,30 +188,21 @@ private Map getBatteryResult(rawValue) {
log.debug rawValue log.debug rawValue
def result = [ def result = [:]
name: 'battery',
value: '--'
]
def volts = rawValue / 10 def volts = rawValue / 10
def descriptionText
if (rawValue == 0 || rawValue == 255) {} 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 minVolts = 2.1
def maxVolts = 3.0 def maxVolts = 3.0
def pct = (volts - minVolts) / (maxVolts - minVolts) def pct = (volts - minVolts) / (maxVolts - minVolts)
def roundedPct = Math.round(pct * 100) def roundedPct = Math.round(pct * 100)
if (roundedPct <= 0) if (roundedPct <= 0)
roundedPct = 1 roundedPct = 1
result.name = 'battery'
result.value = Math.min(100, roundedPct) result.value = Math.min(100, roundedPct)
result.descriptionText = "${linkText} battery was ${result.value}%" result.descriptionText = "${linkText} battery was ${result.value}%"
} }
}
return result return result
} }

View File

@@ -286,20 +286,14 @@ def getTemperature(value) {
private Map getBatteryResult(rawValue) { private Map getBatteryResult(rawValue) {
log.debug "Battery rawValue = ${rawValue}" log.debug "Battery rawValue = ${rawValue}"
def result = [ def result = [:]
name: 'battery',
value: '--',
translatable: true
]
def volts = rawValue / 10 def volts = rawValue / 10
if (rawValue == 0 || rawValue == 255) {} if (!(rawValue == 0 || rawValue == 255)) {
else { result.name = 'battery'
if (volts > 3.5) { result.translatable = true
result.descriptionText = "{{ device.displayName }} battery has too much power: (> 3.5) volts." result.descriptionText = "{{ device.displayName }} battery was {{ value }}%"
}
else {
if (device.getDataValue("manufacturer") == "SmartThings") { if (device.getDataValue("manufacturer") == "SmartThings") {
volts = rawValue // For the batteryMap to work the key needs to be an int 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, def batteryMap = [28: 100, 27: 100, 26: 100, 25: 90, 24: 90, 23: 70,
@@ -312,12 +306,8 @@ private Map getBatteryResult(rawValue) {
else if (volts > maxVolts) else if (volts > maxVolts)
volts = maxVolts volts = maxVolts
def pct = batteryMap[volts] def pct = batteryMap[volts]
if (pct != null) {
result.value = pct result.value = pct
result.descriptionText = "{{ device.displayName }} battery was {{ value }}%" } else {
}
}
else {
def minVolts = 2.1 def minVolts = 2.1
def maxVolts = 3.0 def maxVolts = 3.0
def pct = (volts - minVolts) / (maxVolts - minVolts) def pct = (volts - minVolts) / (maxVolts - minVolts)
@@ -325,8 +315,6 @@ private Map getBatteryResult(rawValue) {
if (roundedPct <= 0) if (roundedPct <= 0)
roundedPct = 1 roundedPct = 1
result.value = Math.min(100, roundedPct) result.value = Math.min(100, roundedPct)
result.descriptionText = "{{ device.displayName }} battery was {{ value }}%"
}
} }
} }

View File

@@ -191,25 +191,21 @@ def getTemperature(value) {
log.debug 'Battery' log.debug 'Battery'
def linkText = getLinkText(device) def linkText = getLinkText(device)
def result = [ def result = [:]
name: 'battery'
]
def volts = rawValue / 10 def volts = rawValue / 10
def descriptionText
if (rawValue == 0 || rawValue == 255) {} 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 minVolts = 2.1
def maxVolts = 3.0 def maxVolts = 3.0
def pct = (volts - minVolts) / (maxVolts - minVolts) def pct = (volts - minVolts) / (maxVolts - minVolts)
def roundedPct = Math.round(pct * 100) def roundedPct = Math.round(pct * 100)
if (roundedPct <= 0) if (roundedPct <= 0)
roundedPct = 1 roundedPct = 1
result.name = 'battery'
result.value = Math.min(100, roundedPct) result.value = Math.min(100, roundedPct)
result.descriptionText = "${linkText} battery was ${result.value}%" result.descriptionText = "${linkText} battery was ${result.value}%"
} }
return result return result

View File

@@ -205,17 +205,10 @@ private Map getBatteryResult(rawValue) {
log.debug 'Battery' log.debug 'Battery'
def linkText = getLinkText(device) def linkText = getLinkText(device)
def result = [ def result = [:]
name: 'battery'
]
def volts = rawValue / 10 def volts = rawValue / 10
def descriptionText if (!(rawValue == 0 || rawValue == 255)) {
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 minVolts = 2.1
def maxVolts = 3.0 def maxVolts = 3.0
def pct = (volts - minVolts) / (maxVolts - minVolts) def pct = (volts - minVolts) / (maxVolts - minVolts)
@@ -224,6 +217,7 @@ private Map getBatteryResult(rawValue) {
roundedPct = 1 roundedPct = 1
result.value = Math.min(100, roundedPct) result.value = Math.min(100, roundedPct)
result.descriptionText = "${linkText} battery was ${result.value}%" result.descriptionText = "${linkText} battery was ${result.value}%"
result.name = 'battery'
} }
return result return result

View File

@@ -216,17 +216,10 @@ private Map getBatteryResult(rawValue) {
log.debug 'Battery' log.debug 'Battery'
def linkText = getLinkText(device) def linkText = getLinkText(device)
def result = [ def result = [:]
name: 'battery'
]
def volts = rawValue / 10 def volts = rawValue / 10
def descriptionText if (!(rawValue == 0 || rawValue == 255)) {
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 minVolts = 2.1
def maxVolts = 3.0 def maxVolts = 3.0
def pct = (volts - minVolts) / (maxVolts - minVolts) def pct = (volts - minVolts) / (maxVolts - minVolts)
@@ -235,6 +228,8 @@ private Map getBatteryResult(rawValue) {
roundedPct = 1 roundedPct = 1
result.value = Math.min(100, roundedPct) result.value = Math.min(100, roundedPct)
result.descriptionText = "${linkText} battery was ${result.value}%" result.descriptionText = "${linkText} battery was ${result.value}%"
result.name = 'battery'
} }
return result return result

View File

@@ -181,22 +181,17 @@ private Map getBatteryResult(rawValue) {
log.debug 'Battery' log.debug 'Battery'
def linkText = getLinkText(device) def linkText = getLinkText(device)
def result = [ def result = [:]
name: 'battery'
]
if (!(rawValue == 0 || rawValue == 255)) {
def volts = rawValue / 10 def volts = rawValue / 10
def descriptionText
if (volts > 3.5) {
result.descriptionText = "${linkText} battery has too much power (${volts} volts)."
}
else {
def minVolts = 2.1 def minVolts = 2.1
def maxVolts = 3.0 def maxVolts = 3.0
def pct = (volts - minVolts) / (maxVolts - minVolts) def pct = (volts - minVolts) / (maxVolts - minVolts)
def roundedPct = Math.round(pct * 100) def roundedPct = Math.round(pct * 100)
result.value = Math.min(100, roundedPct) result.value = Math.min(100, roundedPct)
result.descriptionText = "${linkText} battery was ${result.value}%" result.descriptionText = "${linkText} battery was ${result.value}%"
result.name = 'battery'
} }
return result return result