-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (42 loc) · 1.29 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
vars = require('./vars');
dash_button = require('node-dash-button');
nest = require('unofficial-nest-api');
var device;
var structure;
var dash = dash_button(vars.AMAZON_DASH_BUTTON_MAC);
console.log("Nest API Connecting...");
nest.login(vars.NEST_USER_NAME, vars.NEST_PASSWORD, function (err, data) {
if (err) {
console.log(err.message);
process.exit(1);
return;
}
console.log('Logged in!');
listenForDash();
});
function fetchNestStatus(callback) {
nest.fetchStatus(function (data) {
structure = data.structure[vars.NEST_STRUCTURE_ID];
for (var deviceId in data.device) {
if (data.device.hasOwnProperty(deviceId)) {
device = data.shared[deviceId];
}
}
callback();
});
}
function listenForDash() {
console.log("Listening For Dash Button...");
dash.on("detected", function () {
console.log("Button Pressed!");
fetchNestStatus(function () {
console.log("Current Temp: " + nest.ctof(device.current_temperature));
if (structure.away) {
console.log("Nest is set to Away. Setting to home.");
nest.setHome();
} else {
console.log("Nest is set to home.");
}
});
});
}