-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessoryCreator.js
75 lines (67 loc) · 2.43 KB
/
accessoryCreator.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
//Credit: https://github.com/nfarina/homebridge/blob/master/lib/server.js#L425
function createAccessory(
accessoryInstance,
displayName,
accessoryType,
{ hap: { Service, Accessory, Characteristic, uuid }, platformAccessory }
) {
const services = accessoryInstance.getServices();
// The returned "services" for this accessory are simply an array of new-API-style
// Service instances which we can add to a created HAP-NodeJS Accessory directly.
const accessoryUUID = uuid.generate(accessoryType + ':' + displayName);
const accessory = new platformAccessory(displayName, accessoryUUID, accessoryType);
// listen for the identify event if the accessory instance has defined an identify() method
if (accessoryInstance.identify)
accessory.on('identify', function(paired, callback) {
accessoryInstance.identify(callback);
});
services.forEach(function(service) {
// if you returned an AccessoryInformation service, merge its values with ours
if (service instanceof Service.AccessoryInformation) {
const existingService = accessory.getService(
Service.AccessoryInformation
);
// pull out any values you may have defined
const manufacturer = service.getCharacteristic(
Characteristic.Manufacturer
).value;
const model = service.getCharacteristic(Characteristic.Model).value;
const serialNumber = service.getCharacteristic(
Characteristic.SerialNumber
).value;
const firmwareRevision = service.getCharacteristic(
Characteristic.FirmwareRevision
).value;
const hardwareRevision = service.getCharacteristic(
Characteristic.HardwareRevision
).value;
if (manufacturer)
existingService.setCharacteristic(
Characteristic.Manufacturer,
manufacturer
);
if (model) existingService.setCharacteristic(Characteristic.Model, model);
if (serialNumber)
existingService.setCharacteristic(
Characteristic.SerialNumber,
serialNumber
);
if (firmwareRevision)
existingService.setCharacteristic(
Characteristic.FirmwareRevision,
firmwareRevision
);
if (hardwareRevision)
existingService.setCharacteristic(
Characteristic.HardwareRevision,
hardwareRevision
);
} else {
accessory.addService(service);
}
});
return accessory;
}
module.exports = {
createAccessory
};