@switchbot/homebridge-switchbot 2.11.0 → 2.12.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.
@@ -10,10 +10,16 @@ import { Devices, irDevicesConfig, irdevice } from '../settings';
10
10
  */
11
11
  export class Light {
12
12
  // Services
13
- lightBulbService!: Service;
13
+ lightBulbService?: Service;
14
+ ProgrammableSwitchServiceOn?: Service;
15
+ ProgrammableSwitchServiceOff?: Service;
14
16
 
15
17
  // Characteristic Values
16
18
  On!: CharacteristicValue;
19
+ ProgrammableSwitchEventOn?: CharacteristicValue;
20
+ ProgrammableSwitchOutputStateOn?: CharacteristicValue;
21
+ ProgrammableSwitchEventOff?: CharacteristicValue;
22
+ ProgrammableSwitchOutputStateOff?: CharacteristicValue;
17
23
  FirmwareRevision!: CharacteristicValue;
18
24
 
19
25
  // Config
@@ -41,19 +47,74 @@ export class Light {
41
47
  .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
42
48
  .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
43
49
 
44
- // get the Light service if it exists, otherwise create a new Light service
45
- // you can create multiple services for each accessory
46
- const lightBulbService = `${accessory.displayName} ${device.remoteType}`;
47
- (this.lightBulbService = accessory.getService(this.platform.Service.Lightbulb)
48
- || accessory.addService(this.platform.Service.Lightbulb)), lightBulbService;
50
+ if (!device.irlight?.stateless) {
51
+ // get the Light service if it exists, otherwise create a new Light service
52
+ // you can create multiple services for each accessory
53
+ const lightBulbService = `${accessory.displayName} ${device.remoteType}`;
54
+ (this.lightBulbService = accessory.getService(this.platform.Service.Lightbulb)
55
+ || accessory.addService(this.platform.Service.Lightbulb)), lightBulbService;
49
56
 
50
- this.lightBulbService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
51
- if (!this.lightBulbService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
52
- this.lightBulbService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
57
+
58
+ this.lightBulbService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
59
+ if (!this.lightBulbService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
60
+ this.lightBulbService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
61
+ }
62
+
63
+ // handle on / off events using the On characteristic
64
+ this.lightBulbService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
65
+ } else {
66
+
67
+ // create a new Stateful Programmable Switch service
68
+ const ProgrammableSwitchServiceOn = `${accessory.displayName} ${device.remoteType} On`;
69
+ (this.ProgrammableSwitchServiceOn = accessory.getService(this.platform.Service.StatefulProgrammableSwitch)
70
+ || accessory.addService(this.platform.Service.StatefulProgrammableSwitch)), ProgrammableSwitchServiceOn;
71
+
72
+
73
+ this.ProgrammableSwitchServiceOn.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} On`);
74
+ if (!this.ProgrammableSwitchServiceOn.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
75
+ this.ProgrammableSwitchServiceOn.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} On`);
76
+ }
77
+
78
+ this.ProgrammableSwitchServiceOn.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent).setProps({
79
+ validValueRanges: [0, 0],
80
+ minValue: 0,
81
+ maxValue: 0,
82
+ validValues: [0],
83
+ })
84
+ .onGet(() => {
85
+ return this.ProgrammableSwitchEventOn!;
86
+ });
87
+
88
+ this.ProgrammableSwitchServiceOn.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState)
89
+ .onSet(this.ProgrammableSwitchOutputStateSetOn.bind(this));
90
+
91
+
92
+
93
+ // create a new Stateful Programmable Switch service
94
+ const ProgrammableSwitchServiceOff = `${accessory.displayName} ${device.remoteType} Off`;
95
+ (this.ProgrammableSwitchServiceOff = accessory.getService(this.platform.Service.StatefulProgrammableSwitch)
96
+ || accessory.addService(this.platform.Service.StatefulProgrammableSwitch)), ProgrammableSwitchServiceOff;
97
+
98
+
99
+ this.ProgrammableSwitchServiceOff.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Off`);
100
+ if (!this.ProgrammableSwitchServiceOff.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
101
+ this.ProgrammableSwitchServiceOff.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Off`);
102
+ }
103
+
104
+ this.ProgrammableSwitchServiceOff.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent).setProps({
105
+ validValueRanges: [0, 0],
106
+ minValue: 0,
107
+ maxValue: 0,
108
+ validValues: [0],
109
+ })
110
+ .onGet(() => {
111
+ return this.ProgrammableSwitchEventOff!;
112
+ });
113
+
114
+ this.ProgrammableSwitchServiceOff.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState)
115
+ .onSet(this.ProgrammableSwitchOutputStateSetOff.bind(this));
53
116
  }
54
117
 
55
- // handle on / off events using the On characteristic
56
- this.lightBulbService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
57
118
  }
58
119
 
59
120
  async OnSet(value: CharacteristicValue): Promise<void> {
@@ -71,6 +132,36 @@ export class Light {
71
132
  */
72
133
  }
73
134
 
135
+ async ProgrammableSwitchOutputStateSetOn(value: CharacteristicValue): Promise<void> {
136
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} On: ${value}`);
137
+
138
+ this.ProgrammableSwitchOutputStateOn = value;
139
+ if (this.ProgrammableSwitchOutputStateOn === 1) {
140
+ this.On = true;
141
+ await this.pushLightOnChanges();
142
+ }
143
+ /**
144
+ * pushLightOnChanges and pushLightOffChanges above assume they are measuring the state of the accessory BEFORE
145
+ * they are updated, so we are only updating the accessory state after calling the above.
146
+ */
147
+ }
148
+
149
+ async ProgrammableSwitchOutputStateSetOff(value: CharacteristicValue): Promise<void> {
150
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} On: ${value}`);
151
+
152
+ this.ProgrammableSwitchOutputStateOff = value;
153
+ if (this.ProgrammableSwitchOutputStateOff === 1) {
154
+ this.On = false;
155
+ await this.pushLightOffChanges();
156
+ }
157
+ /**
158
+ * pushLightOnChanges and pushLightOffChanges above assume they are measuring the state of the accessory BEFORE
159
+ * they are updated, so we are only updating the accessory state after calling the above.
160
+ */
161
+ }
162
+
163
+
164
+
74
165
  /**
75
166
  * Pushes the requested changes to the SwitchBot API
76
167
  * deviceType commandType Command command parameter Description
@@ -113,7 +204,7 @@ export class Light {
113
204
  }
114
205
  }
115
206
 
116
- async pushLightBrightnessUpChanges(): Promise<void> {
207
+ /*async pushLightBrightnessUpChanges(): Promise<void> {
117
208
  const bodyChange = JSON.stringify({
118
209
  command: 'brightnessUp',
119
210
  parameter: 'default',
@@ -129,7 +220,7 @@ export class Light {
129
220
  commandType: 'command',
130
221
  });
131
222
  await this.pushChanges(bodyChange);
132
- }
223
+ }*/
133
224
 
134
225
  async pushChanges(bodyChange: any): Promise<void> {
135
226
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} pushChanges`);
@@ -173,13 +264,38 @@ export class Light {
173
264
  }
174
265
 
175
266
  async updateHomeKitCharacteristics(): Promise<void> {
176
- // On
177
- if (this.On === undefined) {
178
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} On: ${this.On}`);
267
+ if (this.device.irlight?.stateless) {
268
+ // On
269
+ if (this.On === undefined) {
270
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} On: ${this.On}`);
271
+ } else {
272
+ this.accessory.context.On = this.On;
273
+ this.lightBulbService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
274
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
275
+ }
179
276
  } else {
180
- this.accessory.context.On = this.On;
181
- this.lightBulbService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
182
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
277
+ // On Stateful Programmable Switch
278
+ if (this.ProgrammableSwitchOutputStateOn === undefined) {
279
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName}`
280
+ + ` ProgrammableSwitchOutputStateOn: ${this.ProgrammableSwitchOutputStateOn}`);
281
+ } else {
282
+ this.accessory.context.ProgrammableSwitchOutputStateOn = this.ProgrammableSwitchOutputStateOn;
283
+ this.ProgrammableSwitchServiceOn?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState,
284
+ this.ProgrammableSwitchOutputStateOn);
285
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic`
286
+ + ` ProgrammableSwitchOutputStateOn: ${this.ProgrammableSwitchOutputStateOn}`);
287
+ }
288
+ // Off Stateful Programmable Switch
289
+ if (this.ProgrammableSwitchOutputStateOff === undefined) {
290
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName}`
291
+ + ` ProgrammableSwitchOutputStateOff: ${this.ProgrammableSwitchOutputStateOff}`);
292
+ } else {
293
+ this.accessory.context.ProgrammableSwitchOutputStateOff = this.ProgrammableSwitchOutputStateOff;
294
+ this.ProgrammableSwitchServiceOff?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState,
295
+ this.ProgrammableSwitchOutputStateOff);
296
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic`
297
+ + ` ProgrammableSwitchOutputStateOff: ${this.ProgrammableSwitchOutputStateOff}`);
298
+ }
183
299
  }
184
300
  }
185
301
 
@@ -270,7 +386,14 @@ export class Light {
270
386
  }
271
387
 
272
388
  async apiError(e: any): Promise<void> {
273
- this.lightBulbService.updateCharacteristic(this.platform.Characteristic.On, e);
389
+ if (this.device.irlight?.stateless) {
390
+ this.lightBulbService?.updateCharacteristic(this.platform.Characteristic.On, e);
391
+ } else {
392
+ this.ProgrammableSwitchServiceOn?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent, e);
393
+ this.ProgrammableSwitchServiceOn?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState, e);
394
+ this.ProgrammableSwitchServiceOff?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent, e);
395
+ this.ProgrammableSwitchServiceOff?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState, e);
396
+ }
274
397
  }
275
398
 
276
399
  async context() {
package/src/settings.ts CHANGED
@@ -179,7 +179,7 @@ export interface irDevicesConfig extends irdevice {
179
179
  irair?: irair;
180
180
  irpur?: Record<any, any>;
181
181
  ircam?: Record<any, any>;
182
- irlight?: Record<any, any>;
182
+ irlight?: irlight;
183
183
  irvc?: Record<any, any>;
184
184
  irwh?: Record<any, any>;
185
185
  irtv?: Record<any, any>;
@@ -194,6 +194,10 @@ export type irfan = {
194
194
  set_min?: number; //set_min
195
195
  };
196
196
 
197
+ export type irlight = {
198
+ stateless?: boolean;
199
+ };
200
+
197
201
  export type irair = {
198
202
  hide_automode?: boolean;
199
203
  meterType?: string;