generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.ts
118 lines (102 loc) · 4.11 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
API,
DynamicPlatformPlugin,
Logger,
PlatformAccessory,
PlatformConfig,
Service,
Characteristic,
APIEvent,
} from 'homebridge';
import {DEFAULT_ADDRESS, DEFAULT_BROADCAST_ADDRESS, PLATFORM_NAME, PLUGIN_NAME} from './settings';
import * as broadlink from 'node-broadlink';
import {Sp4b} from 'node-broadlink';
import {PlugAccessory} from './plugAccessory';
/**
* HomebridgePlatform
* This class is the main constructor for your plugin, this is where you should
* parse the user config and discover/register accessories with Homebridge.
*/
export class BroadlinkHomebridgePlatform implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
private readonly address = DEFAULT_ADDRESS;
private readonly broadcastAddress = DEFAULT_BROADCAST_ADDRESS;
// 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);
if(config.address) {
this.address = config.address;
}
if(config.broadcastAddress) {
this.broadcastAddress = config.broadcastAddress;
}
// 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(APIEvent.DID_FINISH_LAUNCHING, async () => {
log.debug('Executed didFinishLaunching callback');
// run the method to discover / register your devices as accessories
await this.discoverDevices();
});
}
/**
* This function is invoked when homebridge restores cached accessories from disk at startup.
* It should be used to setup 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.accessories.push(accessory);
}
/**
* This is an example method showing how to register discovered accessories.
* Accessories must only be registered once, previously created accessories
* must not be registered again to prevent "duplicate UUID" errors.
*/
async discoverDevices() {
const devices = await broadlink.discover(undefined, {address: this.address, broadcastAddress: this.broadcastAddress});
this.log.info('test:', devices, devices.map(device => device.constructor.name.toLowerCase()));
for (const device of devices) {
switch (device.constructor.name.toLowerCase()) {
case 'sp4b': {
const plug = device as Sp4b;
const {mac, name, manufacturer, model} = plug;
await plug.auth();
const uuid = this.api.hap.uuid.generate(mac.join('.'));
const existingAccessory = this.accessories.find(
accessory => accessory.UUID === uuid,
);
if (existingAccessory) {
// the accessory already exists
this.log.info(
'Restoring existing accessory from cache:',
existingAccessory.displayName,
);
new PlugAccessory(this, existingAccessory, manufacturer, model, plug);
} else {
this.log.info(
'registering new accessory:',
name,
);
const accessory = new this.api.platformAccessory(
name,
uuid,
);
accessory.context.host = device.host;
new PlugAccessory(this, accessory, manufacturer, model, plug);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
accessory,
]);
}
}
}
}
}
}