-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
115 lines (100 loc) · 3.23 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
const RpiGpioRts = require('./RpiGpioRts');
let Service, Characteristic;
module.exports = homebridge => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-rpi-rts', 'Somfy RTS Remote', SomfyRtsRemoteAccessory);
};
/**
* Class simulating a Somfy RTS Remote Accessory for Homebridge
* with 4 'stateless' switches: Up, Down, My, Prog
*
* @class SomfyRtsRemoteAccessory
*/
class SomfyRtsRemoteAccessory {
/**
* Constructor of the class SomfyRtsRemoteAccessory
*
* @constructor
* @param {Object} log - The Homebridge log
* @param {Object} config - The Homebridge config data filtered for this item
*/
constructor(log, config) {
this.log = log;
if (!config || !config.name || !config.id) {
throw new Error(`Invalid or missing configuration.`);
}
this.config = config;
this.emitter = new RpiGpioRts(log, config);
// Delay to reset the switch after being pressed
this.delay = 500;
this.buttons = ['Up', 'Down', 'My'];
if (this.config.prog === true) this.buttons.push('Prog');
// Create an object such as {'Up': false, 'Down': false, ...}
this.states = this.buttons.reduce((acc, cur) => {
acc[cur] = false;
return acc;
}, {});
this.switchServices = {};
this.buttons.forEach(button => {
this.switchServices[button] = new Service.Switch(`${this.config.name} ${button}`, button);
this.switchServices[button]
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this, button))
.on('set', this.setOn.bind(this, button));
});
this.log.debug(`Initialized accessory`);
}
/**
* Getter for the 'On' characteristic of the 'Switch' service
*
* @method getOn
* @param {Function} callback - A callback function from Homebridge
* @param {String} button - 'Up', 'Down', 'My', 'Prog'
*/
getOn(button, callback) {
this.log.debug(`Function getOn called for button ${button}`);
const value = this.states[button];
callback(null, value);
}
/**
* Setter for the 'On' characteristic of the 'Switch' service
*
* @method setOn
* @param {Object} value - The value for the characteristic
* @param {Function} callback - A callback function from Homebridge
* @param {String} button - 'Up', 'Down', 'My', 'Prog'
*/
setOn(button, value, callback) {
this.log.debug(`Function setOn called for button ${button} with value ${value}`);
this.states[button] = value;
if (value === true) {
this.emitter.sendCommand(button);
this.resetSwitchWithTimeout(button);
}
callback(null);
}
/**
* Reset the switch to false to simulate a stateless behavior
*
* @method resetSwitchWithTimeout
* @param {String} button - 'Up', 'Down', 'My', 'Prog'
*/
resetSwitchWithTimeout(button) {
this.log.debug(`Function resetSwitchWithTimeout called for button ${button}`);
setTimeout(function() {
this.switchServices[button].setCharacteristic(Characteristic.On, false);
}.bind(this), this.delay);
}
/**
* Mandatory method for Homebridge
* Return a list of services provided by this accessory
*
* @method getServices
* @return {Array} - An array containing the services
*/
getServices() {
this.log.debug(`Function getServices called`);
return Object.values(this.switchServices);
}
}