forked from thoukydides/homebridge-skybell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (64 loc) · 2.66 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
// Homebridge plugin for SkyBell HD video doorbells
// Copyright © 2017, 2020 Alexander Thoukydides
'use strict';
let SkyBellAccount = require('./skybell_account');
let SkyBellAccessory = require('./accessory');
let Webhooks = require('./webhooks');
// Platform identifiers
const PLUGIN_NAME = 'homebridge-skybell';
const PLATFORM_NAME = 'SkyBell';
// Register as a non-dynamic platform
module.exports = homebridge => {
homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME,
SkyBellPlatform, false);
}
// A Homebridge SkyBell platform
class SkyBellPlatform {
// Create a new SkyBell platform object
constructor(log, config, homebridge) {
log('new SkyBellPlatform');
this.log = log;
this.config = config || {};
this.homebridge = homebridge;
// Enumerate SkyBell devices after cached accessories restored
if (homebridge) {
this.homebridge.on('didFinishLaunching',
() => this.finishedLaunching());
} else {
this.finishedLaunching();
}
}
// Required to indicate support for Plugin 2.0 API, but won't be called
configureAccessory(accessory) {
this.log('configureAccessory');
}
// Update list of SkyBell accessories after cache has been restored
finishedLaunching() {
this.log('finishedLaunching');
// Extract the account credentials from the configuration
let user = this.config['username'];
let pass = this.config['password'];
if (!user) this.log.error('Platform ' + PLATFORM_NAME + " configuration is missing 'username' property");
if (!pass) this.log.error('Platform ' + PLATFORM_NAME + " configuration is missing 'password' property");
// Connect to the SkyBell cloud
this.skybellAccount = new SkyBellAccount(user, pass, {
log: this.log.debug.bind(this.log),
callbackAdd: this.addAccessory.bind(this)
});
// Start the webhooks server if configured
if (this.config.port) {
this.webhooks = new Webhooks(this.config.port, {
log: this.log.debug.bind(this.log),
secret: this.config.secret
});
}
}
// Create a new accessory
addAccessory(skybellDevice) {
this.log("addAccessory '" + skybellDevice.name + "'");
let skybell = new SkyBellAccessory(this.log, this.homebridge,
skybellDevice, this.webhooks);
this.homebridge.publishExternalAccessories(PLUGIN_NAME,
[skybell.accessory]);
}
}