4 * Copyright 2015 Jesse Newland
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.
18 namespace: "jnewland",
19 author: "Jesse Newland",
20 description: "A JSON API for SmartThings",
21 category: "SmartThings Labs",
22 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
24 iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
38 if (!state.accessToken) {
44 page(name: "copyConfig")
48 if (!state.accessToken) {
51 dynamicPage(name: "copyConfig", title: "Config", install:true) {
52 section("Select devices to include in the /devices API call") {
53 input "switches", "capability.switch", title: "Switches", multiple: true, required: false
54 input "hues", "capability.colorControl", title: "Hues", multiple: true, required: false
58 paragraph "View this SmartApp's configuration to use it in other places."
59 href url:"https://graph.api.smartthings.com/api/smartapps/installations/${app.id}/config?access_token=${state.accessToken}", style:"embedded", required:false, title:"Config", description:"Tap, select, copy, then click \"Done\""
63 href url:"https://graph.api.smartthings.com/api/smartapps/installations/${app.id}/devices?access_token=${state.accessToken}", style:"embedded", required:false, title:"Debug", description:"View accessories JSON"
69 def configJson = new groovy.json.JsonOutput().toJson([
70 description: "JSON API",
73 platform: "SmartThings",
76 access_token: state.accessToken
81 def configString = new groovy.json.JsonOutput().prettyPrint(configJson)
82 render contentType: "text/plain", data: configString
85 def deviceCommandMap(device, type) {
86 device.supportedCommands.collectEntries { command->
87 def commandUrl = "https://graph.api.smartthings.com/api/smartapps/installations/${app.id}/${type}/${device.id}/command/${command.name}?access_token=${state.accessToken}"
89 (command.name): commandUrl
94 def authorizedDevices() {
101 def renderDevices() {
102 def deviceData = authorizedDevices().collectEntries { devices->
104 (devices.key): devices.value.collect { device->
106 name: device.displayName,
107 commands: deviceCommandMap(device, devices.key)
112 def deviceJson = new groovy.json.JsonOutput().toJson(deviceData)
113 def deviceString = new groovy.json.JsonOutput().prettyPrint(deviceJson)
114 render contentType: "application/json", data: deviceString
117 def deviceCommand() {
118 def device = authorizedDevices()[params.type].find { it.id == params.id }
119 def command = params.command
121 httpError(404, "Device not found")
124 device."$command"(params.value)
132 if (!params.access_token || (params.access_token && params.access_token != state.accessToken)) {
133 path("/devices") { action: [GET: "authError"] }
134 path("/config") { action: [GET: "authError"] }
135 path("/:type/:id/command/:command") { action: [PUT: "authError"] }
137 path("/devices") { action: [GET: "renderDevices"] }
138 path("/config") { action: [GET: "renderConfig"] }
139 path("/:type/:id/command/:command") { action: [PUT: "deviceCommand"] }
144 [error: "Permission denied"]