-
Notifications
You must be signed in to change notification settings - Fork 21
/
app.js
277 lines (232 loc) · 9.87 KB
/
app.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict';
const Homey = require('homey'),
node_tradfri_client = require("node-tradfri-client");
const lightDriverName = "light";
const groupDriverName = "group";
const plugDriverName = "plug";
const BlindDriverName = "blinds";
class IkeaTradfriGatewayApp extends Homey.App {
onInit() {
this._gatewayConnected = false;
// this._homeyPlugDriver = this.homey.drivers.getDriver(plugDriverName);
// this._homeyBlindDriver = this.homey.drivers.getDriver(BlindDriverName);
// this._homeyLightDriver = this.homey.drivers.getDriver(lightDriverName);
// this._homeyGroupDriver = this.homey.drivers.getDriver(groupDriverName);
this._plugs = {};
this._blinds = {};
this._lights = {};
this._groups = {};
this._groupScenes = {};
this._gatewayScenes = {};
(async (args, callback) => {
try {
await this.connect();
} catch (err) {
this.log(err.message);
}
})();
this.homey.flow.getActionCard('setScene')
.registerRunListener(this._onFlowActionSetScene.bind(this))
.getArgument('scene')
.registerAutocompleteListener(this._onSceneAutoComplete.bind(this));
this.homey.flow.getActionCard('setGatewayScene')
.registerRunListener(this._onFlowActionSetGatewayScene.bind(this))
.getArgument('gatewayScene')
.registerAutocompleteListener(this._onGatewaySceneAutoComplete.bind(this));
this.log(`Tradfri Gateway App has been initialized`);
}
async discover() {
return node_tradfri_client.discoverGateway();
}
async authenticate(name, securityCode) {
let client = new node_tradfri_client.TradfriClient(name);
return client.authenticate(securityCode);
}
async connect() {
this._gatewayConnected = false;
if (this._tradfri != null) {
this._tradfri.destroy();
}
this._tradfri = new node_tradfri_client.TradfriClient(this.homey.settings.get('name'), {
watchConnection: {
pingInterval: 10000,
failedPingCountUntilOffline: 1,
failedPingBackoffFactor: 1.5,
reconnectionEnabled: true,
offlinePingCountUntilReconnect: 3,
// maximumReconnects: 3, // default is infinite
connectionInterval: 10000,
// maximumConnectionAttempts: 1, //default is infinite
failedConnectionBackoffFactor: 1.5
}
});
this._tradfri
//.on("ping failed", (count) => this.log(`${count} pings failed`))
//.on("ping succeeded", () => this.log("ping succeeded"))
.on("connection lost", () => this.log("connection lost"))
.on("connection failed", (att, max) => this.log(`connection failed: attempt ${att} of ${max}`))
.on("connection alive", () => this.log("connection alive"))
.on("gateway offline", () => this.log("gateway offline"))
.on("reconnecting", (att, max) => this.log(`reconnect attempt ${att} of ${max}`))
.on("give up", () => this.log("giving up..."))
.on("device updated", this._deviceUpdated.bind(this))
.on("device removed", this._deviceRemoved.bind(this))
.on("group updated", this._groupUpdated.bind(this))
.on("group removed", this._groupRemoved.bind(this))
.on("scene updated", this._sceneUpdated.bind(this))
.on("scene removed", this._sceneRemoved.bind(this));
await this._tradfri.connect(this.homey.settings.get('identity'), this.homey.settings.get('psk'));
this._gatewayConnected = true;
this._tradfri.observeDevices();
this._tradfri.observeGroupsAndScenes();
}
isGatewayConnected() {
return this._gatewayConnected;
}
getPlugs() {
return this._plugs;
}
getPlug(tradfriInstanceId) {
return this._plugs[tradfriInstanceId];
}
getBlinds() {
return this._blinds;
}
getBlind(tradfriInstanceId) {
return this._blinds[tradfriInstanceId];
}
getLight(tradfriInstanceId) {
return this._lights[tradfriInstanceId];
}
getLights() {
return this._lights;
}
getGroup(tradfriInstanceId) {
return this._groups[tradfriInstanceId];
}
getGroups() {
return this._groups;
}
getScenes(groupId) {
return this._groupScenes[groupId];
}
operatePlug(tradfriInstanceId, commands) {
this.log('operatePlug: Sending command',commands);
let acc = this._plugs[tradfriInstanceId];
if (typeof acc !== "undefined")
return this._tradfri.operatePlug(acc, commands);
return Promise.reject("plug not found");
}
operateBlind(tradfriInstanceId, commands) {
this.log('operateBlind: Sending command', commands);
let acc = this._blinds[tradfriInstanceId];
if (typeof acc !== "undefined")
return this._tradfri.operateBlind(acc, commands);
return Promise.reject("blind not found");
}
operateLight(tradfriInstanceId, commands) {
this.log('operateLight: Sending command',commands);
let acc = this._lights[tradfriInstanceId];
if (typeof acc !== "undefined")
commands["transitionTime"] = 0;
return this._tradfri.operateLight(acc, commands);
return Promise.reject("light not found");
}
operateGroup(tradfriInstanceId, commands) {
this.log('operateGroup: Sending command',commands);
let group = this._groups[tradfriInstanceId];
if (typeof group !== "undefined")
return this._tradfri.operateGroup(group, commands, true);
this.log(`Group with id ${tradfriInstanceId} not found`);
return Promise.reject("group not found");
}
_deviceUpdated(acc) {
if (acc.type === node_tradfri_client.AccessoryTypes.lightbulb) {
this.log(`${acc.name} updated`);
this._lights[acc.instanceId] = acc;
this.homey.drivers.getDriver(lightDriverName).updateCapabilities(acc);
//Update dim values on group if lights are updated individually or through scene
for (const [groupInstanceId, group] of Object.entries(this._groups)) {
if (group.deviceIDs && group.deviceIDs.indexOf(acc.instanceId) > -1)
this.homey.drivers.getDriver(groupDriverName).deviceInGroupUpdated(group);
}
}
if (acc.type === node_tradfri_client.AccessoryTypes.plug) {
this.log(`${acc.name} updated`);
this._plugs[acc.instanceId] = acc;
this.homey.drivers.getDriver(plugDriverName).updateCapabilities(acc);
}
if (acc.type === node_tradfri_client.AccessoryTypes.blind) {
//this.log('_deviceUpdated name and type: ',acc);
this.log(`${acc.name} updated`);
this._blinds[acc.instanceId] = acc;
this.homey.drivers.getDriver(BlindDriverName).updateCapabilities(acc);
}
if (acc.type === node_tradfri_client.AccessoryTypes.signalRepeater) {
//this.log(`Signal repeater: ${acc.name} found. Currently not supported.`);
}
if (acc.type === node_tradfri_client.AccessoryTypes.motionSensor) {
//this.log(`Motion sensor: ${acc.name} found. Currently not supported.`);
}
}
_deviceRemoved(acc) {
this.log(`${acc.name} removed`);
if (acc.type === node_tradfri_client.AccessoryTypes.lightbulb) {
delete this._lights[acc.instanceId];
}
if (acc.type === node_tradfri_client.AccessoryTypes.plug) {
delete this._plugs[acc.instanceId];
}
if (acc.type === node_tradfri_client.AccessoryTypes.blind) {
delete this._blinds[acc.instanceId];
}
}
_groupUpdated(group) {
this.log(`Group ${group.name} updated`);
this._groups[group.instanceId] = group;
this.homey.drivers.getDriver(groupDriverName).updateCapabilities(group);
}
_groupRemoved(instanceId) {
this.log(`Group ${instanceId} removed`);
delete this._groups[instanceId];
}
_sceneUpdated(groupId, scene) {
this.log(`Scene ${scene.name} updated`);
if (!this._groupScenes[groupId]) this._groupScenes[groupId] = {};
if (!this._gatewayScenes[groupId]) this._gatewayScenes[groupId] = {};
this._groupScenes[groupId][scene.instanceId] = scene;
this._gatewayScenes[groupId][scene.instanceId] = scene;
}
_sceneRemoved(groupId, instanceId) {
this.log(`Scene ${instanceId} removed`);
if (this._groupScenes[groupId])
delete this._groupScenes[groupId][instanceId];
if (this._gatewayScenes[groupId])
delete this._gatewayScenes[groupId][instanceId];
}
_onFlowActionSetScene(args) {
return this.operateGroup(args.group._tradfriInstanceId, { transitionTime: 1, onOff: true, sceneId: args.scene.instanceId });
}
_onFlowActionSetGatewayScene(args) {
return this.operateGroup(args.gatewayScene.groupId, { transitionTime: 1, onOff: true, sceneId: args.gatewayScene.instanceId });
}
_onSceneAutoComplete(query, args) {
const scenes = this.getScenes(args.group._tradfriInstanceId);
if (scenes) {
return Object.values(scenes).map(s => {
return { instanceId: s.instanceId, name: s.name }
});
}
return [];
}
_onGatewaySceneAutoComplete(query, args) {
const scenes = [];
Object.keys(this._gatewayScenes).forEach((groupId) => {
Object.values(this._gatewayScenes[groupId]).forEach(s => {
scenes.push({ instanceId: s.instanceId, name: s.name, groupId: groupId });
})
});
return scenes;
}
}
module.exports = IkeaTradfriGatewayApp;