Update notify-me-with-hue.groovy
[smartapps.git] / official / vinli-home-connect.groovy
1 /**
2  *  Vinli Home Beta
3  *
4  *  Copyright 2015 Daniel
5  *
6  */
7 definition(
8     name: "Vinli Home Connect",
9     namespace: "com.vinli.smartthings",
10     author: "Daniel",
11     description: "Allows Vinli users to connect their car to SmartThings",
12     category: "SmartThings Labs",
13     iconUrl: "https://d3azp77rte0gip.cloudfront.net/smartapps/baeb2e5d-ebd0-49fe-a4ec-e92417ae20bb/images/vinli_oauth_60.png",
14     iconX2Url: "https://d3azp77rte0gip.cloudfront.net/smartapps/baeb2e5d-ebd0-49fe-a4ec-e92417ae20bb/images/vinli_oauth_120.png",
15     iconX3Url: "https://d3azp77rte0gip.cloudfront.net/smartapps/baeb2e5d-ebd0-49fe-a4ec-e92417ae20bb/images/vinli_oauth_120.png",
16     oauth: true)
17   
18 preferences {
19   section ("Allow external service to control these things...") {
20     input "switches", "capability.switch", multiple: true, required: true
21     input "locks", "capability.lock", multiple: true, required: true
22   }
23 }
24
25 mappings {
26   
27   path("/devices") {
28     action: [
29       GET: "listAllDevices"
30     ]
31   }
32         
33   path("/switches") {
34     action: [
35       GET: "listSwitches"
36     ]
37   }
38   path("/switches/:command") {
39     action: [
40       PUT: "updateSwitches"
41     ]
42   }
43   path("/switches/:id/:command") {
44     action: [
45       PUT: "updateSwitch"
46     ]
47   }
48    path("/locks/:command") {
49     action: [
50       PUT: "updateLocks"
51     ]
52   }
53   path("/locks/:id/:command") {
54     action: [
55       PUT: "updateLock"
56     ]
57   }
58   
59    path("/devices/:id/:command") {
60     action: [
61       PUT: "commandDevice"
62     ]
63   }
64 }
65
66 // returns a list of all devices
67 def listAllDevices() {
68         def resp = []
69         switches.each {
70       resp << [name: it.name, label: it.label, value: it.currentValue("switch"), type: "switch", id: it.id, hub: it.hub?.name]
71     }
72     
73     locks.each {
74       resp << [name: it.name, label: it.label, value: it.currentValue("lock"), type: "lock", id: it.id, hub: it.hub?.name]
75     }
76     return resp
77 }
78  
79 // returns a list like
80 // [[name: "kitchen lamp", value: "off"], [name: "bathroom", value: "on"]]
81 def listSwitches() {
82     def resp = []
83     switches.each {
84       resp << [name: it.displayName, value: it.currentValue("switch"), type: "switch", id: it.id]
85     }
86     return resp
87 }
88
89 void updateLocks() {
90     // use the built-in request object to get the command parameter
91     def command = params.command
92
93     if (command) {
94
95         // check that the switch supports the specified command
96         // If not, return an error using httpError, providing a HTTP status code.
97         locks.each {
98             if (!it.hasCommand(command)) {
99                 httpError(501, "$command is not a valid command for all switches specified")
100             } 
101         }
102         
103         // all switches have the comand
104         // execute the command on all switches
105         // (note we can do this on the array - the command will be invoked on every element
106         locks."$command"()
107     }
108 }
109
110 void updateLock() {
111     def command = params.command
112     
113     locks.each {
114         if (!it.hasCommand(command)) {
115             httpError(400, "$command is not a valid command for all lock specified")
116         }
117         
118         if (it.id == params.id) {
119             it."$command"()
120         }
121     }
122 }
123
124 void updateSwitch() {
125     def command = params.command
126     
127     switches.each {
128         if (!it.hasCommand(command)) {
129             httpError(400, "$command is not a valid command for all switches specified")
130         }
131         
132         if (it.id == params.id) {
133             it."$command"()
134         }
135     }
136 }
137
138 void commandDevice() {
139     def command = params.command
140     def devices = []
141     
142     switches.each {
143         devices << it
144     }
145     
146     locks.each {
147         devices << it
148     }
149     
150     devices.each {
151         if (it.id == params.id) {
152             if (!it.hasCommand(command)) {
153                 httpError(400, "$command is not a valid command for specified device")
154             }
155             it."$command"()
156         }
157     }
158 }
159
160 void updateSwitches() {
161     // use the built-in request object to get the command parameter
162     def command = params.command
163
164     if (command) {
165
166         // check that the switch supports the specified command
167         // If not, return an error using httpError, providing a HTTP status code.
168         switches.each {
169             if (!it.hasCommand(command)) {
170                 httpError(400, "$command is not a valid command for all switches specified")
171             } 
172         }
173         
174         // all switches have the comand
175         // execute the command on all switches
176         // (note we can do this on the array - the command will be invoked on every element
177         switches."$command"()
178     }
179 }
180
181  def installed() {
182         log.debug "Installed with settings: ${settings}"
183 }
184
185 def updated() {
186         log.debug "Updated with settings: ${settings}"
187
188         unsubscribe()
189 }