generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.ts
87 lines (73 loc) · 3.36 KB
/
platform.ts
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
import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic } from 'homebridge';
import { AirQualitySensorAccessory } from './airQualitySensorAccessory';
import { HumiditySensorAccessory } from './humiditySensorAccessory';
import { TemperatureSensorAccessory } from './temperatureSensorAccessory';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
import { ApiConfig, Device } from './model';
export class Platform implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
// this is used to track restored cached accessories
public readonly accessories: PlatformAccessory[] = [];
constructor(
public readonly log: Logger,
public readonly config: PlatformConfig,
public readonly api: API,
) {
this.log.debug('Finished initializing platform:', this.config.name);
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
this.discoverDevices(this.config.api as Array<ApiConfig>);
});
}
configureAccessory(accessory: PlatformAccessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
}
discoverDevices(api: ApiConfig[]) {
const devices: Device[] = [];
api.forEach(apiItem => {
devices.push({
uniqueId: `bi-temperature-sensor-${apiItem.name.toLowerCase()}`,
displayName: `BI Temperature Sensor ${apiItem.name}`,
accessoryType: TemperatureSensorAccessory,
apiUrl: apiItem.url,
});
devices.push({
uniqueId: `bi-humidity-sensor-${apiItem.name.toLowerCase()}`,
displayName: `BI Humidity Sensor ${apiItem.name}`,
accessoryType: HumiditySensorAccessory,
apiUrl: apiItem.url,
});
devices.push({
uniqueId: `bi-air-quality-sensor-${apiItem.name.toLowerCase()}`,
displayName: `BI Air Quality Sensor ${apiItem.name}`,
accessoryType: AirQualitySensorAccessory,
apiUrl: apiItem.url,
});
});
for (const accessory of this.accessories) {
const deviceExists = devices.some(d => this.api.hap.uuid.generate(d.uniqueId) === accessory.UUID);
if (!deviceExists) {
this.log.info('Removing existing accessory from cache:', accessory.displayName);
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
for (const device of devices) {
const uuid = this.api.hap.uuid.generate(device.uniqueId);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
if (existingAccessory) {
existingAccessory.displayName = device.displayName;
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
new device.accessoryType(this, existingAccessory, device.apiUrl);
this.api.updatePlatformAccessories([existingAccessory]);
} else {
this.log.info('Adding new accessory:', device.displayName);
const accessory = new this.api.platformAccessory(device.displayName, uuid);
accessory.context.device = device;
new device.accessoryType(this, accessory, device.apiUrl);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
}