From 798a5dea0a5d90ead0915b8f1f6a677e6fbf5275 Mon Sep 17 00:00:00 2001 From: Nathan Shaw Date: Sat, 15 Jul 2017 08:11:10 -0700 Subject: [PATCH] MSA-2097: Simple Hue Bulb Template. --- .../smartthings/hue-bulb.src/hue-bulb.groovy | 405 +++++++++--------- 1 file changed, 202 insertions(+), 203 deletions(-) diff --git a/devicetypes/smartthings/hue-bulb.src/hue-bulb.groovy b/devicetypes/smartthings/hue-bulb.src/hue-bulb.groovy index 8752ce4..d29e1f0 100644 --- a/devicetypes/smartthings/hue-bulb.src/hue-bulb.groovy +++ b/devicetypes/smartthings/hue-bulb.src/hue-bulb.groovy @@ -1,203 +1,202 @@ -//DEPRECATED. INTEGRATION MOVED TO SUPER LAN CONNECT - -/** - * Hue Bulb - * - * Philips Hue Type "Extended Color Light" - * - * Author: SmartThings - */ - -// for the UI -metadata { - // Automatically generated. Make future change here. - definition (name: "Hue Bulb", namespace: "smartthings", author: "SmartThings") { - capability "Switch Level" - capability "Actuator" - capability "Color Control" - capability "Color Temperature" - capability "Switch" - capability "Refresh" - capability "Sensor" - capability "Health Check" - capability "Light" - - command "setAdjustedColor" - command "reset" - command "refresh" - } - - simulator { - // TODO: define status and reply messages here - } - - tiles (scale: 2){ - multiAttributeTile(name:"rich-control", type: "lighting", width: 6, height: 4, canChangeIcon: true){ - tileAttribute ("device.switch", key: "PRIMARY_CONTROL") { - attributeState "on", label:'${name}', action:"switch.off", icon:"st.lights.philips.hue-single", backgroundColor:"#00A0DC", nextState:"turningOff" - attributeState "off", label:'${name}', action:"switch.on", icon:"st.lights.philips.hue-single", backgroundColor:"#ffffff", nextState:"turningOn" - attributeState "turningOn", label:'${name}', action:"switch.off", icon:"st.lights.philips.hue-single", backgroundColor:"#00A0DC", nextState:"turningOff" - attributeState "turningOff", label:'${name}', action:"switch.on", icon:"st.lights.philips.hue-single", backgroundColor:"#ffffff", nextState:"turningOn" - } - tileAttribute ("device.level", key: "SLIDER_CONTROL") { - attributeState "level", action:"switch level.setLevel", range:"(0..100)" - } - tileAttribute ("device.color", key: "COLOR_CONTROL") { - attributeState "color", action:"setAdjustedColor" - } - } - - controlTile("colorTempSliderControl", "device.colorTemperature", "slider", width: 4, height: 2, inactiveLabel: false, range:"(2000..6500)") { - state "colorTemperature", action:"color temperature.setColorTemperature" - } - - valueTile("colorTemp", "device.colorTemperature", inactiveLabel: false, decoration: "flat", width: 2, height: 2) { - state "colorTemperature", label: 'WHITES' - } - - standardTile("reset", "device.reset", height: 2, width: 2, inactiveLabel: false, decoration: "flat") { - state "default", label:"Reset To White", action:"reset", icon:"st.lights.philips.hue-single" - } - - standardTile("refresh", "device.refresh", height: 2, width: 2, inactiveLabel: false, decoration: "flat") { - state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh" - } - - main(["rich-control"]) - details(["rich-control", "colorTempSliderControl", "colorTemp", "reset", "refresh"]) - } -} - -def initialize() { - sendEvent(name: "DeviceWatch-Enroll", value: "{\"protocol\": \"LAN\", \"scheme\":\"untracked\", \"hubHardwareId\": \"${device.hub.hardwareID}\"}", displayed: false) -} - -void installed() { - log.debug "installed()" - initialize() -} - -def updated() { - log.debug "updated()" - initialize() -} - -// parse events into attributes -def parse(description) { - log.debug "parse() - $description" - def results = [] - - def map = description - if (description instanceof String) { - log.debug "Hue Bulb stringToMap - ${map}" - map = stringToMap(description) - } - - if (map?.name && map?.value) { - results << createEvent(name: "${map?.name}", value: "${map?.value}") - } - results -} - -// handle commands -void on() { - log.trace parent.on(this) -} - -void off() { - log.trace parent.off(this) -} - -void setLevel(percent) { - log.debug "Executing 'setLevel'" - if (verifyPercent(percent)) { - log.trace parent.setLevel(this, percent) - } -} - -void setSaturation(percent) { - log.debug "Executing 'setSaturation'" - if (verifyPercent(percent)) { - log.trace parent.setSaturation(this, percent) - } -} - -void setHue(percent) { - log.debug "Executing 'setHue'" - if (verifyPercent(percent)) { - log.trace parent.setHue(this, percent) - } -} - -void setColor(value) { - def events = [] - def validValues = [:] - - if (verifyPercent(value.hue)) { - validValues.hue = value.hue - } - if (verifyPercent(value.saturation)) { - validValues.saturation = value.saturation - } - if (value.hex != null) { - if (value.hex ==~ /^\#([A-Fa-f0-9]){6}$/) { - validValues.hex = value.hex - } else { - log.warn "$value.hex is not a valid color" - } - } - if (verifyPercent(value.level)) { - validValues.level = value.level - } - if (value.switch == "off" || (value.level != null && value.level <= 0)) { - validValues.switch = "off" - } else { - validValues.switch = "on" - } - if (!validValues.isEmpty()) { - log.trace parent.setColor(this, validValues) - } -} - -void reset() { - log.debug "Executing 'reset'" - setColorTemperature(4000) -} - -void setAdjustedColor(value) { - if (value) { - log.trace "setAdjustedColor: ${value}" - def adjusted = value + [:] - // Needed because color picker always sends 100 - adjusted.level = null - setColor(adjusted) - } else { - log.warn "Invalid color input $value" - } -} - -void setColorTemperature(value) { - if (value) { - log.trace "setColorTemperature: ${value}k" - log.trace parent.setColorTemperature(this, value) - } else { - log.warn "Invalid color temperature $value" - } -} - -void refresh() { - log.debug "Executing 'refresh'" - parent?.manualRefresh() -} - -def verifyPercent(percent) { - if (percent == null) - return false - else if (percent >= 0 && percent <= 100) { - return true - } else { - log.warn "$percent is not 0-100" - return false - } -} - +//DEPRECATED. INTEGRATION MOVED TO SUPER LAN CONNECT + +/** + * Hue Bulb + * + * Philips Hue Type "Extended Color Light" + * + * Author: SmartThings + */ + +// for the UI +metadata { + // Automatically generated. Make future change here. + definition (name: "Hue Bulb", namespace: "smartthings", author: "SmartThings") { + capability "Switch Level" + capability "Actuator" + capability "Color Control" + capability "Color Temperature" + capability "Switch" + capability "Refresh" + capability "Sensor" + capability "Health Check" + capability "Light" + + command "setAdjustedColor" + command "reset" + command "refresh" + } + + simulator { + // TODO: define status and reply messages here + } + + tiles (scale: 2){ + multiAttributeTile(name:"rich-control", type: "lighting", width: 6, height: 4, canChangeIcon: true){ + tileAttribute ("device.switch", key: "PRIMARY_CONTROL") { + attributeState "on", label:'${name}', action:"switch.off", icon:"st.lights.philips.hue-single", backgroundColor:"#00A0DC", nextState:"turningOff" + attributeState "off", label:'${name}', action:"switch.on", icon:"st.lights.philips.hue-single", backgroundColor:"#ffffff", nextState:"turningOn" + attributeState "turningOn", label:'${name}', action:"switch.off", icon:"st.lights.philips.hue-single", backgroundColor:"#00A0DC", nextState:"turningOff" + attributeState "turningOff", label:'${name}', action:"switch.on", icon:"st.lights.philips.hue-single", backgroundColor:"#ffffff", nextState:"turningOn" + } + tileAttribute ("device.level", key: "SLIDER_CONTROL") { + attributeState "level", action:"switch level.setLevel", range:"(0..100)" + } + tileAttribute ("device.color", key: "COLOR_CONTROL") { + attributeState "color", action:"setAdjustedColor" + } + } + + controlTile("colorTempSliderControl", "device.colorTemperature", "slider", width: 4, height: 2, inactiveLabel: false, range:"(2000..6500)") { + state "colorTemperature", action:"color temperature.setColorTemperature" + } + + valueTile("colorTemp", "device.colorTemperature", inactiveLabel: false, decoration: "flat", width: 2, height: 2) { + state "colorTemperature", label: 'WHITES' + } + + standardTile("reset", "device.reset", height: 2, width: 2, inactiveLabel: false, decoration: "flat") { + state "default", label:"Reset To White", action:"reset", icon:"st.lights.philips.hue-single" + } + + standardTile("refresh", "device.refresh", height: 2, width: 2, inactiveLabel: false, decoration: "flat") { + state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh" + } + + main(["rich-control"]) + details(["rich-control", "colorTempSliderControl", "colorTemp", "reset", "refresh"]) + } +} + +def initialize() { + sendEvent(name: "DeviceWatch-Enroll", value: "{\"protocol\": \"LAN\", \"scheme\":\"untracked\", \"hubHardwareId\": \"${device.hub.hardwareID}\"}", displayed: false) +} + +void installed() { + log.debug "installed()" + initialize() +} + +def updated() { + log.debug "updated()" + initialize() +} + +// parse events into attributes +def parse(description) { + log.debug "parse() - $description" + def results = [] + + def map = description + if (description instanceof String) { + log.debug "Hue Bulb stringToMap - ${map}" + map = stringToMap(description) + } + + if (map?.name && map?.value) { + results << createEvent(name: "${map?.name}", value: "${map?.value}") + } + results +} + +// handle commands +void on() { + log.trace parent.on(this) +} + +void off() { + log.trace parent.off(this) +} + +void setLevel(percent) { + log.debug "Executing 'setLevel'" + if (verifyPercent(percent)) { + log.trace parent.setLevel(this, percent) + } +} + +void setSaturation(percent) { + log.debug "Executing 'setSaturation'" + if (verifyPercent(percent)) { + log.trace parent.setSaturation(this, percent) + } +} + +void setHue(percent) { + log.debug "Executing 'setHue'" + if (verifyPercent(percent)) { + log.trace parent.setHue(this, percent) + } +} + +void setColor(value) { + def events = [] + def validValues = [:] + + if (verifyPercent(value.hue)) { + validValues.hue = value.hue + } + if (verifyPercent(value.saturation)) { + validValues.saturation = value.saturation + } + if (value.hex != null) { + if (value.hex ==~ /^\#([A-Fa-f0-9]){6}$/) { + validValues.hex = value.hex + } else { + log.warn "$value.hex is not a valid color" + } + } + if (verifyPercent(value.level)) { + validValues.level = value.level + } + if (value.switch == "off" || (value.level != null && value.level <= 0)) { + validValues.switch = "off" + } else { + validValues.switch = "on" + } + if (!validValues.isEmpty()) { + log.trace parent.setColor(this, validValues) + } +} + +void reset() { + log.debug "Executing 'reset'" + setColorTemperature(4000) +} + +void setAdjustedColor(value) { + if (value) { + log.trace "setAdjustedColor: ${value}" + def adjusted = value + [:] + // Needed because color picker always sends 100 + adjusted.level = null + setColor(adjusted) + } else { + log.warn "Invalid color input $value" + } +} + +void setColorTemperature(value) { + if (value) { + log.trace "setColorTemperature: ${value}k" + log.trace parent.setColorTemperature(this, value) + } else { + log.warn "Invalid color temperature $value" + } +} + +void refresh() { + log.debug "Executing 'refresh'" + parent?.manualRefresh() +} + +def verifyPercent(percent) { + if (percent == null) + return false + else if (percent >= 0 && percent <= 100) { + return true + } else { + log.warn "$percent is not 0-100" + return false + } +} \ No newline at end of file