From f5d3cca6a0ee83e97469fd296927a98d49b23f9f Mon Sep 17 00:00:00 2001 From: vlaminck Date: Thu, 23 Jun 2016 09:27:54 -0500 Subject: [PATCH 1/2] Guard against devices that don't support necessary commands --- .../gentle-wake-up.src/gentle-wake-up.groovy | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/smartapps/smartthings/gentle-wake-up.src/gentle-wake-up.groovy b/smartapps/smartthings/gentle-wake-up.src/gentle-wake-up.groovy index 11f295c..f79a8fd 100644 --- a/smartapps/smartthings/gentle-wake-up.src/gentle-wake-up.groovy +++ b/smartapps/smartthings/gentle-wake-up.src/gentle-wake-up.groovy @@ -528,14 +528,16 @@ def updateDimmers(percentComplete) { } else { def shouldChangeColors = (colorize && colorize != "false") - def canChangeColors = hasSetColorCommand(dimmer) - log.debug "Setting ${deviceLabel(dimmer)} to ${nextLevel}" - - if (shouldChangeColors && canChangeColors) { - dimmer.setColor([hue: getHue(dimmer, nextLevel), saturation: 100, level: nextLevel]) - } else { + if (shouldChangeColors && hasSetColorCommand(dimmer)) { + def hue = getHue(dimmer, nextLevel) + log.debug "Setting ${deviceLabel(dimmer)} level to ${nextLevel} and hue to ${hue}" + dimmer.setColor([hue: hue, saturation: 100, level: nextLevel]) + } else if (hasSetLevelCommand(dimmer)) { + log.debug "Setting ${deviceLabel(dimmer)} level to ${nextLevel}" dimmer.setLevel(nextLevel) + } else { + log.warn "${deviceLabel(dimmer)} does not have setColor or setLevel commands." } } @@ -818,23 +820,15 @@ private getRedHue(level) { } private hasSetLevelCommand(device) { - def isDimmer = false - device.supportedCommands.each { - if (it.name.contains("setLevel")) { - isDimmer = true - } - } - return isDimmer + return hasCommand(device, "setLevel") } private hasSetColorCommand(device) { - def hasColor = false - device.supportedCommands.each { - if (it.name.contains("setColor")) { - hasColor = true - } - } - return hasColor + return hasCommand(device, "setColor") +} + +private hasCommand(device, String command) { + return (device.supportedCommands.find { it.name == command } != null) } private dimmersWithSetColorCommand() { From 972599b1b5325b555d942e41e754fd00c9d5950c Mon Sep 17 00:00:00 2001 From: vlaminck Date: Thu, 23 Jun 2016 12:19:54 -0500 Subject: [PATCH 2/2] Provide a way to remove unsupported devices from settings --- .../gentle-wake-up.src/gentle-wake-up.groovy | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/smartapps/smartthings/gentle-wake-up.src/gentle-wake-up.groovy b/smartapps/smartthings/gentle-wake-up.src/gentle-wake-up.groovy index f79a8fd..89dd9f6 100644 --- a/smartapps/smartthings/gentle-wake-up.src/gentle-wake-up.groovy +++ b/smartapps/smartthings/gentle-wake-up.src/gentle-wake-up.groovy @@ -39,6 +39,7 @@ preferences { page(name: "completionPage") page(name: "numbersPage") page(name: "controllerExplanationPage") + page(name: "unsupportedDevicesPage") } def rootPage() { @@ -47,6 +48,9 @@ def rootPage() { section("What to dim") { input(name: "dimmers", type: "capability.switchLevel", title: "Dimmers", description: null, multiple: true, required: true, submitOnChange: true) if (dimmers) { + if (dimmersContainUnsupportedDevices()) { + href(name: "toUnsupportedDevicesPage", page: "unsupportedDevicesPage", title: "Some of your selected dimmers don't seem to be supported", description: "Tap here to fix it", required: true) + } href(name: "toNumbersPage", page: "numbersPage", title: "Duration & Direction", description: numbersPageHrefDescription(), state: "complete") } } @@ -71,6 +75,31 @@ def rootPage() { } } +def unsupportedDevicesPage() { + + def unsupportedDimmers = dimmers.findAll { !hasSetLevelCommand(it) } + + dynamicPage(name: "unsupportedDevicesPage") { + if (unsupportedDimmers) { + section("These devices do not support the setLevel command") { + unsupportedDimmers.each { + paragraph deviceLabel(it) + } + } + section { + input(name: "dimmers", type: "capability.sensor", title: "Please remove the above devices from this list.", submitOnChange: true, multiple: true) + } + section { + paragraph "If you think there is a mistake here, please contact support." + } + } else { + section { + paragraph "You're all set. You can hit the back button, now. Thanks for cleaning up your settings :)" + } + } + } +} + def controllerExplanationPage() { dynamicPage(name: "controllerExplanationPage", title: "How To Control Gentle Wake Up") { @@ -819,6 +848,11 @@ private getRedHue(level) { if (level >= 96) return 17 } +private dimmersContainUnsupportedDevices() { + def found = dimmers.find { hasSetLevelCommand(it) == false } + return found != null +} + private hasSetLevelCommand(device) { return hasCommand(device, "setLevel") } @@ -1067,4 +1101,4 @@ def hasStartLevel() { def hasEndLevel() { return (endLevel != null && endLevel != "") -} +} \ No newline at end of file