forked from Pythe1337N/homebridge-http-doorbell-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoorbell.js
More file actions
69 lines (59 loc) · 1.9 KB
/
Copy pathDoorbell.js
File metadata and controls
69 lines (59 loc) · 1.9 KB
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
class Doorbell {
constructor(config, index, Service, Characteristic, log) {
this.Service = Service;
this.Characteristic = Characteristic;
this.log = log;
const {
name,
id,
debounce,
manufacturer,
model,
serialNumber,
} = config;
this.name = name;
this.id = id || index;
this.debounce = debounce || 2;
this.manufacturer = manufacturer || 'Pythe1337N Inc.';
this.model = model || 'Pythe1337N Doorbell';
this.serialNumber = serialNumber || this.id;
this.state = 0;
this.busy = false;
}
stepState() {
if (this.state === 1) {
this.state = 2;
} else {
this.state = 1;
}
}
getState(callback) {
callback(null, this.state);
}
getServices() {
const info = new this.Service.AccessoryInformation();
info.setCharacteristic(this.Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(this.Characteristic.Model, this.model)
.setCharacteristic(this.Characteristic.SerialNumber, this.serialNumber);
this.service = new this.Service.Doorbell(this.name);
this.service.getCharacteristic(this.Characteristic.ProgrammableSwitchEvent)
.on('get', this.getState.bind(this));
return [info, this.service];
}
ring() {
if (!this.busy) {
this.busy = true;
this.service.getCharacteristic(this.Characteristic.ProgrammableSwitchEvent).updateValue(this.state);
this.stepState();
}
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = setTimeout(() => {
this.busy = false;
this.timeout = undefined;
}, this.debounce * 1000);
return true;
}
}
module.exports = Doorbell;