-
Notifications
You must be signed in to change notification settings - Fork 3
/
myq.js
75 lines (70 loc) · 3.69 KB
/
myq.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module.exports = function (RED) {
function MyqDeviceNode(config) {
RED.nodes.createNode(this, config);
const node = this;
this.user = RED.nodes.getNode(config.user);
const serialNumber = config.serialnumber;
const deviceName = config.name;
const api = this.user.api;
this.username = this.user.username;
this.password = this.user.password;
node.on('input', function (msg, send, done) {
if (!api) {
this.error('No MyQ Api Server was registered!');
} else {
this.debug(`Using MyQ Api Server for user ${this.username}`);
}
(api.accessToken ? api.refreshDevices() : api.login(this.username, this.password))
.then((refreshResult) => {
this.debug(`Result of devices refresh: '${refreshResult}'`);
if (!refreshResult) {
return Promise.reject('Could not refresh devices!');
} else {
this.debug(JSON.stringify(api.devices, null, 2));
const device = api.devices.find(
(d) => (serialNumber && serialNumber === d.serial_number || deviceName === d.name)
);
if (!device) {
return Promise.reject(`No device with either serial number "${serialNumber}" or name "${deviceName}" were found!`);
} else {
if (!msg.payload || msg.payload === '' || msg.payload === 'info') {
this.debug(`Retrieving state for serial number "${serialNumber}" or name "${deviceName}"`);
return Promise.resolve(device);
} else {
this.debug(`Executing command "${msg.payload}" for device door with serial number "${serialNumber}" or name "${deviceName}"`);
return api.execute(device, msg.payload);
}
}
}
})
.then((result) => {
this.debug('MyQ result:');
this.debug(JSON.stringify(result, null, 2));
if (result.state) {
if (result.state.door_state === 'open' || result.state.door_state === 'opening' || result.state.light_state === 'on' || result.state.lamp_state === 'on') {
this.status({ fill: "red", shape: "dot", text: result.state.door_state || result.state.light_state || result.state.lamp_state });
} else if (result.state.door_state === 'closed' || result.state.door_state === 'closing' || result.state.light_state === 'off' || result.state.lamp_state === 'off') {
this.status({ fill: "green", shape: "dot", text: result.state.door_state || result.state.light_state || result.state.lamp_state });
}
}
msg.payload = result;
node.send(msg);
if (done) {
// Node-RED 1.0 compatible
done();
}
})
.catch((error) => {
this.error(error);
if (done) {
// Node-RED 1.0 compatible
done(error);
} else {
// Node-RED 0.x compatible
node.error(error, msg);
}
});
});
}
RED.nodes.registerType("myq-device-node", MyqDeviceNode);
}