homebridge-multiple-switch 1.1.2 → 1.1.3

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.
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "pluginAlias": "MultipleSwitchAccessory",
3
3
  "pluginType": "accessory",
4
+ "singular": false,
4
5
  "headerDisplay": "Multiple Switch Accessory",
5
6
  "schema": {
6
7
  "type": "object",
@@ -15,9 +16,24 @@
15
16
  "type": "string",
16
17
  "default": "independent",
17
18
  "oneOf": [
18
- { "title": "Independent", "enum": ["independent"] },
19
- { "title": "Master", "enum": ["master"] },
20
- { "title": "Single", "enum": ["single"] }
19
+ {
20
+ "title": "Independent",
21
+ "enum": [
22
+ "independent"
23
+ ]
24
+ },
25
+ {
26
+ "title": "Master",
27
+ "enum": [
28
+ "master"
29
+ ]
30
+ },
31
+ {
32
+ "title": "Single",
33
+ "enum": [
34
+ "single"
35
+ ]
36
+ }
21
37
  ]
22
38
  },
23
39
  "switches": {
@@ -29,13 +45,68 @@
29
45
  "name": {
30
46
  "title": "Switch Name",
31
47
  "type": "string"
48
+ },
49
+ "type": {
50
+ "title": "Switch Type",
51
+ "type": "string",
52
+ "default": "outlet",
53
+ "oneOf": [
54
+ {
55
+ "title": "Switch",
56
+ "enum": [
57
+ "switch"
58
+ ]
59
+ },
60
+ {
61
+ "title": "Outlet",
62
+ "enum": [
63
+ "outlet"
64
+ ]
65
+ },
66
+ {
67
+ "title": "Lightbulb",
68
+ "enum": [
69
+ "lightbulb"
70
+ ]
71
+ },
72
+ {
73
+ "title": "Fan",
74
+ "enum": [
75
+ "fan"
76
+ ]
77
+ }
78
+ ]
79
+ },
80
+ "defaultState": {
81
+ "title": "Default State",
82
+ "type": "boolean",
83
+ "default": false
84
+ },
85
+ "delayOff": {
86
+ "title": "Auto Turn Off (ms)",
87
+ "type": "number",
88
+ "default": 0,
89
+ "minimum": 0
32
90
  }
33
91
  },
34
- "required": ["name"]
92
+ "required": [
93
+ "name",
94
+ "type"
95
+ ]
35
96
  },
36
- "default": [{ "name": "Switch 1" }]
97
+ "default": [
98
+ {
99
+ "name": "Switch 1",
100
+ "type": "outlet",
101
+ "defaultState": false,
102
+ "delayOff": 0
103
+ }
104
+ ]
37
105
  }
38
106
  },
39
- "required": ["name", "switches"]
107
+ "required": [
108
+ "name",
109
+ "switches"
110
+ ]
40
111
  }
41
112
  }
package/index.js CHANGED
@@ -1,72 +1,78 @@
1
- let Accessory, Service, Characteristic, UUIDGen;
1
+ let Accessory, Service, Characteristic;
2
2
 
3
- module.exports = (api) => {
4
- Accessory = api.hap.Accessory;
5
- Service = api.hap.Service;
6
- Characteristic = api.hap.Characteristic;
7
- UUIDGen = api.hap.uuid;
3
+ module.exports = (homebridge) => {
4
+ Accessory = homebridge.platformAccessory || homebridge.hap.Accessory;
5
+ Service = homebridge.hap.Service;
6
+ Characteristic = homebridge.hap.Characteristic;
8
7
 
9
- api.registerAccessory("MultipleSwitchAccessory", MultipleSwitchAccessory);
8
+ homebridge.registerAccessory(
9
+ 'homebridge-multiple-switch',
10
+ 'MultipleSwitchAccessory',
11
+ MultipleSwitchAccessory
12
+ );
10
13
  };
11
14
 
12
15
  class MultipleSwitchAccessory {
13
- constructor(log, config, api) {
16
+ constructor(log, config) {
14
17
  this.log = log;
15
18
  this.config = config;
16
- this.name = config.name;
19
+ this.name = config.name || 'Multiple Switch';
17
20
  this.switches = config.switches || [];
18
- this.mode = config.switchBehavior || "independent";
19
21
  this.services = [];
20
22
 
21
- const uuid = UUIDGen.generate(this.name);
22
- this.accessory = new Accessory(this.name, uuid);
23
- this.switchStates = Array(this.switches.length).fill(false);
23
+ const infoService = new Service.AccessoryInformation()
24
+ .setCharacteristic(Characteristic.Manufacturer, 'Custom')
25
+ .setCharacteristic(Characteristic.Model, 'MultipleSwitch')
26
+ .setCharacteristic(Characteristic.SerialNumber, 'v1.0.0');
24
27
 
25
- this.switches.forEach((sw, index) => {
26
- const service = new Service.Outlet(sw.name, "switch_" + index);
27
- service
28
- .getCharacteristic(Characteristic.On)
29
- .onGet(() => this.switchStates[index])
30
- .onSet((value) => this.setState(index, value));
31
- this.accessory.addService(service);
32
- this.services.push(service);
33
- });
28
+ this.services.push(infoService);
29
+
30
+ const serviceTypes = {
31
+ switch: Service.Switch,
32
+ outlet: Service.Outlet,
33
+ lightbulb: Service.Lightbulb,
34
+ fan: Service.Fan
35
+ };
36
+
37
+ this.switches.forEach((conf, index) => {
38
+ const switchName = conf.name || `Switch ${index + 1}`;
39
+ const type = (conf.type || 'outlet').toLowerCase();
40
+ const ServiceClass = serviceTypes[type];
41
+
42
+ if (!ServiceClass) {
43
+ this.log.warn(`"${switchName}" üçün tanınmayan növ: "${type}". Default olaraq Outlet istifadə olunur.`);
44
+ return;
45
+ }
34
46
 
35
- if (this.mode === "master") {
36
- const master = new Service.Outlet("Master", "master");
37
- master
47
+ conf.state = conf.defaultState === true;
48
+
49
+ const service = new ServiceClass(switchName, `subtype-${index}`);
50
+
51
+ service
38
52
  .getCharacteristic(Characteristic.On)
39
- .onGet(() => this.switchStates.every((s) => s))
40
- .onSet((value) => this.setAll(value));
41
- this.accessory.addService(master);
42
- this.services.unshift(master);
43
- }
44
- }
53
+ .on('get', (callback) => {
54
+ callback(null, conf.state);
55
+ })
56
+ .on('set', (value, callback) => {
57
+ conf.state = value;
58
+ this.log(`"${switchName}" vəziyyəti dəyişdi: ${value}`);
45
59
 
46
- setState(index, value) {
47
- if (this.mode === "single" && value) {
48
- this.switchStates = this.switchStates.map((_, i) => i === index);
49
- } else {
50
- this.switchStates[index] = value;
51
- }
52
- this.updateStates();
53
- }
60
+ if (value && conf.delayOff && conf.delayOff > 0) {
61
+ setTimeout(() => {
62
+ conf.state = false;
63
+ service.getCharacteristic(Characteristic.On).updateValue(false);
64
+ this.log(`"${switchName}" auto turn off tətbiq olundu`);
65
+ }, conf.delayOff); // saniyəni ms-ə çeviririk
66
+ }
54
67
 
55
- setAll(value) {
56
- this.switchStates = this.switchStates.map(() => value);
57
- this.updateStates();
58
- }
68
+ callback();
69
+ });
59
70
 
60
- updateStates() {
61
- this.switches.forEach((_, i) => {
62
- this.services
63
- .find((s) => s.subtype === "switch_" + i)
64
- ?.getCharacteristic(Characteristic.On)
65
- .updateValue(this.switchStates[i]);
71
+ this.services.push(service);
66
72
  });
67
73
  }
68
74
 
69
75
  getServices() {
70
- return [this.accessory.getService(Service.AccessoryInformation), ...this.accessory.services];
76
+ return this.services;
71
77
  }
72
78
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-multiple-switch",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Multiple switch accessory for Homebridge",
5
5
  "main": "index.js",
6
6
  "author": "Azad Aydınlı",