generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.ts
96 lines (84 loc) · 3.95 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
88
89
90
91
92
93
94
95
96
import {
API,
Categories,
Characteristic,
DynamicPlatformPlugin,
Logging,
PlatformAccessory,
PlatformConfig,
Service,
} from 'homebridge';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
import { HeatzyPiloteAccessory, HeatzyPiloteAccessoryConfig } from './heatzy-pilote-accessory.js';
import { HeatzyClient } from './heatzy-client.js';
export class HeatzyChineseMultiswitchPlatform implements DynamicPlatformPlugin {
public readonly Service: typeof Service;
public readonly Characteristic: typeof Characteristic;
public readonly cachedAccessories: Map<string, PlatformAccessory> = new Map();
public readonly heatzyClient: HeatzyClient;
constructor(
public readonly log: Logging,
public readonly config: PlatformConfig,
public readonly api: API,
) {
this.Service = api.hap.Service;
this.Characteristic = api.hap.Characteristic;
this.heatzyClient = new HeatzyClient({
username: this.config.heatzyUsername,
password: this.config.heatzyPassword,
});
this.log.debug('Initializing platform:', PLATFORM_NAME);
// When this event is fired it means Homebridge has restored all cached accessories from disk.
// Dynamic Platform plugins should only register new accessories after this event was fired,
// in order to ensure they weren't added to homebridge already. This event can also be used
// to start discovery of new accessories.
this.api.on('didFinishLaunching', () => {
log.debug('Discovering devices from configured Heatzy account...');
// run the method to discover / register your devices as accessories
this.syncDevicesFromHeatzy();
});
}
/**
* This function is invoked when homebridge restores cached accessories from disk at startup.
* It should be used to set up event handlers for characteristics and update respective values.
*/
configureAccessory(accessory: PlatformAccessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
// add the restored accessory to the accessories cache, so we can track if it has already been registered
this.cachedAccessories.set(accessory.UUID, accessory);
}
private async syncDevicesFromHeatzy() {
const { devices } = await this.heatzyClient.getAllBindings();
this.log.debug(`Discovered ${devices.length} devices from Heatzy account.`);
const discoveredAccessoryUUIDs = new Set<string>();
for (const device of devices) {
const uuid = this.api.hap.uuid.generate(device.did);
discoveredAccessoryUUIDs.add(uuid);
const cachedAccessory = this.cachedAccessories.get(uuid);
const accessoryConfig: HeatzyPiloteAccessoryConfig = {
device,
includeAliases: this.config.includeAliasesInConfiguredNames,
heatzySyncInterval: this.config.heatzySyncInterval,
};
if (cachedAccessory) {
// Restore previously configured accessory
this.log.info(`Restoring previously configured accessory: ${device.dev_alias} (ID: ${device.did})`);
new HeatzyPiloteAccessory(this, cachedAccessory, this.log, accessoryConfig);
} else {
// Configure new accessory
this.log.info(`Configuring new accessory: ${device.dev_alias} (ID: ${device.did})`);
const newAccessory = new this.api.platformAccessory(`Heatzy Pilote ${device.dev_alias}`, uuid, Categories.SWITCH);
new HeatzyPiloteAccessory(this, newAccessory, this.log, accessoryConfig);
this.cachedAccessories.set(uuid, newAccessory);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [newAccessory]);
}
}
for (const [uuid, accessory] of this.cachedAccessories) {
this.log.debug('Checking if accessory should be removed from cache:', accessory.displayName);
if (!discoveredAccessoryUUIDs.has(uuid)) {
this.log.info('Removing existing accessory from cache:', accessory.displayName);
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
}