smartthings.groovy
.
21 |
22 | ## Shards
23 |
24 | If you are having trouble connecting to SmartThings, you may have to change your shard.
25 | Add to `smartthings.json`
26 |
27 | "api_location": "smartthings.groovy
and paste
68 | * Press *Save*
69 | * Press *Publish*
70 |
71 | access\_token
.
76 | Normally this is a real pain in the arse to generate, but we've
77 | done the hard work.
78 |
79 | You'll need the OAuth Client ID
and OAuth Client Secret
,
80 | which you copied from the last major step. If you didn't, you can go back
81 | and get them now.
82 |
83 | * Go to this page: https://iotdb.org/playground/oauthorize/smartthings
84 | * Fill in the form
85 | * Press *Submit*
86 | * You'll be brought to the SmartThings website, where you'll allow access to your stuff
87 | * When that's done, you'll be brought back to IOTDB OAuthorize
88 | * copy the JSON data displayed and save it in a file called smartthings.json
89 | in the same directory
90 |
91 | Keep that file to yourself. If you need to turn off access to your things, you can
92 | just go generate new OAuth keys in SmartThings.
93 |
94 | If you add new devices to your SmartThings setup, you can give the API access using
95 | your SmartThings phone app.
96 |
97 | ## API Access
98 | ### Python
99 | #### List all the motion detectors
100 |
101 | $ python smartthings.py --type motion
102 | [
103 | {
104 | "id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
105 | "label": "Motion LivingRoom",
106 | "type": "motion",
107 | "url": "http://graph.api.smartthings.com/api/smartapps/installations/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/motion/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
108 | "value": {
109 | "motion": false,
110 | "timestamp": "2014-02-03T13:30:31.361Z"
111 | }
112 | }
113 | ]
114 |
115 | #### Turn off a switch
116 |
117 | $ python smartthings.py --type switch --id "My Z-Wave Switch" --request switch=0
118 |
119 | #### Toggle a switch
120 |
121 | $ python smartthings.py --type switch --id "My Z-Wave Switch" --request switch=-1
122 |
123 | ### Node
124 |
125 | A work in progress. The code is solid, but there's no command line argument parsing
126 |
--------------------------------------------------------------------------------
/images/ST1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dpjanes/iotdb-smartthings/407fb412186c7d73fe4fc349c3e31d2b12efb9be/images/ST1.png
--------------------------------------------------------------------------------
/images/ST2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dpjanes/iotdb-smartthings/407fb412186c7d73fe4fc349c3e31d2b12efb9be/images/ST2.png
--------------------------------------------------------------------------------
/images/ST3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dpjanes/iotdb-smartthings/407fb412186c7d73fe4fc349c3e31d2b12efb9be/images/ST3.png
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "author": "David P. Janes 48 | * See the documentation, but briefly you can get it from here: 49 | * {@link https://iotdb.org/playground/oauthorize} 50 | */ 51 | SmartThings.prototype.load_settings = function (filename) { 52 | var self = this; 53 | 54 | if (!filename) { 55 | filename = "smartthings.json"; 56 | } 57 | 58 | var data = fs.readFileSync(filename, 'utf8'); 59 | self.std = JSON.parse(data); 60 | }; 61 | 62 | /** 63 | * Get the endpoints exposed by the SmartThings App. 64 | * 65 | *
66 | * When the endpoints are found, we emit an 'endpoint' event. 67 | * The endpoint itself will be stored in the SmartThings object 68 | * 69 | *
70 | * The first command you need to call 71 | */ 72 | SmartThings.prototype.request_endpoint = function () { 73 | var self = this; 74 | 75 | var endpoints_url = self.std["api"]; 76 | var endpoints_paramd = { 77 | "access_token": self.std["access_token"] 78 | }; 79 | 80 | unirest 81 | .get(endpoints_url) 82 | .query(endpoints_paramd) 83 | .end(function (result) { 84 | if (!result.ok) { 85 | console.log("SmartThings.request_endpoints", "something went wrong", result); 86 | return; 87 | } 88 | 89 | self.endpointd = result.body[0]; 90 | self.emit("endpoint", self); 91 | }); 92 | }; 93 | 94 | /** 95 | * Get devices. 96 | *
97 | * When devices are found, we emit a "devices" event
98 | *
99 | * @param {string} device_type
100 | * The type of device to request, i.e.
101 | * switch, motion, acceleration, contact, presence
102 | */
103 | SmartThings.prototype.request_devices = function (device_type) {
104 | var self = this;
105 |
106 | if (!self.endpointd.url) {
107 | console.log("SmartThings.request_devices: no endpoint? Perhaps .request_endpoint has not been called");
108 | return;
109 | }
110 |
111 | var devices_url = "https://graph.api.smartthings.com" + self.endpointd.url + "/" + device_type;
112 | var devices_paramd = {};
113 | var devices_headerd = {
114 | "Authorization": "Bearer " + self.std["access_token"]
115 | };
116 |
117 | unirest
118 | .get(devices_url)
119 | .query(devices_paramd)
120 | .headers(devices_headerd)
121 | .end(function (result) {
122 | if (!result.ok) {
123 | console.log("SmartThings.request_devices", "something went wrong",
124 | "\n url=", devices_url,
125 | "\n error=", result.error,
126 | "\n body=", result.body
127 | );
128 | return;
129 | }
130 |
131 | self.emit("devices", device_type, result.body);
132 | });
133 | };
134 |
135 | /**
136 | * Send a request to a device
137 | *
138 | * @param {dictionary} device
139 | * A device dictionary, as found by {@link request_devices}
140 | *
141 | * @param {dictionary} requestd
142 | * A request dictinary, for example something like
143 | * { switch: 1 }
144 | */
145 | SmartThings.prototype.device_request = function (deviced, requestd, callback) {
146 | var self = this;
147 |
148 | if (!callback) {
149 | callback = function () {};
150 | }
151 |
152 | if (!self.endpointd.url) {
153 | console.log("SmartThings.device_request: no endpoint? Perhaps .request_endpoint has not been called");
154 | return;
155 | }
156 |
157 | var devices_url = "https://graph.api.smartthings.com" + self.endpointd.url + "/" + deviced.type + "/" + deviced.id;
158 | var devices_headerd = {
159 | "Authorization": "Bearer " + self.std["access_token"]
160 | };
161 |
162 | unirest
163 | .put(devices_url)
164 | .type('json')
165 | .send(requestd)
166 | .headers(devices_headerd)
167 | .end(function (result) {
168 | if (!result.ok) {
169 | callback(result.error, deviced, null);
170 |
171 | console.log("SmartThings.device_request", "something went wrong",
172 | "\n url=", devices_url,
173 | "\n error=", result.error,
174 | "\n body=", result.body
175 | );
176 | return;
177 | }
178 |
179 | self.emit("request", deviced);
180 | callback(null, deviced, result.body);
181 |
182 | });
183 | };
184 |
185 | /**
186 | * Request device's state
187 | *
188 | * @param {dictionary} device
189 | * A device dictionary, as found by {@link request_devices}
190 | */
191 | SmartThings.prototype.device_poll = function (deviced, callback) {
192 | var self = this;
193 |
194 | if (!self.endpointd.url) {
195 | console.log("SmartThings.device_request: no endpoint? Perhaps .request_endpoint has not been called");
196 | return;
197 | }
198 |
199 | var devices_url = "https://graph.api.smartthings.com" + self.endpointd.url + "/" + deviced.type + "/" + deviced.id;
200 | var devices_headerd = {
201 | "Authorization": "Bearer " + self.std["access_token"]
202 | };
203 |
204 | unirest
205 | .get(devices_url)
206 | .type('json')
207 | .headers(devices_headerd)
208 | .end(function (result) {
209 | if (!result.ok) {
210 | callback(result.error, deviced, null);
211 |
212 | console.log("SmartThings.device_request", "something went wrong",
213 | "\n url=", devices_url,
214 | "\n error=", result.error,
215 | "\n body=", result.body
216 | );
217 | return;
218 | }
219 |
220 | self.emit("device", result.body);
221 | callback(null, deviced, result.body);
222 | });
223 | };
224 |
225 | exports.SmartThings = SmartThings;
226 | exports.device_types = [
227 | "switch",
228 | "motion",
229 | "presence",
230 | "acceleration",
231 | "contact",
232 | "temperature",
233 | "battery",
234 | "threeAxis",
235 | ];
236 |
--------------------------------------------------------------------------------