-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathapp.js
304 lines (275 loc) · 11.6 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const Homey = require('homey')
const { HomeyAPI } = require('./modules/homey-api');
const fs = require('fs');
const storage = require('node-persist');
const path = require('path');
const debounce = require('lodash.debounce');
const {
HAPStorage,
uuid,
Bridge,
Service,
Characteristic,
Accessory
} = require('./modules/hap-nodejs');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// Device classes
const homekit = require('./lib/');
let bridge;
// Clear storage the hard way.
function clearHAPStorage() {
console.error('Clearing internal storage:');
for (const f of fs.readdirSync('/userdata')) {
const path = `/userdata/${ f }`;
console.error('- removing', path);
fs.rmSync(path, { recursive : true });
}
}
// Try to initialize existing storage.
try {
HAPStorage.setCustomStoragePath('/userdata');
HAPStorage.storage();
} catch(e) {
console.error('Error initializing existing storage', e);
clearHAPStorage();
throw Error('Please restart do to an internal storage issue');
}
module.exports = class HomekitApp extends Homey.App {
// Get API control function
async getApi() {
if (! this.api) {
this.api = await HomeyAPI.createAppAPI({ homey: this.homey });
// have to do this really early to work around a bug in the Web API
await this.api.devices.connect();
}
return this.api;
}
// Get all devices function
async getDevices() {
// HomeyAPIV3 doesn't set zoneName property (unlike V2)
// so we'll set it ourselves.
const [ zones, devices ] = await Promise.all([
this.api.zones.getZones(),
this.api.devices.getDevices()
]);
for (const device of Object.values(devices)) {
device.zoneName = zones[device.zone].name;
}
return devices;
}
// Start server function
async startingServer() {
this.log('starting server');
const api = this.api;
// Start by creating our Bridge which will host all loaded Accessories
let bridgeIdentifier = this.homey.settings.get('bridgeIdentifier') || 'Homey';
this.log(`Using "${ bridgeIdentifier }" as bridge identifier`);
bridge = new Bridge(bridgeIdentifier, uuid.generate(bridgeIdentifier));
bridge
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, 'Athom')
.setCharacteristic(Characteristic.Model, 'Homey')
.setCharacteristic(Characteristic.FirmwareRevision, '2.0');
// Listen for bridge identification event
bridge.on('identify', function(paired, callback) {
console.log('Homey identify');
callback(); // success
});
// Retrieve a list of all devices, and a list of devices that should (not) be paired.
let knownDevices = {};
let allDevices = await this.getDevices();
let pairedDevicesSetting = this.homey.settings.get('pairedDevices') || {};
this.pairedDevices = {};
for (let id in allDevices) {
let device = allDevices[id];
// Assume that unknown (new) devices should be paired.
if (! (device.id in pairedDevicesSetting)) {
pairedDevicesSetting[device.id] = true;
}
if (pairedDevicesSetting[id] === true) {
this.addDevice(device);
} else {
this.pairedDevices[device.id] = false;
this.log(`Not adding '${ device.name }' (shouldn't be paired)`);
}
knownDevices[id] = true;
}
// Update settings
this.homey.settings.set('pairedDevices', this.pairedDevices);
// Watch for setting changes
this.homey.settings.on('set', debounce(key => {
if (key === 'pairedDevices') {
this.pairedDevices = this.homey.settings.get('pairedDevices') || {};
}
}, 100));
// Subscribe to realtime events and set all devices global
api.devices.on('device.update', async ({ id, ready }) => {
if (! ready) return;
if (id in knownDevices) return;
knownDevices[id] = true;
this.log('New device found!');
const device = await api.devices.getDevice({ id });
this.addDevice(device);
this.homey.settings.set('pairedDevices', this.pairedDevices);
});
// Publish bridge
const username = this.homey.settings.get('username') || 'CC:22:3D:E3:CE:F6';
this.log(`Using ${ username } as username.`);
try {
await bridge.publish({
username: username,
port: 51833,
pincode: '200-20-200',
category: Accessory.Categories.BRIDGE
});
this.log('Started bridge');
} catch(e) {
this.error('Unable to start bridge');
this.error(e);
}
}
// On app init
async onInit() {
if (Homey.env.RESET_SETTINGS) {
this.homey.settings.set('pairedDevices', null);
}
try {
this.api = await this.getApi();
} catch(e) {
this.error('Unable to get API instance');
this.error(e);
return;
}
this.onInit2();
}
async onInit2() {
this.pairedDevices = {};
// If the app is started less than 10 minuten after a reboot, wait for
// devices to settle before starting the bridge, otherwise iOS will get
// confused.
const settleTime = this.homey.settings.get('settleTime') || 120;
const uptime = (await this.api.system.getInfo()).uptime;
if (uptime < 600) {
this.log('Homey rebooted, waiting for devices to settle');
let previousDeviceCount = 0;
while (true) {
let newDeviceCount = Object.keys(await this.getDevices()).length;
if (newDeviceCount && newDeviceCount === previousDeviceCount) {
this.log(`devices have settled (counted ${ newDeviceCount } in total)`);
break;
}
previousDeviceCount = newDeviceCount;
this.log(`devices have not yet settled, waiting for ${ settleTime } seconds...`);
await delay(settleTime * 1000);
}
}
this.startingServer();
}
// Add device function
async addDevice(device) {
if (! device || ! device.ready || ! device.capabilitiesObj || bridge.bridgedAccessories.length >= 149) return;
let api = this.api;
let capabilities = device.capabilities.reduce((acc, val) => {
if (typeof val === 'string') {
acc[val.split('.')[0]] = true;
}
return acc;
}, {});
let isPaired = false;
if ([device.class, device.virtualClass].includes('light') && 'onoff' in capabilities) {
this.log('Found light: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createLight(device, api));
} else if (device.class === 'lock' && 'locked' in capabilities) {
this.log('Found lock: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createLock(device, api));
} else if ([ 'curtain', 'blinds', 'sunshade', 'windowcoverings' ].includes(device.class) && 'windowcoverings_set' in capabilities && !('dim' in capabilities)) {
this.log(`Found ${ device.class } (windowcovering_set): ${ device.name }`);
isPaired = true;
bridge.addBridgedAccessory(homekit.createCurtains(device, api));
} else if ([ 'curtain', 'blinds', 'sunshade', 'windowcoverings' ].includes(device.class) && 'windowcoverings_state' in capabilities && !('dim' in capabilities)) {
this.log('Found blinds (state): ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createStateBlinds(device, api));
} else if (device.class === 'windowcoverings' && 'dim' in capabilities) {
this.log('Found blinds (dim): ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createDimBlinds(device, api));
} else if (device.class === 'socket' && 'onoff' in capabilities) {
this.log('Found socket: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createSocket(device, api, capabilities));
} else if ((device.class === 'fan' || device.class === 'heater') && 'onoff' in capabilities) {
this.log('Found fan/heater: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createFan(device, api, capabilities));
} else if (['amplifier', 'button', 'coffeemachine', 'kettle', 'tv', 'other', 'remote'].includes(device.class) && 'onoff' in capabilities) {
this.log('Found class with onoff: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createSwitch(device, api));
} else if (device.class === 'thermostat' && 'measure_temperature' in capabilities && 'target_temperature' in capabilities) {
this.log('Found thermostat: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createThermostat(device, api, capabilities));
} else if (device.class === 'doorbell' && 'alarm_generic' in capabilities) {
this.log('Found doorbell: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createDoorbell(device, api));
} else if ('homealarm_state' in capabilities || device.class === 'homealarm' || device.virtualClass === 'homealarm') {
this.log('Found Security system: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createSecuritySystem(device, api, capabilities));
} else if ([ device.class, device.virtualClass ].includes('speaker')) {
this.log('Found speaker: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createSpeaker(device, api, capabilities));
} else if ('button' in capabilities) {
this.log('Found button: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createButton(device, api));
} else if ([ 'sensor', 'other' ].includes(device.class) && ('measure_luminance' in capabilities || 'measure_temperature' in capabilities || 'measure_humidity' in capabilities || 'measure_pressure' in capabilities || 'alarm_motion' in capabilities || 'alarm_water' in capabilities || 'alarm_contact' in capabilities || 'alarm_smoke' in capabilities || 'alarm_co' in capabilities || 'alarm_co2' in capabilities)) {
this.log('Found Sensor: ' + device.name)
isPaired = true;
bridge.addBridgedAccessory(homekit.createSensor(device, api, capabilities));
} else if (device.class === 'garagedoor' && 'garagedoor_closed' in capabilities) {
this.log('Found garage door:', device.name);
isPaired = true;
bridge.addBridgedAccessory(homekit.createGarageDoorOpener(device, api, capabilities));
} else {
this.log(`No matching class found for: ${ device.name } of class ${ device.class }`);
}
this.pairedDevices[device.id] = isPaired;
device.on('$delete', id => {
this.deleteDevice(device);
});
}
async addDeviceById(id) {
return this.addDevice(await this.findDeviceById(id));
}
deleteDevice(device) {
if (! device || ! bridge) return;
this.log(`Deleting device '${ device.name }' (${ device.id }) from HomeKit`);
delete this.pairedDevices[device.id];
this.homey.settings.set('pairedDevices', this.pairedDevices);
let acc = bridge.bridgedAccessories.find(r => r.UUID === device.id);
acc && bridge.removeBridgedAccessory(acc);
}
async deleteDeviceById(id) {
return this.deleteDevice(await this.findDeviceById(id));
}
async findDeviceById(id) {
let allDevices = await this.getDevices();
return Object.values(allDevices).find(device => device.id === id);
}
clearStorage() {
clearHAPStorage();
// generate a new bridge 'username' to allow iOS to rediscover the bridge
const username = 'XX:XX:XX:XX:XX:XX'.replace(/X/g, function() {
return '0123456789ABCDEF'.charAt(Math.floor(Math.random() * 16))
});
this.log(`Setting new username to ${ username }`);
this.homey.settings.set('username', username);
this.homey.settings.set('pairedDevices', null);
}
}