homebridge-multiple-switch 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config.schema.json +51 -0
- package/index.js +61 -0
- package/package.json +19 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"pluginAlias": "MultipleSwitchAccessory",
|
|
3
|
+
"pluginType": "accessory",
|
|
4
|
+
"schema": {
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"name": {
|
|
8
|
+
"title": "Accessory Name",
|
|
9
|
+
"type": "string",
|
|
10
|
+
"required": true
|
|
11
|
+
},
|
|
12
|
+
"addMasterSwitch": {
|
|
13
|
+
"title": "Add Master Switch",
|
|
14
|
+
"type": "boolean",
|
|
15
|
+
"default": false
|
|
16
|
+
},
|
|
17
|
+
"switchNames": {
|
|
18
|
+
"title": "Switches",
|
|
19
|
+
"type": "array",
|
|
20
|
+
"items": {
|
|
21
|
+
"type": "string",
|
|
22
|
+
"title": "Switch Name",
|
|
23
|
+
"required": true
|
|
24
|
+
},
|
|
25
|
+
"minItems": 1,
|
|
26
|
+
"default": ["Switch 1"]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"layout": [
|
|
31
|
+
{
|
|
32
|
+
"key": "name",
|
|
33
|
+
"title": "Accessory Name"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"key": "addMasterSwitch",
|
|
37
|
+
"title": "Add Master Switch"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"key": "switchNames",
|
|
41
|
+
"title": "Switches",
|
|
42
|
+
"type": "array",
|
|
43
|
+
"items": {
|
|
44
|
+
"key": "switchNames[]",
|
|
45
|
+
"title": "Switch Name",
|
|
46
|
+
"required": true
|
|
47
|
+
},
|
|
48
|
+
"add": "ADD SWITCH"
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
let Service, Characteristic;
|
|
2
|
+
|
|
3
|
+
module.exports = (homebridge) => {
|
|
4
|
+
Service = homebridge.hap.Service;
|
|
5
|
+
Characteristic = homebridge.hap.Characteristic;
|
|
6
|
+
homebridge.registerAccessory("MultipleSwitchAccessory", MultipleSwitchAccessory);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
class MultipleSwitchAccessory {
|
|
10
|
+
constructor(log, config) {
|
|
11
|
+
this.log = log;
|
|
12
|
+
this.name = config.name || "Multiple Switch";
|
|
13
|
+
this.addMasterSwitch = config.addMasterSwitch || false;
|
|
14
|
+
this.switchNames = config.switchNames.map((name, index) => name || `Switch ${index + 1}`);
|
|
15
|
+
|
|
16
|
+
this.switchStates = Array(this.switchNames.length).fill(false);
|
|
17
|
+
this.services = [];
|
|
18
|
+
|
|
19
|
+
if (this.addMasterSwitch) {
|
|
20
|
+
const masterSwitchService = new Service.Switch(`${this.name} Master`, 'masterSwitch');
|
|
21
|
+
masterSwitchService.getCharacteristic(Characteristic.On)
|
|
22
|
+
.on('set', this.setMasterSwitch.bind(this))
|
|
23
|
+
.on('get', this.getMasterSwitch.bind(this));
|
|
24
|
+
this.services.push(masterSwitchService);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this.switchNames.forEach((name, i) => {
|
|
28
|
+
const switchService = new Service.Switch(name, `switch${i + 1}`);
|
|
29
|
+
switchService.getCharacteristic(Characteristic.On)
|
|
30
|
+
.on('set', this.setOn.bind(this, i))
|
|
31
|
+
.on('get', this.getOn.bind(this, i));
|
|
32
|
+
this.services.push(switchService);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
setMasterSwitch(value, callback) {
|
|
37
|
+
this.switchStates = this.switchStates.map(() => value);
|
|
38
|
+
this.services.slice(1).forEach((service, i) => {
|
|
39
|
+
service.getCharacteristic(Characteristic.On).updateValue(value);
|
|
40
|
+
});
|
|
41
|
+
callback(null);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getMasterSwitch(callback) {
|
|
45
|
+
const allOn = this.switchStates.every(state => state === true);
|
|
46
|
+
callback(null, allOn);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
setOn(index, value, callback) {
|
|
50
|
+
this.switchStates[index] = value;
|
|
51
|
+
callback(null);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getOn(index, callback) {
|
|
55
|
+
callback(null, this.switchStates[index]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getServices() {
|
|
59
|
+
return this.services;
|
|
60
|
+
}
|
|
61
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "homebridge-multiple-switch",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Multiple switch accessory for Homebridge",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"author": "Azad Aydınlı",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"homebridge-plugin"
|
|
10
|
+
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/azadaydinli/homebridge-multiple-switch.git"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=14.17.0",
|
|
17
|
+
"homebridge": ">=1.3.0"
|
|
18
|
+
}
|
|
19
|
+
}
|