generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platformAccessory.ts
85 lines (66 loc) · 2.47 KB
/
platformAccessory.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
import type {
CharacteristicValue,
PlatformAccessory,
Service,
} from 'homebridge';
import fetch from 'node-fetch';
import type { GaroPlatform } from './platform.js';
import type { GaroStatusResponse } from './types.js';
export class GaroAccessory {
private service: Service;
constructor(
private readonly platform: GaroPlatform,
private readonly accessory: PlatformAccessory,
) {
// set accessory information
this.accessory
.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Garo')
.setCharacteristic(
this.platform.Characteristic.Model,
this.accessory.context.device.model,
)
.setCharacteristic(
this.platform.Characteristic.SerialNumber,
this.accessory.context.device.serialnumber,
);
this.service =
this.accessory.getService(this.platform.Service.Outlet) ||
this.accessory.addService(this.platform.Service.Outlet);
this.service.setCharacteristic(
this.platform.Characteristic.Name,
accessory.context.device.serialnumber,
);
this.service
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
this.service
.getCharacteristic(this.platform.Characteristic.OutletInUse)
.onGet(this.getInUse.bind(this));
}
async setOn(value: CharacteristicValue) {
this.platform.log.debug('Set Characteristic On ->', value);
const mode = (value as boolean) ? 'ALWAYS_ON' : 'ALWAYS_OFF';
const url = `${this.accessory.context.device.address}/servlet/rest/chargebox/mode/${mode}`;
await fetch(url, { method: 'POST' });
}
async getOn(): Promise<CharacteristicValue> {
const response = await fetch(
`${this.accessory.context.device.address}/servlet/rest/chargebox/status`,
);
const data = (await response.json()) as GaroStatusResponse;
const isOn = data.mode === 'ALWAYS_ON';
this.platform.log.debug('Get Characteristic On ->', isOn);
return isOn;
}
async getInUse(): Promise<CharacteristicValue> {
const response = await fetch(
`${this.accessory.context.device.address}/servlet/rest/chargebox/status`,
);
const data = (await response.json()) as GaroStatusResponse;
const inUse = data.mainCharger.connector !== 'INITIALIZATION';
this.platform.log.debug(`Get Characteristic OutletInUse (${data.mainCharger.connector}) ->`, inUse);
return inUse;
}
}