2 * Copyright 2015 SmartThings
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11 * for the specific language governing permissions and limitations under the License.
19 name: "Alfred Workflow",
20 namespace: "vlaminck",
21 author: "SmartThings",
22 description: "This SmartApp allows you to interact with the things in your physical graph through Alfred.",
23 category: "Convenience",
24 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/alfred-app.png",
25 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/alfred-app@2x.png",
26 oauth: [displayName: "SmartThings Alfred Workflow", displayLink: ""]
30 section("Allow Alfred to Control These Things...") {
31 input "switches", "capability.switch", title: "Which Switches?", multiple: true, required: false
32 input "locks", "capability.lock", title: "Which Locks?", multiple: true, required: false
43 path("/switches/:id") {
68 switches.collect { device(it,"switch") }
70 void updateSwitches() {
74 show(switches, "switch")
81 locks.collect { device(it, "lock") }
93 private void updateAll(devices) {
94 def command = request.JSON?.command
95 def type = params.param1
97 httpError(404, "Devices not found")
101 devices.each { device ->
102 executeCommand(device, type, command)
107 private void update(devices) {
108 log.debug "update, request: ${request.JSON}, params: ${params}, devices: $devices.id"
109 def command = request.JSON?.command
110 def type = params.param1
111 def device = devices?.find { it.id == params.id }
114 httpError(404, "Device not found")
118 executeCommand(device, type, command)
123 * Validating the command passed by the user based on capability.
126 def validateCommand(device, deviceType, command) {
127 def capabilityCommands = getDeviceCapabilityCommands(device.capabilities)
128 def currentDeviceCapability = getCapabilityName(deviceType)
129 if (capabilityCommands[currentDeviceCapability]) {
130 return command in capabilityCommands[currentDeviceCapability] ? true : false
132 // Handling other device types here, which don't accept commands
133 httpError(400, "Bad request.")
138 * Need to get the attribute name to do the lookup. Only
139 * doing it for the device types which accept commands
140 * @return attribute name of the device type
142 def getCapabilityName(type) {
154 * Constructing the map over here of
155 * supported commands by device capability
156 * @return a map of device capability -> supported commands
158 def getDeviceCapabilityCommands(deviceCapabilities) {
160 deviceCapabilities.collect {
161 map[it.name] = it.commands.collect{ it.name.toString() }
167 * Validates and executes the command
168 * on the device or devices
170 def executeCommand(device, type, command) {
171 if (validateCommand(device, type, command)) {
174 httpError(403, "Access denied. This command is not supported by current capability.")
178 private show(devices, name) {
179 def device = devices.find { it.id == params.id }
181 httpError(404, "Device not found")
184 def s = device.currentState(name)
185 [id: device.id, label: device.displayName, name: device.displayName, state: s]
189 private device(it, name) {
191 def s = it.currentState(name)
192 [id: it.id, label: it.displayName, name: it.displayName, state: s]