homey-api 1.6.1 → 1.7.0

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.
@@ -35,7 +35,7 @@ and login on that user's Homey.`;
35
35
  const AthomCloudAPI = require('homey-api/lib/AthomCloudAPI');
36
36
 
37
37
  // Create an AthomCloudAPI instance
38
- const api = new AthomCloudAPI({
38
+ const cloudApi = new {@link AthomCloudAPI AthomCloudAPI}({
39
39
  clientId: '5a8d4ca6eb9f7a2c9d6ccf6d',
40
40
  clientSecret: 'e3ace394af9f615857ceaa61b053f966ddcfb12a',
41
41
  redirectUrl: 'http://localhost',
@@ -43,30 +43,38 @@ const api = new AthomCloudAPI({
43
43
 
44
44
  // Check if we're logged in
45
45
  // If not, redirect the user to the OAuth2 dialog
46
- const loggedIn = await api.isLoggedIn();
46
+ const loggedIn = await {@link AthomCloudAPI cloudApi}.{@link AthomCloudAPI#isLoggedIn isLoggedIn}();
47
47
  if (!loggedIn) {
48
- if (api.hasAuthorizationCode()) {
49
- const token = await api.authenticateWithAuthorizationCode();
48
+ if ({@link AthomCloudAPI cloudApi}.{@link AthomCloudAPI#hasAuthorizationCode hasAuthorizationCode}()) {
49
+ const token = await {@link AthomCloudAPI cloudApi}.{@link AthomCloudAPI#authenticateWithAuthorizationCode authenticateWithAuthorizationCode}();
50
50
  } else {
51
- window.location.href = api.getLoginUrl();
51
+ window.location.href = {@link AthomCloudAPI cloudApi}.{@link AthomCloudAPI#getLoginUrl getLoginUrl}();
52
52
  return;
53
53
  }
54
54
  }
55
55
 
56
56
  // Get the logged in user
57
- const user = await cloudApi.getAuthenticatedUser();
57
+ const user = await {@link AthomCloudAPI cloudApi}.{@link AthomCloudAPI#getAuthenticatedUser getAuthenticatedUser}();
58
58
 
59
59
  // Get the first Homey of the logged in user
60
- const homey = await user.getFirstHomey();
60
+ const homey = await {@link AthomCloudAPI.User user}.{@link AthomCloudAPI.User#getFirstHomey getFirstHomey}();
61
61
 
62
62
  // Create a session on this Homey
63
- const homeyApi = await homey.authenticate();
63
+ const homeyApi = await {@link AthomCloudAPI.Homey homey}.{@link AthomCloudAPI.Homey#authenticate authenticate}();
64
64
 
65
- // Loop all devices
66
- const devices = await homeyApi.devices.getDevices();
67
- for(const device of Object.values(devices)) {
65
+ // Get all Zones from ManagerZones
66
+ const zones = await {@link HomeyAPIV2 homeyApi}.{@link HomeyAPIV2.ManagerZones zones}.{@link HomeyAPIV2.ManagerZones#getZones getZones}();
67
+
68
+ // Get all Devices from ManagerDevices
69
+ const devices = await {@link HomeyAPIV2 homeyApi}.{@link HomeyAPIV2.ManagerDevices devices}.{@link HomeyAPIV2.ManagerDevices#getDevices getDevices}();
70
+
71
+ // Turn all devices on
72
+ for(const {@link HomeyAPIV2.ManagerDevices.Device device} of Object.values(devices)) {
68
73
  // Turn device on
69
- await device.setCapabilityValue({ capabilityId: 'onoff', value: true });
74
+ await {@link HomeyAPIV2.ManagerDevices.Device device}.{@link HomeyAPIV2.ManagerDevices.Device#setCapabilityValue setCapabilityValue}({
75
+ capabilityId: 'onoff',
76
+ value: true,
77
+ });
70
78
  }`;
71
79
 
72
80
  static JSDOC_PARAMS = `
@@ -307,7 +315,7 @@ for(const device of Object.values(devices)) {
307
315
  body: body.toString(),
308
316
  method: 'post',
309
317
  headers: {
310
- 'Authorization': `Basic ${Util.base64(`${this.__clientId}:${this.__clientSecret}`)}`,
318
+ Authorization: `Basic ${Util.base64(`${this.__clientId}:${this.__clientSecret}`)}`,
311
319
  'Content-Type': 'application/x-www-form-urlencoded',
312
320
  },
313
321
  });
@@ -333,6 +341,13 @@ for(const device of Object.values(devices)) {
333
341
  return this.__token;
334
342
  }
335
343
 
344
+ /**
345
+ * Authenticate with an authorization code.
346
+ * @param {Object} [opts]
347
+ * @param {String} opts.code - Default to `?code=...` when in a browser.
348
+ * @param {Boolean} [opts.removeCodeFromHistory=true] - Remove `?code=...` from the URL in the address bar.
349
+ * @returns {Promise<AthomCloudAPI.Token>}
350
+ */
336
351
  async authenticateWithAuthorizationCode({
337
352
  code,
338
353
  removeCodeFromHistory = true,
@@ -362,7 +377,7 @@ for(const device of Object.values(devices)) {
362
377
  body: body.toString(),
363
378
  method: 'post',
364
379
  headers: {
365
- 'Authorization': `Basic ${Util.base64(`${this.__clientId}:${this.__clientSecret}`)}`,
380
+ Authorization: `Basic ${Util.base64(`${this.__clientId}:${this.__clientSecret}`)}`,
366
381
  'Content-Type': 'application/x-www-form-urlencoded',
367
382
  },
368
383
  });
@@ -423,7 +438,7 @@ for(const device of Object.values(devices)) {
423
438
  body: body.toString(),
424
439
  method: 'post',
425
440
  headers: {
426
- 'Authorization': `Basic ${Util.base64(`${this.__clientId}:${this.__clientSecret}`)}`,
441
+ Authorization: `Basic ${Util.base64(`${this.__clientId}:${this.__clientSecret}`)}`,
427
442
  'Content-Type': 'application/x-www-form-urlencoded',
428
443
  },
429
444
  });
@@ -468,7 +483,7 @@ for(const device of Object.values(devices)) {
468
483
  body: body.toString(),
469
484
  method: 'post',
470
485
  headers: {
471
- 'Authorization': `Basic ${Util.base64(`${this.__clientId}:${this.__clientSecret}`)}`,
486
+ Authorization: `Basic ${Util.base64(`${this.__clientId}:${this.__clientSecret}`)}`,
472
487
  'Content-Type': 'application/x-www-form-urlencoded',
473
488
  },
474
489
  });
@@ -504,6 +519,76 @@ for(const device of Object.values(devices)) {
504
519
  return this.__refreshTokenPromise;
505
520
  }
506
521
 
522
+ /**
523
+ * Update the currently authenticated user.
524
+ *
525
+ * @private
526
+ * @param {Object} [opts]
527
+ * @param {String} [opts.firstname]
528
+ * @param {String} [opts.lastname]
529
+ * @param {String} [opts.email]
530
+ * @returns {Promise<AthomCloudAPI.User>}
531
+ */
532
+ async updateUserMe({
533
+ firstname,
534
+ lastname,
535
+ email,
536
+ }) {
537
+ const me = await this.getAuthenticatedUser();
538
+ return this.updateUser({
539
+ id: me._id,
540
+ user: {
541
+ firstname,
542
+ lastname,
543
+ email,
544
+ },
545
+ });
546
+ }
547
+
548
+ /**
549
+ * Update the currently authenticated user's avatar.
550
+ *
551
+ * @private
552
+ * @param {Buffer} imageBuffer Buffer of the new avatat
553
+ * @param {"jpg"|"jpeg"|"png"|"gif"} imageType Type of the new avatar
554
+ * @returns {Promise<Object>}
555
+ */
556
+ async updateUserMeAvatar(imageBuffer, imageType) {
557
+ if (!Buffer.isBuffer(imageBuffer)) {
558
+ throw new Error('Invalid Image. Expected Buffer.');
559
+ }
560
+
561
+ if (!imageType) {
562
+ throw new Error('Missing Image Type');
563
+ }
564
+
565
+ if (!['jpg', 'png', 'gif'].includes(imageType)) {
566
+ throw new Error(`Invalid Image Type: ${imageType}`);
567
+ }
568
+
569
+ if (imageType === 'jpg') {
570
+ imageType = 'jpeg';
571
+ }
572
+
573
+ const me = await this.getAuthenticatedUser();
574
+ const body = Buffer.concat([
575
+ Buffer.from(`--__X_HOMEY_BOUNDARY__\r\nContent-Disposition: form-data; name="avatar"; filename="avatar"\r\nContent-Type: image/${imageType}\r\n\r\n`),
576
+ Buffer.from(imageBuffer),
577
+ Buffer.from('\r\n--__X_HOMEY_BOUNDARY__--\r\n'),
578
+ ]);
579
+
580
+ return this.call({
581
+ method: 'POST',
582
+ path: `/user/${me._id}/avatar`,
583
+ headers: {
584
+ 'Content-Type': 'multipart/form-data; boundary="__X_HOMEY_BOUNDARY__"',
585
+ 'Content-Length': body.length,
586
+ },
587
+ body,
588
+ bodyJSON: false,
589
+ });
590
+ }
591
+
507
592
  }
508
593
 
509
594
  module.exports = AthomCloudAPI;
@@ -24,6 +24,14 @@ class Device extends Item {
24
24
  * @param {number|boolean|string} listener.value
25
25
  * @returns {HomeyAPIV2.ManagerDevices.Device.DeviceCapability}
26
26
  * @function HomeyAPIV2.ManagerDevices.Device#makeCapabilityInstance
27
+ * @example
28
+ *
29
+ * const onOffInstance = device.makeCapabilityInstance('onoff', value => {
30
+ * console.log('Device onoff changed to:', value);
31
+ * });
32
+ *
33
+ * // Turn on
34
+ * onOffInstance.setValue(true).catch(console.error);
27
35
  */
28
36
  makeCapabilityInstance(capabilityId, listener) {
29
37
  this.connect().catch(err => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homey-api",
3
- "version": "1.6.1",
3
+ "version": "1.7.0",
4
4
  "description": "Homey API",
5
5
  "main": "index.js",
6
6
  "types": "assets/types/homey-api.d.ts",
@@ -53,7 +53,7 @@
53
53
  "eslint": "^7.32.0",
54
54
  "eslint-config-athom": "^2.1.1",
55
55
  "fs-extra": "^10.0.0",
56
- "homey-jsdoc-template": "github:athombv/homey-jsdoc-template#1.4",
56
+ "homey-jsdoc-template": "github:athombv/homey-jsdoc-template#1.5.1",
57
57
  "http-server": "^0.12.3",
58
58
  "jsdoc": "^3.6.7",
59
59
  "jsdoc-to-markdown": "^7.1.0",