@switchbot/homebridge-switchbot 1.15.0 → 1.15.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/config.schema.json +12 -5
  2. package/dist/device/bot.d.ts.map +1 -1
  3. package/dist/device/bot.js +10 -10
  4. package/dist/device/bot.js.map +1 -1
  5. package/dist/device/colorbulb.d.ts +15 -1
  6. package/dist/device/colorbulb.d.ts.map +1 -1
  7. package/dist/device/colorbulb.js +140 -13
  8. package/dist/device/colorbulb.js.map +1 -1
  9. package/dist/device/contact.d.ts.map +1 -1
  10. package/dist/device/contact.js +12 -9
  11. package/dist/device/contact.js.map +1 -1
  12. package/dist/device/curtain.d.ts.map +1 -1
  13. package/dist/device/curtain.js +13 -9
  14. package/dist/device/curtain.js.map +1 -1
  15. package/dist/device/humidifier.d.ts.map +1 -1
  16. package/dist/device/humidifier.js +9 -8
  17. package/dist/device/humidifier.js.map +1 -1
  18. package/dist/device/lock.d.ts.map +1 -1
  19. package/dist/device/lock.js +2 -1
  20. package/dist/device/lock.js.map +1 -1
  21. package/dist/device/meter.d.ts.map +1 -1
  22. package/dist/device/meter.js +29 -18
  23. package/dist/device/meter.js.map +1 -1
  24. package/dist/device/meterplus.d.ts.map +1 -1
  25. package/dist/device/meterplus.js +6 -5
  26. package/dist/device/meterplus.js.map +1 -1
  27. package/dist/device/motion.d.ts.map +1 -1
  28. package/dist/device/motion.js +6 -5
  29. package/dist/device/motion.js.map +1 -1
  30. package/dist/device/plug.d.ts +7 -7
  31. package/dist/device/plug.d.ts.map +1 -1
  32. package/dist/device/plug.js +5 -5
  33. package/dist/device/plug.js.map +1 -1
  34. package/dist/device/striplight.d.ts.map +1 -1
  35. package/dist/device/striplight.js +7 -11
  36. package/dist/device/striplight.js.map +1 -1
  37. package/dist/platform.d.ts +7 -1
  38. package/dist/platform.d.ts.map +1 -1
  39. package/dist/platform.js +75 -13
  40. package/dist/platform.js.map +1 -1
  41. package/dist/settings.d.ts +3 -1
  42. package/dist/settings.d.ts.map +1 -1
  43. package/dist/settings.js +1 -1
  44. package/dist/settings.js.map +1 -1
  45. package/package.json +81 -79
  46. package/src/device/bot.ts +10 -10
  47. package/src/device/colorbulb.ts +169 -18
  48. package/src/device/contact.ts +13 -10
  49. package/src/device/curtain.ts +13 -9
  50. package/src/device/humidifier.ts +9 -8
  51. package/src/device/lock.ts +4 -3
  52. package/src/device/meter.ts +46 -36
  53. package/src/device/meterplus.ts +6 -5
  54. package/src/device/motion.ts +6 -5
  55. package/src/device/plug.ts +13 -13
  56. package/src/device/striplight.ts +8 -14
  57. package/src/platform.ts +81 -13
  58. package/src/settings.ts +3 -1
package/src/platform.ts CHANGED
@@ -18,10 +18,12 @@ import { AirPurifier } from './irdevice/airpurifier';
18
18
  import { WaterHeater } from './irdevice/waterheater';
19
19
  import { VacuumCleaner } from './irdevice/vacuumcleaner';
20
20
  import { AirConditioner } from './irdevice/airconditioner';
21
+ import sha256 from 'crypto-js/sha256';
22
+ import fakegato from 'fakegato-history';
23
+ import { readFileSync, writeFileSync } from 'fs';
21
24
  import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
22
25
  import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, Service, Characteristic } from 'homebridge';
23
26
  import { PLATFORM_NAME, PLUGIN_NAME, DeviceURL, irdevice, device, SwitchBotPlatformConfig, deviceResponses, devicesConfig } from './settings';
24
- import fakegato from 'fakegato-history';
25
27
 
26
28
  /**
27
29
  * HomebridgePlatform
@@ -74,7 +76,10 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
74
76
 
75
77
  // setup axios interceptor to add headers / api key to each request
76
78
  this.axios.interceptors.request.use((request: AxiosRequestConfig) => {
77
- request.headers!.Authorization = this.config.credentials?.openToken;
79
+ request.headers!.Authorization = this.config.credentials?.token;
80
+ request.headers!.sign = String(sha256(this.config.credentials!.token + Date.now(), this.config.credentials!.secret)).toUpperCase();
81
+ request.headers!.t = String(Date.now());
82
+ request.headers!.nonce = '';
78
83
  request.headers!['Content-Type'] = 'application/json; charset=utf8';
79
84
  return request;
80
85
  });
@@ -90,7 +95,11 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
90
95
  this.debugLog('Executed didFinishLaunching callback');
91
96
  // run the method to discover / register your devices as accessories
92
97
  try {
93
- this.discoverDevices();
98
+ if (this.config.credentials?.openToken && !this.config.credentials.token) {
99
+ await this.updateToken();
100
+ } else {
101
+ this.discoverDevices();
102
+ }
94
103
  } catch (e: any) {
95
104
  this.errorLog('Failed to Discover Devices.', JSON.stringify(e.message));
96
105
  if (this.platformLogging?.includes('debug')) {
@@ -134,7 +143,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
134
143
  /**
135
144
  * Verify the config passed to the plugin is valid
136
145
  */
137
- verifyConfig() {
146
+ async verifyConfig() {
138
147
  this.config.options = this.config.options || {};
139
148
 
140
149
  const platformConfig = {};
@@ -181,8 +190,8 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
181
190
  }
182
191
  }
183
192
 
184
- if (this.config.options!.refreshRate! < 30) {
185
- throw new Error('Refresh Rate must be above 30 (30 seconds).');
193
+ if (this.config.options!.refreshRate! < 5) {
194
+ throw new Error('Refresh Rate must be above 5 (5 seconds).');
186
195
  }
187
196
 
188
197
  if (!this.config.options.refreshRate) {
@@ -205,12 +214,71 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
205
214
  this.debugLog('Missing Credentials');
206
215
  }
207
216
  }
208
- if (!this.config.credentials?.openToken) {
217
+ if (!this.config.credentials?.token) {
209
218
  if (this.platformLogging?.includes('debug')) {
210
- this.errorLog('Missing openToken');
219
+ this.errorLog('Missing token');
211
220
  this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
212
221
  }
213
222
  }
223
+ if (this.config.credentials?.token) {
224
+ if (!this.config.credentials?.secret) {
225
+ if (this.platformLogging?.includes('debug')) {
226
+ this.errorLog('Missing secret');
227
+ this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
228
+ }
229
+ throw Error('Missing secret');
230
+ }
231
+ }
232
+ }
233
+ }
234
+
235
+ /**
236
+ * The openToken was old config.
237
+ * This method saves the openToken as the token in the config.json file
238
+ * @param this.config.credentials.openToken
239
+ */
240
+ async updateToken() {
241
+ try {
242
+ // check the new token was provided
243
+ if (!this.config.credentials?.openToken) {
244
+ throw new Error('New token not provided');
245
+ }
246
+
247
+ // load in the current config
248
+ const currentConfig = JSON.parse(readFileSync(this.api.user.configPath(), 'utf8'));
249
+
250
+ // check the platforms section is an array before we do array things on it
251
+ if (!Array.isArray(currentConfig.platforms)) {
252
+ throw new Error('Cannot find platforms array in config');
253
+ }
254
+
255
+ // find this plugins current config
256
+ const pluginConfig = currentConfig.platforms.find((x: { platform: string }) => x.platform === PLATFORM_NAME);
257
+
258
+ if (!pluginConfig) {
259
+ throw new Error(`Cannot find config for ${PLATFORM_NAME} in platforms array`);
260
+ }
261
+
262
+ // check the .credentials is an object before doing object things with it
263
+ if (typeof pluginConfig.credentials !== 'object') {
264
+ throw new Error('pluginConfig.credentials is not an object');
265
+ }
266
+ // Move openToken to token
267
+ this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken.');
268
+ this.warnLog('`openToken` config has been moved to the `token` config, Please restart Homebridge.');
269
+
270
+ // set the refresh token
271
+ pluginConfig.credentials.token = this.config.credentials?.openToken;
272
+ pluginConfig.credentials.openToken = 'Delete';
273
+
274
+ if (this.platformLogging?.includes('debug')) {
275
+ this.warnLog(`Token: ${pluginConfig.credentials.token}`);
276
+ }
277
+ // save the config, ensuring we maintain pretty json
278
+ writeFileSync(this.api.user.configPath(), JSON.stringify(currentConfig, null, 4));
279
+ this.verifyConfig();
280
+ } catch (e: any) {
281
+ this.errorLog(e);
214
282
  }
215
283
  }
216
284
 
@@ -219,7 +287,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
219
287
  */
220
288
  async discoverDevices() {
221
289
  try {
222
- if (this.config.credentials?.openToken) {
290
+ if (this.config.credentials?.token) {
223
291
  const devicesAPI: any = (await this.axios.get(DeviceURL)).data;
224
292
  this.deviceListInfo(devicesAPI);
225
293
  this.debugLog(JSON.stringify(devicesAPI));
@@ -242,7 +310,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
242
310
  this.createDevice(device);
243
311
  }
244
312
  }
245
- } else if (this.config.credentials?.openToken && this.config.options.devices) {
313
+ } else if (this.config.credentials.token && this.config.options.devices) {
246
314
  this.debugLog(`SwitchBot Device Config Set: ${JSON.stringify(this.config.options?.devices)}`);
247
315
  const deviceConfigs = this.config.options?.devices;
248
316
 
@@ -266,7 +334,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
266
334
  }
267
335
  }
268
336
  } else {
269
- this.errorLog('Neither SwitchBot OpenToken or Device Config are not set.');
337
+ this.errorLog('Neither SwitchBot Token or Device Config are not set.');
270
338
  }
271
339
  // IR Devices
272
340
  if (devicesAPI.body.infraredRemoteList.length !== 0) {
@@ -304,7 +372,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
304
372
  }
305
373
  }
306
374
  }
307
- } else if (!this.config.credentials?.openToken && this.config.options?.devices) {
375
+ } else if (!this.config.credentials?.token && this.config.options?.devices) {
308
376
  this.debugLog(`SwitchBot Device Manual Config Set: ${JSON.stringify(this.config.options?.devices)}`);
309
377
  const deviceConfigs = this.config.options?.devices;
310
378
  const devices = deviceConfigs.map((v: any) => v);
@@ -316,7 +384,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
316
384
  }
317
385
  }
318
386
  } else {
319
- this.errorLog('Neither SwitchBot OpenToken or Device Config are not set.');
387
+ this.errorLog('Neither SwitchBot Token or Device Config are not set.');
320
388
  }
321
389
  } catch (e: any) {
322
390
  this.deviceError(e);
package/src/settings.ts CHANGED
@@ -18,7 +18,7 @@ export const AuthURL = 'https://api.switch-bot.com';
18
18
  /**
19
19
  * This is the main url used to access SwitchBot API
20
20
  */
21
- export const DeviceURL = 'https://api.switch-bot.com/v1.0/devices';
21
+ export const DeviceURL = 'https://api.switch-bot.com/v1.1/devices';
22
22
 
23
23
  //Config
24
24
  export interface SwitchBotPlatformConfig extends PlatformConfig {
@@ -27,6 +27,8 @@ export interface SwitchBotPlatformConfig extends PlatformConfig {
27
27
  }
28
28
 
29
29
  export type credentials = {
30
+ token?: any;
31
+ secret?: any;
30
32
  openToken?: any;
31
33
  };
32
34