forked from joppuyo/homebridge-dyson-link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (197 loc) · 10.3 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"use strict";
const https = require('https');
const crypto = require('crypto');
const DysonLinkAccessoryModule = require("./DysonLinkAccessory");
const DysonLinkDevice = require("./DysonLinkDevice").DysonLinkDevice;
const DysonLinkAccessory = DysonLinkAccessoryModule.DysonLinkAccessory;
var Accessory, Service, Characteristic, UUIDGen;
module.exports = function(homebridge) {
console.log("homebridge API version: " + homebridge.version);
// Accessory must be created from PlatformAccessory Constructor
Accessory = homebridge.platformAccessory;
// Service and Characteristic are from hap-nodejs
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
DysonLinkAccessoryModule.setHomebridge(homebridge);
// For platform plugin to be considered as dynamic platform plugin,
// registerPlatform(pluginName, platformName, constructor, dynamic), dynamic must be true
homebridge.registerPlatform("homebridge-dyson-link", "DysonPlatform", DysonPlatform, true);
}
class DysonPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.accessories = [];
if (api) {
// Save the API object as plugin needs to register new accessory via this object.
this.api = api;
var platform = this;
// Listen to event "didFinishLaunching", this means homebridge already finished loading cached accessories
// Platform Plugin should only register new accessory that doesn't exist in homebridge after this event.
// Or start discover new accessories
this.api.on('didFinishLaunching', () => {
platform.log("Finished launching. Start to create accessory from config");
// Check if the accessories is null as this may be called from second instance of homebrdige too
if (this.config && this.config.accessories) {
let accountPassword = this.config.password || process.env.DYSON_PASSWORD;
let accountEmail = this.config.email || process.env.DYSON_EMAIL;
this.getDevicesFromAccount(accountEmail, accountPassword, config.country, (accountDevices) => {
this.config.accessories.forEach((accessory) => {
let nightModeVisible = accessory.nightModeVisible;
if(nightModeVisible == null || nightModeVisible == undefined) {
platform.log.debug("no night mode visible value, default to true");
nightModeVisible = true;
}
let focusModeVisible = accessory.focusModeVisible;
if(focusModeVisible == null || focusModeVisible == undefined) {
platform.log.debug("no focus mode visible value, default to true");
focusModeVisible = true;
}
let autoModeVisible = accessory.autoModeVisible;
if(autoModeVisible == null || autoModeVisible == undefined) {
platform.log.debug("no auto mode visible value, default to true");
autoModeVisible = true;
}
let deviceInfo = accountDevices[accessory.serialNumber];
var password = ''
if (deviceInfo) {
password = deviceInfo.password;
accessory.serialNumber = 'DYSON-'+accessory.serialNumber+'-'+deviceInfo.ProductType;
}
else {
password = crypto.createHash('sha512').update(accessory.password, "utf8").digest("base64");
}
platform.log(accessory.displayName + " IP:" + accessory.ip + " Serial Number:" + accessory.serialNumber);
let device = new DysonLinkDevice(accessory.displayName, accessory.ip, accessory.serialNumber, password, platform.log);
if (device.valid) {
platform.log("Device serial number format valids");
let uuid = UUIDGen.generate(accessory.serialNumber);
// Check if the accessory got cached
let cachedAccessory = platform.accessories.find((item) => item.UUID === uuid);
if (!cachedAccessory) {
platform.log("Device not cached. Create a new one");
let dysonAccessory = new Accessory(accessory.displayName, uuid);
new DysonLinkAccessory(accessory.displayName, device, dysonAccessory, platform.log, nightModeVisible, focusModeVisible, autoModeVisible);
platform.api.registerPlatformAccessories("homebridge-dyson-link", "DysonPlatform", [dysonAccessory]);
platform.accessories.push(accessory);
} else {
platform.log("Device cached. Try to update this");
cachedAccessory.displayName = accessory.displayName;
new DysonLinkAccessory(accessory.displayName, device, cachedAccessory, platform.log, nightModeVisible, focusModeVisible, autoModeVisible);
platform.api.updatePlatformAccessories([cachedAccessory]);
}
}
});
});
}
else{
platform.log.error("Unable to find config or accessories");
}
});
}
}
configureAccessory(accessory) {
this.log(accessory.displayName, "Configure Accessory");
accessory.reachable = true;
accessory.on('identify', (paired, callback) => {
this.log(accessory.displayName, "Identify!!!");
callback();
});
this.accessories.push(accessory);
}
getDevicesFromAccount(email, password, country, callback) {
if (!email || !password) {
this.log("Dyson email/pass not found, v2 devices may not work")
callback({});
return;
}
// Adapted from: https://github.com/CharlesBlonde/libpurecoollink/blob/master/libpurecoollink/utils.py
const decryptPassword = (encryptedPassword) => {
let key = Uint8Array.from(Array(32), (val, index) => index + 1);
let init_vector = new Uint8Array(16);
var decipher = crypto.createDecipheriv('aes-256-cbc', key, init_vector);
var decryptedPassword = decipher.update(encryptedPassword, 'base64', 'utf8');
decryptedPassword = decryptedPassword + decipher.final('utf8');
return decryptedPassword
};
if (!country) {
country = "US"
}
let postData = {
Email: email,
Password: password
};
let postBody = JSON.stringify(postData);
let DYSON_API_URL = "api.cp.dyson.com";
var options = {
hostname: DYSON_API_URL,
port: 443,
path: '/v1/userregistration/authenticate?country=' + country,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postBody.length
},
rejectUnauthorized: false
};
// Initial request with email/pass to get authorization tokens of Account and Password
var req = https.request(options, (res) => {
var data = "";
res.setEncoding('utf-8');
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
if (!data) {
this.log.error("Could not login to Dyson")
callback({});
return;
}
let credentials = JSON.parse(data);
let account = credentials.Account;
let password = credentials.Password;
let auth = 'Basic ' + Buffer.from(account + ':' + password).toString('base64');
var options = {
hostname: DYSON_API_URL,
port: 443,
path: '/v2/provisioningservice/manifest',
headers: {
"Authorization": auth
},
rejectUnauthorized: false
};
// Request devices in user's account to get local credentials
var req = https.get(options, (res) => {
var data = "";
res.setEncoding('utf-8');
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
if (!data || data.length == 0) {
this.log.error("Could not login to Dyson");
callback({});
return;
}
let devices = JSON.parse(data);
var devicesBySerial = {};
devices.forEach((device) => {
if (device.LocalCredentials) {
let decrypted = JSON.parse(decryptPassword(device.LocalCredentials));
device.password = decrypted.apPasswordHash;
devicesBySerial[device.Serial] = device
}
});
callback(devicesBySerial);
});
});
req.on('error', function(err) {
this.log.error("Error logging in, check Dyson email, password, and country - "+err);
});
req.end();
});
});
req.on('error', function(err) {
this.log.error("Error logging in, check Dyson email, password, and country - "+err);
});
req.write(postBody);
req.end();
}
}