@switchbot/homebridge-switchbot 2.11.1-beta.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;
@@ -1,81 +0,0 @@
1
- #!/bin/env node
2
-
3
- /**
4
- * This scripts queries the npm registry to pull out the latest version for a given tag.
5
- */
6
-
7
- const fs = require("fs");
8
- const semver = require("semver");
9
- const child_process = require("child_process");
10
- const assert = require("assert");
11
-
12
- const BRANCH_VERSION_PATTERN = /^([A-Za-z]*)-(\d+.\d+.\d+)$/
13
-
14
- // Load the contents of the package.json file
15
- const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
16
-
17
- let refArgument = process.argv[2];
18
- let tagArgument = process.argv[3] || "latest";
19
-
20
- if (refArgument == null) {
21
- console.error("ref argument is missing");
22
- console.error("Usage: npm-version-script.js <ref> [tag]");
23
- process.exit(1);
24
- }
25
-
26
- /**
27
- * Queries the NPM registry for the latest version for the provided tag.
28
- * @param tag The tag to query for.
29
- * @returns {string} Returns the version.
30
- */
31
- function getTagVersionFromNpm(tag) {
32
- try {
33
- return child_process.execSync(`npm info ${packageJSON.name} version --tag="${tag}"`).toString("utf8").trim();
34
- } catch (e) {
35
- throw e;
36
- }
37
- }
38
-
39
- function desiredTargetVersion(ref) {
40
- // ref is a GitHub action ref string
41
- if (ref.startsWith("refs/pull/")) {
42
- throw Error("The version script was executed inside a PR!");
43
- }
44
-
45
- assert(ref.startsWith("refs/heads/"))
46
- let branchName = ref.slice("refs/heads/".length);
47
-
48
- let results = branchName.match(BRANCH_VERSION_PATTERN);
49
- if (results != null) {
50
- if (results[1] !== tagArgument) {
51
- console.warn(`The base branch name (${results[1]}) differs from the tag name ${tagArgument}`);
52
- }
53
-
54
- return results[2];
55
- }
56
-
57
- // legacy mode were we use the `betaVersion` property in the package.json
58
- if (branchName === "beta" && packageJSON.betaVersion) {
59
- return packageJSON.betaVersion
60
- }
61
-
62
- throw new Error("Malformed branch name for ref: " + ref + ". Can't derive the base version. Use a branch name like: beta-x.x.x!");
63
- }
64
-
65
- // derive the base version from the branch ref
66
- const baseVersion = desiredTargetVersion(refArgument);
67
-
68
- // query the npm registry for the latest version of the provided tag name
69
- const latestReleasedVersion = getTagVersionFromNpm(tagArgument); // e.g. 0.7.0-beta.12
70
- const latestReleaseBase = semver.inc(latestReleasedVersion, "patch"); // will produce 0.7.0 (removing the preid, needed for the equality check below)
71
-
72
- let publishTag;
73
- if (semver.eq(baseVersion, latestReleaseBase)) { // check if we are releasing another version for the latest beta
74
- publishTag = latestReleasedVersion; // set the current latest beta to be incremented
75
- } else {
76
- publishTag = baseVersion; // start of with a new beta version
77
- }
78
-
79
- // save the package.json
80
- packageJSON.version = publishTag;
81
- fs.writeFileSync("package.json", JSON.stringify(packageJSON, null, 2));