Update hue-minimote.groovy
[smartapps.git] / official / 02-control-devices-with-an-HTTP-request.groovy
1 /**
2  *  Example: Control a switch with a contact sensor
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: "Example: Control a device with an API call",
18     namespace: "com.smarthings.developers",
19     author: "Andrew Mager & Kris Schaller",
20     description: "Make an HTTP request to a SmartApp to control devices.",
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
25
26 preferences {
27     section(title: "Select Devices") {
28         input "light", "capability.switch", title: "Select a light or outlet", required: true, multiple:false
29     }
30 }
31
32 // Since the SmartApp doesn't have any dependencies when it's installed or updated,
33 // we don't need to worry about those states.
34 def installed() {}
35 def updated() {}
36
37
38 // This block defines an endpoint, and which functions will fire depending on which type
39 // of HTTP request you send
40 mappings {
41     // The path is appended to the endpoint to make requests
42     path("/switch") {
43         // These actions link HTTP verbs to specific callback functions in your SmartApp
44         action: [
45             GET: "getSwitch", // "When an HTTP GET request is received, run getSwitch()"
46             PUT: "setSwitch"
47         ]
48     }
49 }
50
51
52 // Callback functions
53 def getSwitch() {
54     // This returns the current state of the switch in JSON
55     return light.currentState("switch")
56 }
57
58 def setSwitch() {
59     switch(request.JSON.value) {
60         case "on":
61             light.on();
62             break;
63         case "off":
64             light.off();
65             break;
66         default:
67             break;
68     }
69 }