Update hue-mood-lighting.groovy
[smartapps.git] / official / restful-switch.groovy
1 /**
2  *  Control a Switch with an API call
3  *
4  *  Copyright 2015 SmartThings
5  *
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:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
14  *
15  */
16 definition(
17     name: "Control a Switch with an API call",
18     namespace: "smartthings",
19     author: "SmartThings",
20     description: "V2 of 'RESTful Switch' example. Trying to make OAuth work properly.",
21     category: "My Apps",
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     oauth: true)
25
26 preferences {
27     section("which switches?") {
28         input "theSwitches", "capability.switch", multiple: true
29     }
30 }
31
32 mappings {
33   path("/switches") {
34     // GET requests to /switches endpoint go to listSwitches.
35     // PUT requests go to updateSwitches
36     action: [
37       GET: "getSwitches",
38       PUT: "updateSwitches"
39     ]
40   }
41   
42   // GET requests to endpoint /switches/<id> go to getSwitch
43   // PUT requests to endpoint /switches/<id> go to updateSwitch
44   path("/switches/:id") {
45     action: [
46         GET: "getSwitch",
47         PUT: "updateSwitch"
48     ]
49   }
50 }
51
52 // return a map in the form of [switchName, switchStatus]
53 // the returned value will be converted to JSON by the platform
54 def getSwitches() {
55     def status = [:]
56     theSwitches.each {theSwitch ->
57         log.trace "will populate status map"
58         log.trace "theSwitch id: ${theSwitch.id}"
59         status.put(theSwitch.displayName, theSwitch.currentSwitch)
60     }
61     
62     log.debug "listSwitches returning: $status"
63     return status
64 }
65
66 def getSwitch() {
67     def theSwitch = theSwitches.find{it.id == params.id}
68     [theSwitch.displayName, theSwitch.currentSwitch]
69 }
70
71 // execute the command specified in the request
72 // returns a 400 error if a non-supported command
73 // is specified (only on, off, or toggle supported)
74 // assumes request body with JSON in format {"command" : "<value>"}
75 def updateSwitches() {
76     log.trace "updateSwitches: request: $request"
77     log.trace "updateSwitches: params: $params"
78     
79     theSwitches.each {
80         doCommand(it, request.JSON.command)
81     }
82 }
83
84 // execute the command specified in the request
85 // return a 400 error if a non-supported command 
86 // is specified (only on, off, or toggle supported)
87 // assumes request body with JSON in format {"command" : "<value>"}
88 def updateSwitch() {
89     log.trace "updateSwitch: look for swithc with id ${params.id}"
90     def theSwitch = theSwitches.find{it.id == params.id}
91     doCommand(theSwitch, request.JSON.command)
92 }
93
94 def doCommand(theSwitch, command) {
95     if (command == "toggle") {
96         if (theSwitch.currentSwitch == "on") {
97             log.debug "will try and turn switch ${theSwitch.displayName} on"
98             theSwitch.off()
99         } else {
100             log.debug "will try and turn switch ${theSwitch.displayName} off"
101             theSwitch.on()
102         }
103     } else if (command == "on" || command == "off") {
104         theSwitch."$command"()
105     } else {
106         httpError(400, "Unsupported command - only 'toggle', 'off', and 'on' supported")
107     }
108 }
109
110 // called when SmartApp is installed
111 def installed() {
112     log.debug "Installed with settings: ${settings}"
113 }
114
115 // called when any preferences are changed in this SmartApp. 
116 def updated() {
117     log.debug "Updated with settings: ${settings}"
118     unsubscribe()
119 }