generated from homebridge/homebridge-plugin-template
/
platformAccessory.ts
64 lines (52 loc) · 2.03 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
import { Service, PlatformAccessory, CharacteristicValue } from "homebridge";
import { UnifiFirewallPlatform } from "./platform";
import { UnifiFirewallRuleConfig } from "./config";
import { FWRule } from "unifi-client";
export class UnifiFirewallSwitch {
private service: Service;
constructor(
private readonly platform: UnifiFirewallPlatform,
private readonly accessory: PlatformAccessory<{
rule: UnifiFirewallRuleConfig;
}>,
private readonly fwRule: FWRule,
private readonly invert: boolean
) {
// set accessory information
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.accessory
.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, "Ubiquiti")
.setCharacteristic(this.platform.Characteristic.Model, "Firewall-Rule")
.setCharacteristic(
this.platform.Characteristic.SerialNumber,
this.accessory.context.rule.id
);
this.service =
this.accessory.getService(this.platform.Service.Switch) ||
this.accessory.addService(this.platform.Service.Switch);
this.service.setCharacteristic(
this.platform.Characteristic.Name,
accessory.context.rule.name
);
this.service
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this)) // SET - bind to the `setOn` method below
.onGet(this.getOn.bind(this)); // GET - bind to the `getOn` method below
}
async setOn(value: CharacteristicValue) {
const newValue = value as boolean;
this.fwRule.enabled = this.invert ? !newValue : newValue;
await this.fwRule.save();
this.platform.log.debug(
`Set Unifi Firewall ${this.fwRule._id}: ${newValue} (Inverted? ${this.invert})`
);
}
async getOn(): Promise<CharacteristicValue> {
const isOn = this.invert ? !this.fwRule.enabled : this.fwRule.enabled;
this.platform.log.debug(
`Is Unifi Firewall ${this.fwRule._id} on? ${isOn} (Inverted? ${this.invert})`
);
return isOn;
}
}