forked from PeteLawrence/homebridge-people
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
97 lines (82 loc) · 2.12 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
const { Accessory } = require('homebridge-plugin-helpers');
const { NetworkObserver } = require('./network');
module.exports = function (homebridge) {
PeoplePlusAccessory.register(homebridge);
};
class PeoplePlusAccessory extends Accessory {
static get pluginName() {
return "homebridge-people-plus";
}
static get accessoryName() {
return "PeoplePlus";
}
constructor(homebridge, log, config, api) {
super();
// Save args
this.log = log;
this.config = config;
this.api = api;
// Setup Homebridge
this.Service = homebridge.hap.Service;
this.Characteristic = homebridge.hap.Characteristic;
// Setup Service
this.isDetected = false;
this.service = new this.Service.MotionSensor(this.name);
this.setupCharacteristics();
this.setupDeviceObserver();
}
get name() {
return this.config.name;
}
get manufacturer() {
return "Samsung TV";
}
get model() {
return "1.0.0";
}
get serialNumber() {
return this.config.device.mac;
}
setupCharacteristics() {
const { Characteristic } = this;
const motion = this.service
.getCharacteristic(Characteristic.MotionDetected)
.on('get', (callback) => callback(null, this.isDetected))
;
const active = this.service
.getCharacteristic(Characteristic.StatusActive)
.on('get', (callback) => callback(null, true))
;
this.characteristics = {
motion,
active,
};
}
setupDeviceObserver() {
const { net } = this;
const { mac } = this.config.device;
net.on(`device:mac:${mac}`, device =>
this.setDetected(device.active)
);
net.on(`connected:mac:${mac}`, (device) =>
this.isDetected(true)
);
net.on(`disconnected:mac:${mac}`, (device) =>
this.isDetected(false)
);
}
static get net() {
if (!this._net) {
this._net = new NetworkObserver();
}
return this._net;
}
get net() {
return this.constructor.net;
}
setDetected(isDetected) {
const { motion } = this.characteristics;
this.isDetected = isDetected;
motion.updateValue(isDetected);
}
}