Renaming app.
[smartapps.git] / official / curb-control.groovy
1 /**
2  *  Curb Control
3  *
4  *  Author: Curb
5  */
6
7 definition(
8     name: "Curb Control",
9     namespace: "Curb",
10     author: "Curb",
11     description: "This SmartApp allows you to interact with the switches in your physical graph through Curb.",
12     category: "Convenience",
13     iconUrl: "http://energycurb.com/images/logo.png",
14     iconX2Url: "http://energycurb.com/images/logo.png",
15     oauth: [displayName: "SmartThings Curb Control", displayLink: "energycurb.com"]
16 )
17
18 preferences {
19         section("Allow Curb to Control These Things...") {
20                 input "switches", "capability.switch", title: "Which Switches?", multiple: true, required: false
21         }
22 }
23
24 mappings {
25     path("/") {
26         action: [
27             GET: "index"
28         ]
29     }
30         path("/switches") {
31                 action: [
32                         GET: "listSwitches",
33                         PUT: "updateSwitches"
34                 ]
35         }
36         path("/switches/:id") {
37                 action: [
38                         GET: "showSwitch",
39                         PUT: "updateSwitch"
40                 ]
41         }
42 }
43
44 def installed() {}
45
46 def updated() {}
47
48 def index(){
49     [[url: "/switches"]]
50 }
51
52 def listSwitches() {
53         switches.collect { device(it,"switch") }
54 }
55 void updateSwitches() {
56         updateAll(switches)
57 }
58 def showSwitch() {
59         show(switches, "switch")
60 }
61 void updateSwitch() {
62         update(switches)
63 }
64
65 private void updateAll(devices) {
66         def command = request.JSON?.command
67         if (command) {
68                 switch(command) {
69                         case "on":
70                                 devices.on()
71                                 break
72                         case "off":
73                                 devices.off()
74                                 break
75                         default:
76                                 httpError(403, "Access denied. This command is not supported by current capability.")
77                 }
78         }
79 }
80
81 private void update(devices) {
82         log.debug "update, request: ${request.JSON}, params: ${params}, devices: $devices.id"
83         def command = request.JSON?.command
84         if (command) {
85                 def device = devices.find { it.id == params.id }
86                 if (!device) {
87                         httpError(404, "Device not found")
88                 } else {
89                         switch(command) {
90                                 case "on":
91                                         device.on()
92                                         break
93                                 case "off":
94                                         device.off()
95                                         break
96                                 default:
97                                         httpError(403, "Access denied. This command is not supported by current capability.")
98                         }
99                 }
100         }
101 }
102
103 private show(devices, name) {
104         def d = devices.find { it.id == params.id }
105         if (!d) {
106                 httpError(404, "Device not found")
107         }
108         else {
109         device(d, name)
110         }
111 }
112
113 private device(it, name){
114     if(it) {
115                 def s = it.currentState(name)
116                 [id: it.id, label: it.displayName, name: it.displayName, state: s]    
117     }
118 }