forked from anthonywebb/homebridge-cbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdimmer-accessory.js
executable file
·95 lines (76 loc) · 2.8 KB
/
dimmer-accessory.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
'use strict';
let Service;
let Characteristic;
let CBusLightAccessory;
let uuid;
const chalk = require('chalk');
const ms = require('ms');
const cbusUtils = require('../lib/cbus-utils.js');
const FILE_ID = cbusUtils.extractIdentifierFromFileName(__filename);
module.exports = function (_service, _characteristic, _accessory, _uuid) {
Service = _service;
Characteristic = _characteristic;
CBusLightAccessory = _accessory;
uuid = _uuid;
return CBusDimmerAccessory;
};
function CBusDimmerAccessory(platform, accessoryData) {
// initialize parent
CBusLightAccessory.call(this, platform, accessoryData);
// if we have an activeDuration specified, stash it away
if (typeof accessoryData.rampDuration !== `undefined`) {
this.rampDuration = ms(accessoryData.rampDuration);
if (this.rampDuration > ms(`17m`)) {
throw new Error(`accessory '${this.name} rampDuration (${ms(this.rampDuration)}) is greater than maximum (17m)`);
}
this._log(FILE_ID, `constructed`, `ramp up/down over ${this.rampDuration}ms when activated via homebridge`);
}
// register brightness service
this.brightnessC10tic = this.service.getCharacteristic(Characteristic.Brightness);
this.brightnessC10tic
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
}
CBusDimmerAccessory.prototype.getBrightness = function (callback) {
this.client.receiveLevel(this.netId, message => {
this._log(FILE_ID, `getBrightness`, `returned ${message.level}%`);
if (message.level) {
// update level if the level is non-zero
this.brightness = message.level;
}
callback(/* error */ false, /* newValue */ message.level);
}, `getBrightness`);
};
CBusDimmerAccessory.prototype.setBrightness = function (newLevel, callback, context) {
this.brightness = newLevel;
const wasOn = this.isOn;
this.isOn = (this.brightness > 0);
if (context === `event`) {
// context helps us avoid a never-ending loop
callback();
} else {
if (!wasOn && (newLevel === 0)) {
this._log(FILE_ID, `setBrightness`, chalk.green(`swallowing 0%`));
callback();
} else {
this._log(FILE_ID, `setBrightness`, `changing level to ${newLevel}%`);
this.client.setLevel(this.netId, newLevel, function () {
callback();
}, this.rampDuration / 1000, `setBrightness`);
}
}
};
CBusDimmerAccessory.prototype.processClientData = function (err, message) {
if (!err) {
console.assert(typeof message.level !== `undefined`, `CBusDimmerAccessory.processClientData must receive message.level`);
const level = message.level;
// pick up the special cases of 'on' and 'off'
this.onC10tic.setValue((level > 0) ? 1 : 0, undefined, `event`);
// update brightness
if (level === 0) {
this._log(FILE_ID, `processClientData`, `level 0%; interpreting as 'off'`);
} else {
this.brightnessC10tic.setValue(level, undefined, `event`);
}
}
};