forked from lyonlai/homebridge-unifi-guest-wlan-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
296 lines (240 loc) · 8.24 KB
/
index.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const unifi = require("node-unifi");
const Bluebird = require("bluebird");
const R = require("ramda");
const retry = require("bluebird-retry");
let Accessory, Service, Characteristic, UUIDGen;
module.exports = function(homebridge) {
console.log("homebridge API version: " + homebridge.version);
// Accessory must be created from PlatformAccessory Constructor
Accessory = homebridge.platformAccessory;
// Service and Characteristic are from hap-nodejs
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
// For platform plugin to be considered as dynamic platform plugin,
// registerPlatform(pluginName, platformName, constructor, dynamic), dynamic must be true
homebridge.registerPlatform(
"homebridge-unifi-guest-wifi-platform",
"UnifyGuestWifiPlatform",
UnifyGuestWifiPlatform,
true
);
};
// Platform constructor
// config may be null
// api may be null if launched from old homebridge version
function UnifyGuestWifiPlatform(log, config, api) {
log("UnifyGuestWifiPlatform Init");
var platform = this;
this.log = log;
this.config = config;
this.accessories = {};
this.ready = false;
this.controllerConfig = config.controller;
this.unifiController = new unifi.Controller(
this.controllerConfig.address,
this.controllerConfig.port || 8443
);
Bluebird.promisifyAll(this.unifiController);
if (api) {
// Save the API object as plugin needs to register new accessory via this object
this.api = api;
// Listen to event "didFinishLaunching", this means homebridge already finished loading cached accessories.
// Platform Plugin should only register new accessory that doesn't exist in homebridge after this event.
// Or start discover new accessories.
this.api.on("didFinishLaunching", () => {
retry(
() => {
this.log(
"Logging into Unifi Controller",
this.controllerConfig.address,
this.controllerConfig.port
);
return this.unifiController
.loginAsync(
this.controllerConfig.username,
this.controllerConfig.password
)
.then(() => {
this.log("Logged into Unifi Controller");
return this.loadGuestWifi().then(() => {
const interval = this.controllerConfig.updateInterval || 10000;
this.log(`Setting up update interval: ${interval}`);
this.updateInterval = setInterval(() => {
this.log("Updating Guest Wifi Controller");
this.loadGuestWifi();
}, interval);
platform.log("DidFinishLaunching");
});
})
.catch(e => {
this.log("error loading guest wifies", e);
throw e;
});
},
{
interval: 6000,
max_tries: 10,
throw_original: true
}
).then(() => {
this.ready = true;
});
});
}
}
UnifyGuestWifiPlatform.prototype.loadGuestWifi = async function() {
this.log("loading guest wifies");
const sites = await this.unifiController.getSitesAsync();
const siteNames = sites.map(site => site.name);
let wlans = await this.unifiController.getWLanSettingsAsync(siteNames);
wlans = R.compose(R.filter(wlan => wlan.is_guest), R.flatten)(wlans);
this.log("loaded wlan settings", wlans);
if (wlans.length <= 0) {
return;
}
this.log(`loaded ${wlans.length} wlans`, wlans.map(wlan => wlan.name));
await this.addGuestWifiAccessories(sites, wlans);
};
UnifyGuestWifiPlatform.prototype.addGuestWifiAccessories = async function(
sites,
wlans
) {
this.log(`adding ${wlans.length} guest wifi`, sites);
const sitesBYId = R.indexBy(R.prop("_id"), sites);
await Bluebird.all(
wlans.map(wlan => {
const site = sitesBYId[wlan["site_id"]];
return this.addGuestWifiAccessory(site, wlan);
})
);
};
UnifyGuestWifiPlatform.prototype.generateAccessoryName = (site, wlan) =>
`${site.name}-${wlan.name}`;
UnifyGuestWifiPlatform.prototype.generateAccessoryId = (site, wlan) =>
UUIDGen.generate(`${site._id}-${wlan._id}`);
UnifyGuestWifiPlatform.prototype.setupAccessory = function(
accessory,
configure
) {
const context = accessory.context;
this.log(
`Setting up accessory for Guest Wifi: ${this.generateAccessoryName(
context.site,
context.wlan
)}`
);
accessory.on("identify", (paired, callback) => {
this.log(newAccessory.displayName, "Identify!!!");
callback();
});
// Plugin can save context on accessory to help restore accessory in configureAccessory()
// newAccessory.context.something = "Something"
// Make sure you provided a name for service, otherwise it may not visible in some HomeKit apps
this.accessories[accessory.UUID] = accessory;
this.log(
`getting service switch ${accessory.getService(
Service.Switch
)}, ${configure}`
);
if (configure && !accessory.getService(Service.Switch)) {
accessory.addService(Service.Switch, context.wlan.name);
}
accessory
.getService(Service.Switch)
.getCharacteristic(Characteristic.On)
.on("set", async (value, callback) => {
if (!this.ready) {
callback(new Error("unifi controller is not ready yet."));
}
const { site, wlan } = accessory.context;
this.log(
accessory.displayName,
`Guest Wifi -> ${this.generateAccessoryName(
context.site,
context.wlan
)}, ` + value
);
await this.unifiController.disableWLanAsync(site.name, wlan._id, !value);
wlan.enabled = value;
callback();
})
.on("get", callback => {
if (!this.ready) {
callback(new Error("unifi controller is not ready yet."));
}
const { site, wlan } = accessory.context;
this.log(
accessory.displayName,
`Guest Wifi -> ${this.generateAccessoryName(site, wlan)}, ` +
wlan.enabled
);
callback(null, wlan.enabled);
});
};
UnifyGuestWifiPlatform.prototype.addGuestWifiAccessory = async function(
site,
wlan
) {
const name = this.generateAccessoryName(site, wlan);
this.log(`Adding Guest Wifi: ${name}`);
const uuid = this.generateAccessoryId(site, wlan);
if (this.accessories[uuid]) {
this.log(`Guest Wifi: ${name}, exists, updating value instead`);
this.accessories[uuid].context = { site, wlan };
return;
}
this.log(`Guest Wifi: ${name}, does not exists, adding new accessory`);
var newAccessory = new Accessory(name, uuid);
newAccessory.context = {
site,
wlan
};
this.setupAccessory(newAccessory, true);
this.registerAccessory(newAccessory);
};
UnifyGuestWifiPlatform.prototype.registerAccessory = function(accessory) {
const { site, wlan } = accessory.context;
this.log(
`Registering accessory for Guest Wifi: ${this.generateAccessoryName(
site,
wlan
)}`
);
this.api.registerPlatformAccessories(
"homebridge-unifi-guest-wifi-platform",
"UnifyGuestWifiPlatform",
[accessory]
);
};
// Function invoked when homebridge tries to restore cached accessory.
// Developer can configure accessory at here (like setup event handler).
// Update current value.
UnifyGuestWifiPlatform.prototype.configureAccessory = function(accessory) {
this.log(accessory.displayName, "Configure Accessory", accessory);
if (this.accessories[accessory.UUID]) {
return;
}
// Set the accessory to reachable if plugin can currently process the accessory,
// otherwise set to false and update the reachability later by invoking
// accessory.updateReachability()
accessory.reachable = true;
this.setupAccessory(accessory, true);
};
UnifyGuestWifiPlatform.prototype.updateAccessoriesReachability = function() {
this.log("Update Reachability");
for (var index in this.accessories) {
var accessory = this.accessories[index];
accessory.updateReachability(false);
}
};
// Sample function to show how developer can remove accessory dynamically from outside event
UnifyGuestWifiPlatform.prototype.removeAccessory = function() {
this.log("Remove Accessory");
this.api.unregisterPlatformAccessories(
"homebridge-samplePlatform",
"SamplePlatform",
this.accessories
);
this.accessories = [];
};