forked from lprhodes/homebridge-broadlink-rm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (74 loc) · 3.26 KB
/
index.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
const semver = require('semver');
const persistentState = require('./helpers/persistentState')
if (semver.lt(process.version, '7.6.0')) throw new Error(`homebridge-broadlink-rm requires your node version to be at least v7.6.0. Current version: ${process.version}`)
const Accessory = require('./accessories');
module.exports = (homebridge) => {
global.Service = homebridge.hap.Service;
global.Characteristic = homebridge.hap.Characteristic;
require('./services/TVChannels');
require('./services/UpDown');
homebridge.registerPlatform("homebridge-broadlink-rm", "BroadlinkRM", BroadlinkRMPlatform);
}
class BroadlinkRMPlatform {
constructor (log, config = {}) {
this.log = log;
this.config = config;
const { homebridgeDirectory } = config;
persistentState.init({ homebridgeDirectory })
}
accessories (callback) {
const { config, log } = this;
const accessories = [];
// Add a Learn Code accessory if none exist in the config
const learnIRAccessories = config.accessories ? config.accessories.filter((accessory) => (accessory.type === 'learn-ir' || accessory.type === 'learn-code')) : [];
if (learnIRAccessories.length === 0) {
const learnCodeAccessory = new Accessory.LearnCode(log, { name: 'Learn' });
accessories.push(learnCodeAccessory);
const scanFrequencyAccessory = new Accessory.LearnCode(log, { name: 'Scan Frequency', scanFrequency: true });
accessories.push(scanFrequencyAccessory);
}
// Check for no accessories
if (!config.accessories || config.accessories.length === 0) {
log('No accessories have been added to the Broadlink RM config. Only the Learn Code accessory will be accessible on HomeKit.');
return callback(accessories);
}
// Itterate through the config accessories
config.accessories.forEach((accessory) => {
if (!accessory.type) throw new Error(`Each accessory must be configured with a "type". e.g. "switch"`);
const classTypes = {
'air-conditioner': Accessory.AirCon,
'learn-ir': Accessory.LearnCode,
'learn-code': Accessory.LearnCode,
'switch': Accessory.Switch,
'garage-door-opener': Accessory.GarageDoorOpener,
'switch-multi': Accessory.SwitchMulti,
'switch-multi-repeat': Accessory.SwitchMultiRepeat,
'switch-repeat': Accessory.SwitchRepeat,
'fan': Accessory.Fan,
'light': Accessory.Light,
'window-covering': Accessory.WindowCovering,
}
if (!classTypes[accessory.type]) throw new Error(`We don't support accessories of type "${accessory.type}".`);
const homeKitAccessory = new classTypes[accessory.type](log, accessory)
accessories.push(homeKitAccessory);
})
accessories.forEach((accessory) => {
if (accessory instanceof Accessory.AirCon) {
accessory.updateAccessories(accessories)
}
})
callback(accessories);
}
}
//
// for (var i = 0; i < this.data.length; i++) {
// const data = this.data[i];
// const { name, type } = data;
//
// if (type === 'thermostat') {
// this.log('add thermostat service');
//
// const thermostatService = new ThermostatService(this.log, this.config, data).createService()
//
// services.push(thermostatService);
// }