homey-api 3.4.5 → 3.4.7

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.
@@ -37,27 +37,30 @@ class Item extends EventEmitter {
37
37
  writable: false,
38
38
  });
39
39
 
40
- // Set Homey
41
40
  Object.defineProperty(this, '__homey', {
42
41
  value: homey,
43
42
  enumerable: false,
44
43
  writable: false,
45
44
  });
46
45
 
47
- // Set Manager
48
46
  Object.defineProperty(this, '__manager', {
49
47
  value: manager,
50
48
  enumerable: false,
51
49
  writable: false,
52
50
  });
53
-
54
- // Set Connected
51
+
55
52
  Object.defineProperty(this, '__connected', {
56
53
  value: false,
57
54
  enumerable: false,
58
55
  writable: true,
59
56
  });
60
57
 
58
+ Object.defineProperty(this, '__lastUpdated', {
59
+ value: new Date(),
60
+ enumerable: false,
61
+ writable: true,
62
+ });
63
+
61
64
  // Set Properties
62
65
  for (const [key, value] of Object.entries(properties)) {
63
66
  if (key === 'id') continue;
@@ -97,6 +100,8 @@ class Item extends EventEmitter {
97
100
  this[key] = value;
98
101
  }
99
102
 
103
+ this.__lastUpdated = new Date();
104
+
100
105
  this.emit('update', properties);
101
106
  }
102
107
 
@@ -27,14 +27,12 @@ class Manager extends EventEmitter {
27
27
  }) {
28
28
  super();
29
29
 
30
- // Set Homey
31
30
  Object.defineProperty(this, '__homey', {
32
31
  value: homey,
33
32
  enumerable: false,
34
33
  writable: false,
35
34
  });
36
35
 
37
- // Set Items
38
36
  Object.defineProperty(this, 'itemClasses', {
39
37
  value: Object.entries(items).reduce((obj, [itemName, item]) => {
40
38
  const ItemClass = this.constructor.CRUD[itemName]
@@ -62,14 +60,12 @@ class Manager extends EventEmitter {
62
60
  writable: false,
63
61
  });
64
62
 
65
- // Set Connected
66
63
  Object.defineProperty(this, '__connected', {
67
64
  value: false,
68
65
  enumerable: false,
69
66
  writable: true,
70
67
  });
71
68
 
72
- // Set Cache
73
69
  Object.defineProperty(this, '__cache', {
74
70
  value: Object.values(items).reduce((obj, item) => ({
75
71
  ...obj,
@@ -69,6 +69,13 @@ class Device extends Item {
69
69
  this.__capabilityInstances[capabilityId] = this.__capabilityInstances[capabilityId] || [];
70
70
  this.__capabilityInstances[capabilityId].push(instance);
71
71
 
72
+ // Schedule a refresh if it's been more than a second since the device has updated.
73
+ const now = Date.now();
74
+
75
+ if (this.__lastUpdated.getTime() + 1000 < now) {
76
+ this.manager.scheduleRefresh();
77
+ }
78
+
72
79
  return instance;
73
80
  }
74
81
 
@@ -117,31 +124,32 @@ class Device extends Item {
117
124
  }
118
125
 
119
126
  onReconnect() {
120
- if (Object.keys(this.__capabilityInstances).length > 0) {
121
- // Get the device's latest values
122
- // TODO: Optimize this with `getDevices()` when >1 device has >0 capability instances.
123
- this.manager.getDevice({
124
- id: this.id,
125
- }).then(async device => {
126
- Object.entries(this.__capabilityInstances).forEach(([capabilityId, capabilityInstances]) => {
127
- const value = device.capabilitiesObj
128
- ? typeof device.capabilitiesObj[capabilityId] !== 'undefined'
129
- ? device.capabilitiesObj[capabilityId].value
130
- : null
131
- : null;
132
-
133
- for (const capabilityInstance of capabilityInstances) {
134
- capabilityInstance.__onCapabilityValue({
135
- capabilityId,
136
- value,
137
- transactionId: Util.uuid(),
138
- });
139
- }
127
+ this.manager.scheduleRefresh();
128
+ }
129
+
130
+ refreshCapabilityInstances() {
131
+ Object.entries(this.__capabilityInstances).forEach(([capabilityId, capabilityInstances]) => {
132
+ const value = this.capabilitiesObj
133
+ ? this.capabilitiesObj[capabilityId] != null
134
+ ? this.capabilitiesObj[capabilityId].value
135
+ : null
136
+ : null;
137
+
138
+ const lastUpdated = this.capabilitiesObj
139
+ ? this.capabilitiesObj[capabilityId] != null
140
+ ? this.capabilitiesObj[capabilityId].lastUpdated
141
+ : null
142
+ : null;
143
+
144
+ for (const capabilityInstance of capabilityInstances) {
145
+ capabilityInstance.__onCapabilityValue({
146
+ capabilityId,
147
+ value,
148
+ transactionId: Util.uuid(),
149
+ transactionTime: lastUpdated
140
150
  });
141
- })
142
- // eslint-disable-next-line no-console
143
- .catch(err => this.__debug(`Device[${this.id}].onReconnectError:`, err));
144
- }
151
+ }
152
+ });
145
153
  }
146
154
 
147
155
  /**
@@ -12,6 +12,31 @@ class ManagerDevices extends Manager {
12
12
  Device,
13
13
  }
14
14
 
15
+ scheduleRefresh() {
16
+ if (this.__refreshTimeout) {
17
+ clearTimeout(this.__refreshTimeout);
18
+ }
19
+
20
+ if (this.__pendingRefreshDevicesCall != null) {
21
+ return;
22
+ }
23
+
24
+ this.__refreshTimeout = setTimeout(() => {
25
+ if (this.isConnected()) {
26
+ this.__debug('Refreshing devices...');
27
+ this.__pendingRefreshDevicesCall = this.getDevices({ $cache: false }).then(devices => {
28
+ for (const device of Object.values(devices)) {
29
+ device.refreshCapabilityInstances();
30
+ }
31
+ }).catch(err => {
32
+ this.__debug('Failed to refresh devices.', err);
33
+ }).finally(() => {
34
+ this.__pendingRefreshDevicesCall = null;
35
+ });
36
+ }
37
+ }, 1000);
38
+ }
39
+
15
40
  }
16
41
 
17
42
  module.exports = ManagerDevices;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homey-api",
3
- "version": "3.4.5",
3
+ "version": "3.4.7",
4
4
  "description": "Homey API",
5
5
  "main": "index.js",
6
6
  "files": [