-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
75 lines (67 loc) · 2.42 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
let Service, Characteristic, Accessory, UUID
const platformAccessory = require('./platformAccessory.js');
const PLUGIN_NAME = 'homebridge-venstar-thermostats'
const PLATFORM_NAME = 'VenstarThermostats'
class VenstarThermostatsPlatform {
constructor(log, config, api) {
this.log = log
this.config = config
this.api = api
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
// this is used to track restored cached accessories
this.accessories = []
this.log.debug("Finished initializing platform:", PLATFORM_NAME);
this.api.on("didFinishLaunching", () => {
log.debug("Executed didFinishLaunching callback");
this.discoverDevices();
});
}
configureAccessory(accessory) {
this.log.info("Loading accessory from cache:", accessory.displayName);
this.accessories.push(accessory);
}
getThermostats() {
if (this.config.thermostats == undefined) {
console.log('Thermostats must be defined in the config file')
return {}
}
return this.config.thermostats
}
async discoverDevices() {
const devices = this.getThermostats();
for (const device of devices) {
const uuid = this.api.hap.uuid.generate('venstar' + '.' + device.name + '.'+ device.ip);
const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
if (existingAccessory) {
this.log.info("Restoring existing accessory from cache:", device.name);
new platformAccessory.VenstarThermostatsPlatformAccessory(this, existingAccessory, device.ip);
} else {
this.log.info("Adding new accessory:", device.name, device.ip);
const accessory = new this.api.platformAccessory(device.name, uuid);
accessory.context.device = device;
new platformAccessory.VenstarThermostatsPlatformAccessory(this, accessory, device.ip);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
accessory,
]);
}
}
}
}
module.exports = (api) => {
api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, VenstarThermostatsPlatform);
}
/*
module.exports = function (homebridge) {
//Service = homebridge.hap.Service
//Characteristic = homebridge.hap.Characteristic
//Accessory = homebridge.hap.Accessory
//UUID = homebridge.hap.uuid
homebridge.registerPlatform(
PLUGIN_NAME,
PLATFORM_NAME,
VenstarThermostatsPlatform,
true
)
}
*/