-
Notifications
You must be signed in to change notification settings - Fork 6
/
platform.js
54 lines (40 loc) · 1.59 KB
/
platform.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
const persistentState = require('./helpers/persistentState')
const semver = require('semver');
if (semver.lt(process.version, '7.6.0')) throw new Error(`Homebridge plugins that use the "homebridge-platform-helper" library require your node version to be at least the v12.14.0 LTM. Current version: ${process.version}`)
class HomebridgePlatform {
constructor (log, config = {}, homebridge) {
this.log = log;
this.config = config;
this.homebridge = homebridge;
const { homebridgeDirectory } = config;
persistentState.init({ homebridge, homebridgeDirectory });
}
async addAccessories (accessories) {
throw new Error('The addAccessories method must be overridden.')
};
async accessories (callback) {
const { config, log } = this;
const { name, disableLogs } = config;
const accessories = [];
await this.addAccessories(accessories);
// Disable logs if requested
if (disableLogs !== undefined) {
accessories.forEach((accessory) => {
if (accessory.config.disableLogs === undefined) {
accessory.disableLogs = disableLogs
}
})
}
// Check for no accessories
if (!config.accessories || config.accessories.length === 0) {
if (!disableLogs) log(`No accessories have been added to the "${name}" platform config.`);
return callback(accessories);
}
// Let accessories know about one-another if they wish
accessories.forEach((accessory) => {
if (accessory.updateAccessories) accessory.updateAccessories(accessories);
})
callback(accessories);
}
}
module.exports = HomebridgePlatform;