forked from rogierhofboer/com.ikea.tradfri.gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
173 lines (147 loc) · 5.94 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
'use strict';
const Homey = require('homey'),
node_tradfri_client = require("node-tradfri-client");
const lightDriverName = "light";
const groupDriverName = "group";
class IkeaTradfriGatewayApp extends Homey.App {
onInit() {
this._gatewayConnected = false;
this._homeyLightDriver = Homey.ManagerDrivers.getDriver(lightDriverName);
this._homeyGroupDriver = Homey.ManagerDrivers.getDriver(groupDriverName);
this._lights = {};
this._groups = {};
this._groupScenes = {};
(async (args, callback) => {
try {
await this.connect();
} catch (err) {
this.log(err.message);
}
})();
new Homey.FlowCardAction('setScene')
.register()
.registerRunListener( this._onFlowActionSetScene.bind(this) )
.getArgument('scene')
.registerAutocompleteListener( this._onSceneAutoComplete.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(Homey.ManagerSettings.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(Homey.ManagerSettings.get('identity'), Homey.ManagerSettings.get('psk'));
this._gatewayConnected = true;
this._tradfri.observeDevices();
this._tradfri.observeGroupsAndScenes();
}
isGatewayConnected() {
return this._gatewayConnected;
}
getLight(tradfriInstanceId) {
return this._lights[tradfriInstanceId];
}
getLights() {
return this._lights;
}
getGroups() {
return this._groups;
}
getScenes(groupId) {
return this._groupScenes[groupId];
}
operateLight(tradfriInstanceId, commands) {
let acc = this._lights[tradfriInstanceId];
if (typeof acc !== "undefined")
return this._tradfri.operateLight(acc, commands);
return Promise.reject("light not found");
}
operateGroup(tradfriInstanceId, commands) {
this.log('Sending command',commands);
let group = this._groups[tradfriInstanceId];
//this.log('Sending command',commands);
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._homeyLightDriver.updateCapabilities(acc);
}
}
_deviceRemoved(acc) {
this.log(`${acc.name} removed`);
if (acc.type === node_tradfri_client.AccessoryTypes.lightbulb) {
delete this._lights[acc.instanceId];
}
}
_groupUpdated(group) {
this.log(`Group ${group.name} updated`);
this._groups[group.instanceId] = group;
this._homeyGroupDriver.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] = {};
this._groupScenes[groupId][scene.instanceId] = scene;
}
_sceneRemoved(groupId, instanceId) {
this.log(`Scene ${instanceId} removed`);
if(this._groupScenes[groupId])
delete this._groupScenes[groupId][instanceId];
}
_onFlowActionSetScene( args ) {
return this.operateGroup(args.group._tradfriInstanceId, { transitionTime:1, onOff:true, sceneId:args.scene.instanceId });
}
_onSceneAutoComplete( query, args ) {
const scenes = this.getScenes(args.group._tradfriInstanceId);
return Object.values(scenes).map(s => {
return {instanceId: s.instanceId, name:s.name}
});
}
}
module.exports = IkeaTradfriGatewayApp;