homey-api 3.0.0-rc.2 → 3.0.0-rc.21
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.
- package/assets/specifications/AthomCloudAPI.json +16 -0
- package/assets/specifications/HomeyAPIV3Local.json +365 -7
- package/assets/types/homey-api.d.ts +382 -59
- package/assets/types/homey-api.private.d.ts +428 -59
- package/index.browser.js +1 -0
- package/index.js +19 -20
- package/lib/HomeyAPI/HomeyAPI.js +58 -5
- package/lib/HomeyAPI/HomeyAPIV2/ManagerDevices/Capability.js +2 -2
- package/lib/HomeyAPI/HomeyAPIV2/ManagerDevices/Device.js +4 -3
- package/lib/HomeyAPI/HomeyAPIV2/ManagerDevices.js +0 -14
- package/lib/HomeyAPI/HomeyAPIV2/ManagerDrivers/Driver.js +2 -2
- package/lib/HomeyAPI/HomeyAPIV2/ManagerDrivers/PairSession.js +20 -0
- package/lib/HomeyAPI/HomeyAPIV2/ManagerDrivers.js +17 -0
- package/lib/HomeyAPI/HomeyAPIV2/ManagerFlow/AdvancedFlow.js +19 -4
- package/lib/HomeyAPI/HomeyAPIV2/ManagerFlow/Flow.js +27 -2
- package/lib/HomeyAPI/HomeyAPIV2/ManagerFlow/FlowCardAction.js +2 -2
- package/lib/HomeyAPI/HomeyAPIV2/ManagerFlow/FlowCardCondition.js +2 -2
- package/lib/HomeyAPI/HomeyAPIV2/ManagerFlow/FlowCardTrigger.js +2 -2
- package/lib/HomeyAPI/HomeyAPIV2/ManagerFlow.js +37 -14
- package/lib/HomeyAPI/HomeyAPIV2/ManagerFlowToken/FlowToken.js +3 -3
- package/lib/HomeyAPI/HomeyAPIV2/ManagerInsights/Log.js +2 -3
- package/lib/HomeyAPI/HomeyAPIV2/ManagerInsights.js +17 -0
- package/lib/HomeyAPI/HomeyAPIV3/Item.js +14 -1
- package/lib/HomeyAPI/HomeyAPIV3/Manager.js +34 -46
- package/lib/HomeyAPI/HomeyAPIV3/ManagerDevices/Device.js +14 -12
- package/lib/HomeyAPI/HomeyAPIV3/ManagerDevices/DeviceCapability.js +1 -1
- package/lib/HomeyAPI/HomeyAPIV3/ManagerDrivers/PairSession.js +9 -0
- package/lib/HomeyAPI/HomeyAPIV3/ManagerDrivers.js +2 -0
- package/lib/HomeyAPI/HomeyAPIV3/ManagerFlow/AdvancedFlow.js +119 -0
- package/lib/HomeyAPI/HomeyAPIV3/ManagerFlow/Flow.js +103 -0
- package/lib/HomeyAPI/HomeyAPIV3/ManagerFlow/FlowCard.js +8 -0
- package/lib/HomeyAPI/HomeyAPIV3/ManagerFlowToken/FlowToken.js +12 -4
- package/lib/HomeyAPI/HomeyAPIV3/ManagerInsights/Log.js +14 -0
- package/lib/HomeyAPI/HomeyAPIV3.js +8 -8
- package/lib/HomeyAPI/HomeyAPIV3Local/ManagerDevkit.js +32 -0
- package/lib/HomeyAPI/HomeyAPIV3Local.js +6 -0
- package/package.json +2 -1
package/lib/HomeyAPI/HomeyAPI.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const Util = require('../Util');
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* A Homey API, to be extended.
|
|
5
7
|
* @hideconstructor
|
|
@@ -126,21 +128,25 @@ class HomeyAPI {
|
|
|
126
128
|
}
|
|
127
129
|
|
|
128
130
|
/**
|
|
129
|
-
*
|
|
131
|
+
* Creates a {@link HomeyAPIV3Local} or {@link HomeyAPIV2} instance for use in the Apps SDK.
|
|
130
132
|
* @param {Object} opts
|
|
131
|
-
* @param {Homey} opts.homey — Homey instance
|
|
132
|
-
* @param {Function} opts.debug — Debug
|
|
133
|
+
* @param {Homey} opts.homey — Homey (Apps SDK) instance.
|
|
134
|
+
* @param {Function|null} opts.debug — Debug function, defaults to `null`.
|
|
133
135
|
*/
|
|
134
136
|
static async createAppAPI({
|
|
135
137
|
homey,
|
|
136
|
-
debug =
|
|
138
|
+
debug = null,
|
|
137
139
|
} = {}) {
|
|
138
140
|
if (!homey) {
|
|
139
141
|
throw new Error('Invalid Homey');
|
|
140
142
|
}
|
|
141
143
|
|
|
144
|
+
if (debug === true) {
|
|
145
|
+
debug = (...props) => homey.app.log('[homey-api]', ...props);
|
|
146
|
+
}
|
|
147
|
+
|
|
142
148
|
const props = {
|
|
143
|
-
debug,
|
|
149
|
+
debug: debug ?? function debug() { },
|
|
144
150
|
token: await homey.api.getOwnerApiToken(),
|
|
145
151
|
baseUrl: await homey.api.getLocalUrl(),
|
|
146
152
|
strategy: [],
|
|
@@ -168,6 +174,53 @@ class HomeyAPI {
|
|
|
168
174
|
throw new Error(`Invalid Homey Platform Version: ${homey.platformVersion}`);
|
|
169
175
|
}
|
|
170
176
|
|
|
177
|
+
/**
|
|
178
|
+
* Creates a {@link HomeyAPIV3Local} instance for use in a project.
|
|
179
|
+
* @param {Object} opts
|
|
180
|
+
* @param {String} opts.address — The address of the Homey, e.g. `http://192.168.1.123:80`.
|
|
181
|
+
* @param {String} opts.token — A Personal Access Token created in the Homey Web App.
|
|
182
|
+
* @param {Function|null} opts.debug — Debug function, defaults to `null`.
|
|
183
|
+
* @example
|
|
184
|
+
* import { HomeyAPI } from 'homey-api';
|
|
185
|
+
* const homeyApi = await HomeyAPI.createLocalAPI({
|
|
186
|
+
* address: 'http://192.169.1.123',
|
|
187
|
+
* token: '<my_personal_access_token>',
|
|
188
|
+
* });
|
|
189
|
+
* const devices = await homeyApi.devices.getDevices();
|
|
190
|
+
*/
|
|
191
|
+
static async createLocalAPI({
|
|
192
|
+
address,
|
|
193
|
+
token,
|
|
194
|
+
debug = null,
|
|
195
|
+
}) {
|
|
196
|
+
if (!address) {
|
|
197
|
+
throw new Error('Invalid Address');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (!token) {
|
|
201
|
+
throw new Error('Invalid Token');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const res = await Util.fetch(`${address}/api/manager/system/ping`);
|
|
205
|
+
if (!res.headers.has('X-Homey-ID')) {
|
|
206
|
+
throw new Error(`No Homey Found At Address: ${address}`);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const props = {
|
|
210
|
+
token,
|
|
211
|
+
debug: debug ?? function debug() { },
|
|
212
|
+
baseUrl: address,
|
|
213
|
+
strategy: [],
|
|
214
|
+
properties: {
|
|
215
|
+
id: res.headers.get('X-Homey-ID'),
|
|
216
|
+
softwareVersion: res.headers.get('X-Homey-Version'),
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const HomeyAPIV3Local = require('./HomeyAPIV3Local');
|
|
221
|
+
return new HomeyAPIV3Local(props);
|
|
222
|
+
}
|
|
223
|
+
|
|
171
224
|
}
|
|
172
225
|
|
|
173
226
|
module.exports = HomeyAPI;
|
|
@@ -4,8 +4,8 @@ const CapabilityV3 = require('../../HomeyAPIV3/ManagerDevices/Capability');
|
|
|
4
4
|
|
|
5
5
|
class Capability extends CapabilityV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
item = super.
|
|
7
|
+
static transformGet(item) {
|
|
8
|
+
item = super.transformGet(item);
|
|
9
9
|
|
|
10
10
|
item.id = `${item.uri}:${item.id}`;
|
|
11
11
|
item.ownerUri = item.uri;
|
|
@@ -4,10 +4,11 @@ const DeviceV3 = require('../../HomeyAPIV3/ManagerDevices/Device');
|
|
|
4
4
|
|
|
5
5
|
class Device extends DeviceV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
item = super.transform(item);
|
|
9
|
-
|
|
7
|
+
static transformGet(item) {
|
|
10
8
|
item.driverId = `${item.driverUri}:${item.driverId}`;
|
|
9
|
+
|
|
10
|
+
item = super.transformGet(item);
|
|
11
|
+
|
|
11
12
|
delete item.driverUri;
|
|
12
13
|
delete item.zoneName;
|
|
13
14
|
return item;
|
|
@@ -12,20 +12,6 @@ class ManagerDevices extends Manager {
|
|
|
12
12
|
Device,
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
async getCapability({
|
|
16
|
-
$cache = true,
|
|
17
|
-
id,
|
|
18
|
-
}) {
|
|
19
|
-
if ($cache === true && this.__cache['capability'][id]) {
|
|
20
|
-
return this.__cache['capability'][id];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return this.__super__getCapability({
|
|
24
|
-
id: id.split(':').reverse()[0],
|
|
25
|
-
uri: id.split(':', 3).join(':'),
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
15
|
}
|
|
30
16
|
|
|
31
17
|
module.exports = ManagerDevices;
|
|
@@ -4,8 +4,8 @@ const DriverV3 = require('../../HomeyAPIV3/ManagerDrivers/Driver');
|
|
|
4
4
|
|
|
5
5
|
class Driver extends DriverV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
item = super.
|
|
7
|
+
static transformGet(item) {
|
|
8
|
+
item = super.transformGet(item);
|
|
9
9
|
|
|
10
10
|
item.ownerId = item.id;
|
|
11
11
|
item.ownerUri = item.uri;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const PairSessionV3 = require('../../HomeyAPIV3/ManagerDrivers/PairSession');
|
|
4
|
+
|
|
5
|
+
class PairSession extends PairSessionV3 {
|
|
6
|
+
|
|
7
|
+
static transformGet(item) {
|
|
8
|
+
item = super.transformGet(item);
|
|
9
|
+
|
|
10
|
+
item.driverId = `${item.driverUri}:${item.driverId}`;
|
|
11
|
+
item.ownerUri = item.driverUri;
|
|
12
|
+
|
|
13
|
+
delete item.driverUri;
|
|
14
|
+
|
|
15
|
+
return item;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = PairSession;
|
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
const ManagerDriversV3 = require('../HomeyAPIV3/ManagerDrivers');
|
|
4
4
|
const Driver = require('./ManagerDrivers/Driver');
|
|
5
|
+
const PairSession = require('./ManagerDrivers/PairSession');
|
|
5
6
|
|
|
6
7
|
class ManagerDrivers extends ManagerDriversV3 {
|
|
7
8
|
|
|
8
9
|
static CRUD = {
|
|
9
10
|
...super.CRUD,
|
|
10
11
|
Driver,
|
|
12
|
+
PairSession,
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
async getDriver({
|
|
@@ -24,6 +26,21 @@ class ManagerDrivers extends ManagerDriversV3 {
|
|
|
24
26
|
});
|
|
25
27
|
}
|
|
26
28
|
|
|
29
|
+
async createPairSession({
|
|
30
|
+
pairsession,
|
|
31
|
+
...props
|
|
32
|
+
}) {
|
|
33
|
+
if (pairsession.driverId) {
|
|
34
|
+
pairsession.driverUri = pairsession.driverId.split(':', 3).join(':');
|
|
35
|
+
pairsession.driverId = pairsession.driverId.split(':').reverse()[0];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return this.__super__createPairSession({
|
|
39
|
+
pairsession,
|
|
40
|
+
...props,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
27
44
|
}
|
|
28
45
|
|
|
29
46
|
module.exports = ManagerDrivers;
|
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const AdvancedFlowV3 = require('../../HomeyAPIV3/ManagerFlow/
|
|
3
|
+
const AdvancedFlowV3 = require('../../HomeyAPIV3/ManagerFlow/AdvancedFlow');
|
|
4
4
|
|
|
5
5
|
class AdvancedFlow extends AdvancedFlowV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
|
|
9
|
-
card
|
|
7
|
+
static transformGet(item) {
|
|
8
|
+
if (item.cards) {
|
|
9
|
+
for (const card of Object.values(item.cards)) {
|
|
10
|
+
card.id = `${card.ownerUri}:${card.id}`;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
delete item.broken;
|
|
15
|
+
|
|
16
|
+
return item;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static transformSet(item) {
|
|
20
|
+
if (item.cards) {
|
|
21
|
+
for (const card of Object.values(item.cards)) {
|
|
22
|
+
card.ownerUri = card.id.split(':', 3).join(':');
|
|
23
|
+
card.id = card.id.split(':').reverse()[0];
|
|
24
|
+
}
|
|
10
25
|
}
|
|
11
26
|
|
|
12
27
|
return item;
|
|
@@ -4,8 +4,8 @@ const FlowV3 = require('../../HomeyAPIV3/ManagerFlow/Flow');
|
|
|
4
4
|
|
|
5
5
|
class Flow extends FlowV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
item = super.
|
|
7
|
+
static transformGet(item) {
|
|
8
|
+
item = super.transformGet(item);
|
|
9
9
|
|
|
10
10
|
if (item.trigger) {
|
|
11
11
|
item.trigger.id = `${item.trigger.uri}:${item.trigger.id}`;
|
|
@@ -26,6 +26,31 @@ class Flow extends FlowV3 {
|
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
delete item.broken;
|
|
30
|
+
|
|
31
|
+
return item;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static transformSet(item) {
|
|
35
|
+
if (item.trigger) {
|
|
36
|
+
item.trigger.uri = item.trigger.id.split(':', 3).join(':');
|
|
37
|
+
item.trigger.id = item.trigger.id.split(':').reverse()[0];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (Array.isArray(item.conditions)) {
|
|
41
|
+
item.conditions.forEach(card => {
|
|
42
|
+
card.uri = card.id.split(':', 3).join(':');
|
|
43
|
+
card.id = card.id.split(':').reverse()[0];
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (Array.isArray(item.actions)) {
|
|
48
|
+
item.actions.forEach(card => {
|
|
49
|
+
card.uri = card.id.split(':', 3).join(':');
|
|
50
|
+
card.id = card.id.split(':').reverse()[0];
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
29
54
|
return item;
|
|
30
55
|
}
|
|
31
56
|
|
|
@@ -4,8 +4,8 @@ const FlowCardActionV3 = require('../../HomeyAPIV3/ManagerFlow/FlowCardAction');
|
|
|
4
4
|
|
|
5
5
|
class FlowCardAction extends FlowCardActionV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
item = super.
|
|
7
|
+
static transformGet(item) {
|
|
8
|
+
item = super.transformGet(item);
|
|
9
9
|
|
|
10
10
|
item.id = `${item.uri}:${item.id}`;
|
|
11
11
|
item.ownerUri = item.uri;
|
|
@@ -4,8 +4,8 @@ const FlowCardConditionV3 = require('../../HomeyAPIV3/ManagerFlow/FlowCardCondit
|
|
|
4
4
|
|
|
5
5
|
class FlowCardCondition extends FlowCardConditionV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
item = super.
|
|
7
|
+
static transformGet(item) {
|
|
8
|
+
item = super.transformGet(item);
|
|
9
9
|
|
|
10
10
|
item.id = `${item.uri}:${item.id}`;
|
|
11
11
|
item.ownerUri = item.uri;
|
|
@@ -4,8 +4,8 @@ const FlowCardTriggerV3 = require('../../HomeyAPIV3/ManagerFlow/FlowCardTrigger'
|
|
|
4
4
|
|
|
5
5
|
class FlowCardTrigger extends FlowCardTriggerV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
item = super.
|
|
7
|
+
static transformGet(item) {
|
|
8
|
+
item = super.transformGet(item);
|
|
9
9
|
|
|
10
10
|
item.id = `${item.uri}:${item.id}`;
|
|
11
11
|
item.ownerUri = item.uri;
|
|
@@ -18,22 +18,45 @@ class ManagerFlow extends Manager {
|
|
|
18
18
|
FlowCardAction,
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// async createAdvancedFlow() {
|
|
21
|
+
async createFlow({
|
|
22
|
+
flow,
|
|
23
|
+
...props
|
|
24
|
+
}) {
|
|
25
|
+
return this.__super__.createFlow({
|
|
26
|
+
flow: Flow.transformSet(flow),
|
|
27
|
+
...props,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
31
30
|
|
|
32
|
-
|
|
31
|
+
async updateFlow({
|
|
32
|
+
flow,
|
|
33
|
+
...props
|
|
34
|
+
}) {
|
|
35
|
+
return this.__super__.updateFlow({
|
|
36
|
+
flow: Flow.transformSet(flow),
|
|
37
|
+
...props,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
33
40
|
|
|
34
|
-
|
|
41
|
+
async createAdvancedFlow({
|
|
42
|
+
advancedflow,
|
|
43
|
+
...props
|
|
44
|
+
}) {
|
|
45
|
+
return this.__super__.createAdvancedFlow({
|
|
46
|
+
advancedflow: AdvancedFlow.transformSet(advancedflow),
|
|
47
|
+
...props,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
35
50
|
|
|
36
|
-
|
|
51
|
+
async updateAdvancedFlow({
|
|
52
|
+
advancedflow,
|
|
53
|
+
...props
|
|
54
|
+
}) {
|
|
55
|
+
return this.__super__.updateAdvancedFlow({
|
|
56
|
+
advancedflow: AdvancedFlow.transformSet(advancedflow),
|
|
57
|
+
...props,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
37
60
|
|
|
38
61
|
async getFlowCardTrigger({
|
|
39
62
|
$cache = true,
|
|
@@ -43,7 +66,7 @@ class ManagerFlow extends Manager {
|
|
|
43
66
|
return this.__cache['flowcardtrigger'][id];
|
|
44
67
|
}
|
|
45
68
|
|
|
46
|
-
return this.
|
|
69
|
+
return this.__super__getFlowCardTrigger({
|
|
47
70
|
id: id.split(':').reverse()[0],
|
|
48
71
|
uri: id.split(':', 3).join(':'),
|
|
49
72
|
});
|
|
@@ -4,15 +4,15 @@ const FlowTokenV3 = require('../../HomeyAPIV3/ManagerFlowToken/FlowToken');
|
|
|
4
4
|
|
|
5
5
|
class FlowToken extends FlowTokenV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
7
|
+
static transformGet(item) {
|
|
8
8
|
// TODO: Remove FlowTokenV3.transform and uncomment this after front-end does not use FlowToken.uri|id|uriObj anymore!
|
|
9
|
-
// item = super.
|
|
9
|
+
// item = super.transformGet(item);
|
|
10
10
|
|
|
11
11
|
item.ownerUri = item.uri;
|
|
12
12
|
item.ownerId = item.id;
|
|
13
|
-
item.ownerName = item.uriObj.name;
|
|
14
13
|
item.id = `${item.ownerUri}:${item.ownerId}`;
|
|
15
14
|
|
|
15
|
+
delete item.ownerName; // Prepare for back-end change
|
|
16
16
|
delete item.uri;
|
|
17
17
|
delete item.uriObj;
|
|
18
18
|
|
|
@@ -4,12 +4,11 @@ const LogV3 = require('../../HomeyAPIV3/ManagerInsights/Log');
|
|
|
4
4
|
|
|
5
5
|
class Log extends LogV3 {
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
item = super.
|
|
7
|
+
static transformGet(item) {
|
|
8
|
+
item = super.transformGet(item);
|
|
9
9
|
|
|
10
10
|
item.ownerId = item.id;
|
|
11
11
|
item.ownerUri = item.uri;
|
|
12
|
-
item.ownerName = item.uriObj.name;
|
|
13
12
|
item.id = `${item.uri}:${item.id}`;
|
|
14
13
|
|
|
15
14
|
delete item.uri;
|
|
@@ -24,6 +24,23 @@ class ManagerInsights extends ManagerInsightsV3 {
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
async getLogEntries({ id }) {
|
|
28
|
+
return this.__super__getLogEntries({
|
|
29
|
+
id: id.split(':').reverse()[0],
|
|
30
|
+
uri: id.split(':', 3).join(':'),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async deleteLogEntries({ id }) {
|
|
35
|
+
return this.__super__deleteLogEntries({
|
|
36
|
+
id: id.split(':').reverse()[0],
|
|
37
|
+
uri: id.split(':', 3).join(':'),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// deleteLog
|
|
42
|
+
// updateLog
|
|
43
|
+
|
|
27
44
|
}
|
|
28
45
|
|
|
29
46
|
module.exports = ManagerInsights;
|
|
@@ -64,8 +64,17 @@ class Item extends EventEmitter {
|
|
|
64
64
|
|
|
65
65
|
__update(properties) {
|
|
66
66
|
for (const [key, value] of Object.entries(properties)) {
|
|
67
|
+
if (key === 'id') continue;
|
|
68
|
+
|
|
67
69
|
this[key] = value;
|
|
68
70
|
}
|
|
71
|
+
|
|
72
|
+
this.emit('update', properties);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
__delete() {
|
|
76
|
+
this.destroy();
|
|
77
|
+
this.emit('delete');
|
|
69
78
|
}
|
|
70
79
|
|
|
71
80
|
async connect() {
|
|
@@ -171,7 +180,11 @@ class Item extends EventEmitter {
|
|
|
171
180
|
this.disconnect().catch(() => { });
|
|
172
181
|
}
|
|
173
182
|
|
|
174
|
-
static
|
|
183
|
+
static transformGet(item) {
|
|
184
|
+
return item;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static transformSet(item) {
|
|
175
188
|
return item;
|
|
176
189
|
}
|
|
177
190
|
|
|
@@ -213,6 +213,7 @@ class Manager extends EventEmitter {
|
|
|
213
213
|
// This is about ~2x faster than HTTP
|
|
214
214
|
if (this.homey.isConnected() && $socket === true) {
|
|
215
215
|
result = await Util.timeout(new Promise((resolve, reject) => {
|
|
216
|
+
this.__debug(`IO ${operationId}`);
|
|
216
217
|
this.homey.__ioNamespace.emit('api', {
|
|
217
218
|
args,
|
|
218
219
|
operation: operationId,
|
|
@@ -258,9 +259,8 @@ class Manager extends EventEmitter {
|
|
|
258
259
|
switch (operation.crud.type) {
|
|
259
260
|
case 'getOne': {
|
|
260
261
|
let item = { ...result };
|
|
261
|
-
item = Item.
|
|
262
|
+
item = Item.transformGet(item);
|
|
262
263
|
item = new Item({
|
|
263
|
-
itemId,
|
|
264
264
|
id: item.id,
|
|
265
265
|
homey: this.homey,
|
|
266
266
|
manager: this,
|
|
@@ -278,13 +278,13 @@ class Manager extends EventEmitter {
|
|
|
278
278
|
|
|
279
279
|
// Add all to cache
|
|
280
280
|
for (let item of Object.values(result)) {
|
|
281
|
-
item = Item.
|
|
282
|
-
|
|
283
|
-
|
|
281
|
+
item = Item.transformGet(item);
|
|
282
|
+
|
|
283
|
+
if (this.isConnected() && this.__cache[itemId][item.id]) {
|
|
284
284
|
items[item.id] = this.__cache[itemId][item.id];
|
|
285
|
+
items[item.id].__update(item);
|
|
285
286
|
} else {
|
|
286
287
|
items[item.id] = new Item({
|
|
287
|
-
itemId,
|
|
288
288
|
id: item.id,
|
|
289
289
|
homey: this.homey,
|
|
290
290
|
manager: this,
|
|
@@ -316,13 +316,14 @@ class Manager extends EventEmitter {
|
|
|
316
316
|
case 'createOne':
|
|
317
317
|
case 'updateOne': {
|
|
318
318
|
let item = { ...result };
|
|
319
|
-
item = Item.
|
|
320
|
-
|
|
321
|
-
|
|
319
|
+
item = Item.transformGet(item);
|
|
320
|
+
|
|
321
|
+
if (this.isConnected() && this.__cache[itemId][item.id]) {
|
|
322
|
+
item = this.__cache[itemId][item.id];
|
|
323
|
+
item.__update(item);
|
|
322
324
|
} else {
|
|
323
|
-
item = Item.
|
|
325
|
+
item = Item.transformGet(item);
|
|
324
326
|
item = new Item({
|
|
325
|
-
itemId,
|
|
326
327
|
id: item.id,
|
|
327
328
|
homey: this.homey,
|
|
328
329
|
manager: this,
|
|
@@ -337,7 +338,7 @@ class Manager extends EventEmitter {
|
|
|
337
338
|
return item;
|
|
338
339
|
}
|
|
339
340
|
case 'deleteOne': {
|
|
340
|
-
if (this.__cache[itemId][args.id]) {
|
|
341
|
+
if (this.isConnected() && this.__cache[itemId][args.id]) {
|
|
341
342
|
this.__cache[itemId][args.id].destroy();
|
|
342
343
|
delete this.__cache[itemId][args.id];
|
|
343
344
|
}
|
|
@@ -413,55 +414,42 @@ class Manager extends EventEmitter {
|
|
|
413
414
|
|
|
414
415
|
switch (operation) {
|
|
415
416
|
case 'create': {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
item = new Item({
|
|
419
|
-
|
|
420
|
-
id: item.id,
|
|
417
|
+
data = Item.transformGet(data);
|
|
418
|
+
|
|
419
|
+
const item = new Item({
|
|
420
|
+
id: data.id,
|
|
421
421
|
homey: this.homey,
|
|
422
422
|
manager: this,
|
|
423
|
-
properties: { ...
|
|
423
|
+
properties: { ...data },
|
|
424
424
|
});
|
|
425
|
+
this.__cache[itemId][data.id] = item;
|
|
425
426
|
|
|
426
|
-
this.
|
|
427
|
-
this.__cache[itemId][item.id].emit('create');
|
|
428
|
-
|
|
429
|
-
return this.emit(`${itemId}.create`, item);
|
|
427
|
+
return this.emit(event, item);
|
|
430
428
|
}
|
|
431
429
|
case 'update': {
|
|
432
|
-
|
|
433
|
-
item = Item.transform(item);
|
|
430
|
+
data = Item.transformGet(data);
|
|
434
431
|
|
|
435
|
-
if (this.__cache[itemId][
|
|
436
|
-
item = this.__cache[itemId][
|
|
437
|
-
item.__update(
|
|
438
|
-
|
|
439
|
-
} else {
|
|
440
|
-
item = new Item({
|
|
441
|
-
itemId,
|
|
442
|
-
id: item.id,
|
|
443
|
-
homey: this.homey,
|
|
444
|
-
manager: this,
|
|
445
|
-
properties: { ...item },
|
|
446
|
-
});
|
|
447
|
-
this.__cache[itemId][item.id] = item;
|
|
432
|
+
if (this.__cache[itemId][data.id]) {
|
|
433
|
+
const item = this.__cache[itemId][data.id];
|
|
434
|
+
item.__update(data);
|
|
435
|
+
return this.emit(event, item);
|
|
448
436
|
}
|
|
449
437
|
|
|
450
|
-
|
|
438
|
+
break;
|
|
451
439
|
}
|
|
452
440
|
case 'delete': {
|
|
453
|
-
|
|
454
|
-
item = Item.transform(item);
|
|
441
|
+
data = Item.transformGet(data);
|
|
455
442
|
|
|
456
|
-
if (this.__cache[itemId][
|
|
457
|
-
this.__cache[itemId][
|
|
458
|
-
|
|
443
|
+
if (this.__cache[itemId][data.id]) {
|
|
444
|
+
const item = this.__cache[itemId][data.id];
|
|
445
|
+
item.__delete();
|
|
459
446
|
delete this.__cache[itemId][item.id];
|
|
447
|
+
return this.emit(event, {
|
|
448
|
+
id: item.id,
|
|
449
|
+
});
|
|
460
450
|
}
|
|
461
451
|
|
|
462
|
-
|
|
463
|
-
id: item.id,
|
|
464
|
-
});
|
|
452
|
+
break;
|
|
465
453
|
}
|
|
466
454
|
default:
|
|
467
455
|
break;
|
|
@@ -17,17 +17,13 @@ class Device extends Item {
|
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
get uri() {
|
|
21
|
-
return `homey:device:${this.id}`;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
20
|
/**
|
|
25
|
-
* Creates an {@link
|
|
21
|
+
* Creates an {@link HomeyAPIV3.DeviceCapability} for realtime capability updates.
|
|
26
22
|
* @param {string} capabilityId
|
|
27
23
|
* @param {Function} listener
|
|
28
24
|
* @param {number|boolean|string} listener.value
|
|
29
|
-
* @returns {
|
|
30
|
-
* @function
|
|
25
|
+
* @returns {HomeyAPIV3.ManagerDevices.Device.DeviceCapability}
|
|
26
|
+
* @function HomeyAPIV3.ManagerDevices.Device#makeCapabilityInstance
|
|
31
27
|
* @example
|
|
32
28
|
*
|
|
33
29
|
* const onOffInstance = device.makeCapabilityInstance('onoff', value => {
|
|
@@ -76,7 +72,7 @@ class Device extends Item {
|
|
|
76
72
|
* @param {object} [opts.opts]
|
|
77
73
|
* @param {number} [opts.opts.duration]
|
|
78
74
|
* @returns {Promise<void>}
|
|
79
|
-
* @function
|
|
75
|
+
* @function HomeyAPIV3.ManagerDevices.Device#setCapabilityValue
|
|
80
76
|
*/
|
|
81
77
|
async setCapabilityValue(options, ...args) {
|
|
82
78
|
// Legacy compatibility from node-athom-api
|
|
@@ -199,10 +195,16 @@ class Device extends Item {
|
|
|
199
195
|
}), {});
|
|
200
196
|
}
|
|
201
197
|
|
|
202
|
-
static
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
198
|
+
static transformGet(item) {
|
|
199
|
+
item = super.transformGet(item);
|
|
200
|
+
|
|
201
|
+
delete item.driverUri;
|
|
202
|
+
|
|
203
|
+
if (item.capabilitiesObj) {
|
|
204
|
+
for (const capabilityObj of Object.values(item.capabilitiesObj)) {
|
|
205
|
+
if (capabilityObj.lastUpdated) {
|
|
206
|
+
capabilityObj.lastUpdated = new Date(capabilityObj.lastUpdated);
|
|
207
|
+
}
|
|
206
208
|
}
|
|
207
209
|
}
|
|
208
210
|
|