4 * Copyright 2014 SmartThings, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13 * for the specific language governing permissions and limitations under the License.
22 namespace: "smartthings",
23 author: "SmartThings",
24 description: "Set your lighting by rotating a cube containing a SmartSense Multi",
25 category: "SmartThings Labs",
26 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/App-LightUpMyWorld.png",
27 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/App-LightUpMyWorld@2x.png"
34 page(name: "mainPage", title: "", nextPage: "scenesPage", uninstall: true) {
35 section("Use the orientation of this cube") {
36 input "cube", "capability.threeAxis", required: false, title: "SmartSense Multi sensor"
38 section("To control these lights") {
39 input "lights", "capability.switch", multiple: true, required: false, title: "Lights, switches & dimmers"
41 section([title: " ", mobileOnly:true]) {
42 label title: "Assign a name", required: false
43 mode title: "Set for specific mode(s)", required: false
46 page(name: "scenesPage", title: "Scenes", install: true, uninstall: true)
47 page(name: "scenePage", title: "Scene", install: false, uninstall: false, previousPage: "scenesPage")
48 page(name: "devicePage", install: false, uninstall: false, previousPage: "scenePage")
49 page(name: "saveStatesPage", install: false, uninstall: false, previousPage: "scenePage")
54 log.debug "scenesPage()"
55 def sceneId = getOrientation()
56 dynamicPage(name:"scenesPage") {
59 href "scenePage", title: "${num}. ${sceneName(num)}${sceneId==num ? ' (current)' : ''}", params: [sceneId:num], description: "", state: sceneIsDefined(num) ? "complete" : "incomplete"
63 href "scenesPage", title: "Refresh", description: ""
68 def scenePage(params=[:]) {
69 log.debug "scenePage($params)"
70 def currentSceneId = getOrientation()
71 def sceneId = params.sceneId as Integer ?: state.lastDisplayedSceneId
72 state.lastDisplayedSceneId = sceneId
73 dynamicPage(name:"scenePage", title: "${sceneId}. ${sceneName(sceneId)}") {
75 input "sceneName${sceneId}", "text", title: "Scene Name", required: false
79 href "devicePage", title: "Show Device States", params: [sceneId:sceneId], description: "", state: sceneIsDefined(sceneId) ? "complete" : "incomplete"
83 href "saveStatesPage", title: "Record Current Device States", params: [sceneId:sceneId], description: ""
88 def devicePage(params) {
89 log.debug "devicePage($params)"
91 getDeviceCapabilities()
93 def sceneId = params.sceneId as Integer ?: state.lastDisplayedSceneId
95 dynamicPage(name:"devicePage", title: "${sceneId}. ${sceneName(sceneId)} Device States") {
98 input "onoff_${sceneId}_${light.id}", "boolean", title: light.displayName
103 lights.each {light ->
104 if (state.lightCapabilities[light.id] in ["level", "color"]) {
105 input "level_${sceneId}_${light.id}", "enum", title: light.displayName, options: levels, description: "", required: false
110 section("Colors (hue/saturation)") {
111 lights.each {light ->
112 if (state.lightCapabilities[light.id] == "color") {
113 input "color_${sceneId}_${light.id}", "text", title: light.displayName, description: "", required: false
120 def saveStatesPage(params) {
126 /*************************
127 * Installation & update *
128 *************************/
130 log.debug "Installed with settings: ${settings}"
136 log.debug "Updated with settings: ${settings}"
143 subscribe cube, "threeAxis", positionHandler
150 def positionHandler(evt) {
152 def sceneId = getOrientation(evt.xyzValue)
153 log.trace "orientation: $sceneId"
155 if (sceneId != state.lastActiveSceneId) {
156 restoreStates(sceneId)
159 log.trace "No status change"
161 state.lastActiveSceneId = sceneId
168 private Boolean sceneIsDefined(sceneId) {
169 def tgt = "onoff_${sceneId}".toString()
170 settings.find{it.key.startsWith(tgt)} != null
173 private updateSetting(name, value) {
174 app.updateSetting(name, value)
175 settings[name] = value
178 private closestLevel(level) {
179 level ? "${Math.round(level/5) * 5}%" : "0%"
182 private saveStates(params) {
183 log.trace "saveStates($params)"
184 def sceneId = params.sceneId as Integer
185 getDeviceCapabilities()
187 lights.each {light ->
188 def type = state.lightCapabilities[light.id]
190 updateSetting("onoff_${sceneId}_${light.id}", light.currentValue("switch") == "on")
192 if (type == "level") {
193 updateSetting("level_${sceneId}_${light.id}", closestLevel(light.currentValue('level')))
195 else if (type == "color") {
196 updateSetting("level_${sceneId}_${light.id}", closestLevel(light.currentValue('level')))
197 updateSetting("color_${sceneId}_${light.id}", "${light.currentValue("hue")}/${light.currentValue("saturation")}")
203 private restoreStates(sceneId) {
204 log.trace "restoreStates($sceneId)"
205 getDeviceCapabilities()
207 lights.each {light ->
208 def type = state.lightCapabilities[light.id]
210 def isOn = settings."onoff_${sceneId}_${light.id}" == "true" ? true : false
211 log.debug "${light.displayName} is '$isOn'"
219 if (type != "switch") {
220 def level = switchLevel(sceneId, light)
222 if (type == "level") {
223 log.debug "${light.displayName} level is '$level'"
225 light.setLevel(level)
228 else if (type == "color") {
229 def segs = settings."color_${sceneId}_${light.id}"?.split("/")
230 if (segs?.size() == 2) {
231 def hue = segs[0].toInteger()
232 def saturation = segs[1].toInteger()
233 log.debug "${light.displayName} color is level: $level, hue: $hue, sat: $saturation"
235 light.setColor(level: level, hue: hue, saturation: saturation)
238 light.setColor(hue: hue, saturation: saturation)
242 log.debug "${light.displayName} level is '$level'"
244 light.setLevel(level)
249 log.error "Unknown type '$type'"
257 private switchLevel(sceneId, light) {
258 def percent = settings."level_${sceneId}_${light.id}"
260 percent[0..-2].toInteger()
267 private getDeviceCapabilities() {
270 if (it.hasCapability("Color Control")) {
271 caps[it.id] = "color"
273 else if (it.hasCapability("Switch Level")) {
274 caps[it.id] = "level"
277 caps[it.id] = "switch"
280 state.lightCapabilities = caps
283 private getLevels() {
285 for (int i = 0; i <= 100; i += 5) {
291 private getOrientation(xyz=null) {
292 final threshold = 250
294 def value = xyz ?: cube.currentValue("threeAxis")
296 def x = Math.abs(value.x) > threshold ? (value.x > 0 ? 1 : -1) : 0
297 def y = Math.abs(value.y) > threshold ? (value.y > 0 ? 1 : -1) : 0
298 def z = Math.abs(value.z) > threshold ? (value.z > 0 ? 1 : -1) : 0
302 if (x == 0 && y == 0) {
307 if (x == 0 && y == 0) {
335 private sceneName(num) {
336 final names = ["UNDEFINED","One","Two","Three","Four","Five","Six"]
337 settings."sceneName${num}" ?: "Scene ${names[num]}"