generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.ts
90 lines (72 loc) · 2.4 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
import type {
API,
Characteristic,
DynamicPlatformPlugin,
Logging,
PlatformAccessory,
Service,
} from 'homebridge';
import { GaroAccessory } from './platformAccessory.js';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
import type { GaroPlatformConfig, GaroStatusResponse } from './types.js';
export class GaroPlatform implements DynamicPlatformPlugin {
public readonly Service: typeof Service;
public readonly Characteristic: typeof Characteristic;
public readonly accessories: PlatformAccessory[] = [];
constructor(
public readonly log: Logging,
public readonly config: GaroPlatformConfig,
public readonly api: API,
) {
this.Service = api.hap.Service;
this.Characteristic = api.hap.Characteristic;
this.log.debug('Finished initializing platform:', this.config.name);
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
this.discoverDevices();
});
}
configureAccessory(accessory: PlatformAccessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
}
async discoverDevices() {
const devices = [];
for (const device of this.config.devices) {
const response = await fetch(
`${device.address}/servlet/rest/chargebox/status`,
);
const data = (await response.json()) as GaroStatusResponse;
// TODO: get model from rest api
devices.push({
serialnumber: String(data.serialNumber),
model: 'GARO',
address: device.address,
});
}
for (const device of devices) {
const uuid = this.api.hap.uuid.generate(device.serialnumber);
const existingAccessory = this.accessories.find(
(accessory) => accessory.UUID === uuid,
);
if (existingAccessory) {
this.log.info(
'Restoring existing accessory from cache:',
existingAccessory.displayName,
);
new GaroAccessory(this, existingAccessory);
} else {
this.log.info('Adding new accessory:', device.serialnumber);
const accessory = new this.api.platformAccessory(
device.serialnumber,
uuid,
);
accessory.context.device = device;
new GaroAccessory(this, accessory);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
accessory,
]);
}
}
}
}