-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
138 lines (124 loc) · 5.41 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
// Generated by CoffeeScript 2.4.1
(function() {
// Copyright (c) 2019 Alexander Sporn. All rights reserved.
var Accessory, Characteristic, Enocean, EnoceanPlatform, Service, UUIDGen;
Enocean = require('./Enocean');
Accessory = void 0;
Service = void 0;
Characteristic = void 0;
UUIDGen = void 0;
module.exports = function(homebridge) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform('homebridge-enocean', 'enocean', EnoceanPlatform, true);
};
EnoceanPlatform = function(log, config1, api) {
this.log = log;
this.config = config1;
this.api = api;
this.accessories = {};
this.enocean = new Enocean({
port: this.config.port
});
// @enocean.on 'pressed', (sender, button) =>
// @log sender + ": " + button + " pressed"
// @sendSwitchEvent(sender, button, 1)
this.enocean.on('released', (sender, button) => {
this.log(sender + ": " + button + " released");
return this.setSwitchEventValue(sender, button, Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
});
this.api.on('didFinishLaunching', () => {
var accessory, i, len, ref;
ref = this.config.accessories;
// @log 'DidFinishLaunching'
for (i = 0, len = ref.length; i < len; i++) {
accessory = ref[i];
this.addAccessory(accessory);
}
});
};
EnoceanPlatform.prototype.setSwitchEventValue = function(sender, button, value) {
var accessory, characteristic, i, len, ref, service;
accessory = this.accessories[sender];
if (accessory == null) {
this.log('Unknown sender', sender);
}
ref = accessory.services;
for (i = 0, len = ref.length; i < len; i++) {
service = ref[i];
if (service.UUID === Service.StatelessProgrammableSwitch.UUID && service.subtype === button) {
characteristic = service.getCharacteristic(Characteristic.ProgrammableSwitchEvent);
characteristic.setValue(value);
this.log('Set', accessory.displayName, button, 'value', value);
return;
}
}
this.log('Could not find button', button);
};
EnoceanPlatform.prototype.configureAccessory = function(accessory) {
var serial;
this.log('Configure Accessory:', accessory.displayName);
accessory.reachable = true;
accessory.on('identify', (paired, callback) => {
this.log(accessory.displayName, 'Identify!!!');
callback();
});
serial = accessory.getService(Service.AccessoryInformation).getCharacteristic(Characteristic.SerialNumber).value;
if (serial == null) {
this.api.unregisterPlatformAccessories('homebridge-enocean', 'enocean', [accessory]);
return;
}
this.accessories[serial] = accessory;
this.setSwitchEventValue(serial, 'A0', -1);
this.setSwitchEventValue(serial, 'AI', -1);
this.setSwitchEventValue(serial, 'B0', -1);
this.setSwitchEventValue(serial, 'BI', -1);
};
EnoceanPlatform.prototype.createProgrammableSwitch = function(name, model, serial) {
var accessory, buttonA0, buttonAI, buttonB0, buttonBI, info, label, uuid;
uuid = UUIDGen.generate(serial);
accessory = new Accessory(name, uuid);
accessory.on('identify', (paired, callback) => {
this.log(accessory.displayName, 'Identify!!!');
callback();
});
info = accessory.getService(Service.AccessoryInformation);
info.updateCharacteristic(Characteristic.Manufacturer, "EnOcean").updateCharacteristic(Characteristic.Model, model).updateCharacteristic(Characteristic.SerialNumber, serial).updateCharacteristic(Characteristic.FirmwareRevision, '1.0');
label = new Service.ServiceLabel(accessory.displayName);
label.getCharacteristic(Characteristic.ServiceLabelNamespace).updateValue(Characteristic.ServiceLabelNamespace.ARABIC_NUMERALS);
accessory.addService(label);
buttonAI = this.createProgrammableSwitchButton(accessory.displayName, 1, 'AI');
buttonA0 = this.createProgrammableSwitchButton(accessory.displayName, 2, 'A0');
buttonBI = this.createProgrammableSwitchButton(accessory.displayName, 3, 'BI');
buttonB0 = this.createProgrammableSwitchButton(accessory.displayName, 4, 'B0');
accessory.addService(buttonAI);
accessory.addService(buttonA0);
accessory.addService(buttonBI);
accessory.addService(buttonB0);
return accessory;
};
EnoceanPlatform.prototype.createProgrammableSwitchButton = function(accesoryName, buttonIndex, button) {
var singleButton;
button = new Service.StatelessProgrammableSwitch(accesoryName + ' ' + button, button);
singleButton = {
minValue: Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS,
maxValue: Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS
};
button.getCharacteristic(Characteristic.ProgrammableSwitchEvent).setProps(singleButton);
button.getCharacteristic(Characteristic.ServiceLabelIndex).setValue(buttonIndex);
return button;
};
EnoceanPlatform.prototype.addAccessory = function(config) {
var accessory;
if (this.accessories[config.id] != null) {
return;
}
// @log 'Skip Accessory: ' + config.name
this.log('Add Accessory:', config.name);
accessory = this.createProgrammableSwitch(config.name, config.eep, config.id);
this.accessories[config.id] = accessory;
this.api.registerPlatformAccessories('homebridge-enocean', 'enocean', [accessory]);
};
}).call(this);