-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
164 lines (128 loc) · 5.1 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const Plejd = require('./plejd');
var Accessory, Service, Characteristic, UUIDGen;
module.exports = function(homebridge) {
// Accessory must be created from PlatformAccessory Constructor
Accessory = homebridge.platformAccessory;
// Service and Characteristic are from hap-nodejs
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform("homebridge-plejd", "plejd", PlejdPlatform, false);
}
// Platform constructor
// config may be null
// api may be null if launched from old homebridge version
function PlejdPlatform(log, config, api) {
var platform = this;
this.log = log;
this.config = config;
this.accessories = [];
this.api = api
var cryptoKey = Buffer.from(config.key.replace(/-/g, ''), 'hex');
this.plejd = new Plejd(cryptoKey, log)
this.plejd.on('updateDevice', this.updateDeviceFromPlejd.bind(this))
if (api) {
// Save the API object as plugin needs to register new accessory via this object
this.api = api;
// Listen to event "didFinishLaunching", this means homebridge already finished loading cached accessories.
// Platform Plugin should only register new accessory that doesn't exist in homebridge after this event.
// Or start discover new accessories.
this.api.on('didFinishLaunching', function() {
// Setup
for (const device of config.devices) {
this.addAccessory(device)
}
}.bind(this));
}
}
PlejdPlatform.prototype.updateDeviceFromPlejd = function(device, state, dim) {
var accessory = this.accessories.find( accessory => accessory.context.config.identifier === device)
if (accessory == null) {
return;
}
if (accessory.getService(Service.Lightbulb)) {
var service = accessory.getService(Service.Lightbulb)
} else if (accessory.getService(Service.Switch)) {
var service = accessory.getService(Service.Switch)
}
service.getCharacteristic(Characteristic.On).updateValue(state);
if(accessory.context.config.dimming && dim) {
if (dim !== 0) {
var dimValue = (100/255) * dim
service.getCharacteristic(Characteristic.Brightness).updateValue(dimValue);
} else {
service.getCharacteristic(Characteristic.Brightness).updateValue(1);
}
}
}
// Function invoked when homebridge tries to restore cached accessory.
// Developer can configure accessory at here (like setup event handler).
// Update current value.
PlejdPlatform.prototype.configureAccessory = function(accessory) {
this.log(accessory.displayName, "Configure Accessory");
// Set the accessory to reachable if plugin can currently process the accessory,
// otherwise set to false and update the reachability later by invoking
// accessory.updateReachability()
accessory.reachable = true;
this.attatchHandlersToAccessory(accessory)
this.accessories.push(accessory);
}
// Sample function to show how developer can add accessory dynamically from outside event
PlejdPlatform.prototype.addAccessory = function(config) {
var newAccessory = this.createPlejdAccessory(config)
if (newAccessory == null) {
return;
}
// Already added?
if (this.accessories.find( accessory => accessory.UUID === newAccessory.UUID) != null) {
return
}
this.log("Adding Accessory");
this.accessories.push(newAccessory);
this.api.registerPlatformAccessories("homebridge-plejd", "plejd", [newAccessory]);
}
PlejdPlatform.prototype.createPlejdAccessory = function(config) {
var newAccessory = new Accessory(config.name, UUIDGen.generate(config.identifier.toString()));
newAccessory.context.config = config;
if (config.model === 'CTR-01') {
newAccessory.addService(Service.Switch, config.name);
} else if (config.model === 'DIM-01') {
newAccessory.addService(Service.Lightbulb, config.name);
} else if (config.model === 'DIM-02') {
newAccessory.addService(Service.Lightbulb, config.name);
} else {
this.log('Unknown accessory model ' + config.model)
return null;
}
this.attatchHandlersToAccessory(newAccessory);
return newAccessory;
}
PlejdPlatform.prototype.attatchHandlersToAccessory = function(accessory) {
var platform = this;
accessory.on('identify', function(paired, callback) {
platform.log(newAccessory.displayName, "Identify!!!");
callback();
});
if (accessory.getService(Service.Lightbulb)) {
var service = accessory.getService(Service.Lightbulb)
} else if (accessory.getService(Service.Switch)) {
var service = accessory.getService(Service.Switch)
}
service.getCharacteristic(Characteristic.On).on('set', function(value, callback) {
if (value) {
platform.plejd.turnOn(accessory.context.config.identifier)
} else {
platform.plejd.turnOff(accessory.context.config.identifier)
}
callback();
});
if(accessory.context.config.dimming) {
service.getCharacteristic(Characteristic.Brightness).on('set', function(value, callback) {
if (value !== 0) {
var dimValue = Math.round((255/100) * value)
platform.plejd.turnOn(accessory.context.config.identifier, dimValue)
}
callback();
});
}
}