@switchbot/homebridge-switchbot 2.9.0-beta.9 → 2.9.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.
Files changed (133) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/config.schema.json +11 -5
  3. package/dist/device/blindtilt.d.ts +1 -1
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +38 -17
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +1 -1
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +64 -17
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +1 -1
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +34 -17
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +1 -1
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +31 -17
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +1 -1
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +41 -17
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +1 -1
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +38 -17
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/hub.d.ts +8 -9
  28. package/dist/device/hub.d.ts.map +1 -1
  29. package/dist/device/hub.js +172 -55
  30. package/dist/device/hub.js.map +1 -1
  31. package/dist/device/humidifier.d.ts +1 -1
  32. package/dist/device/humidifier.d.ts.map +1 -1
  33. package/dist/device/humidifier.js +37 -17
  34. package/dist/device/humidifier.js.map +1 -1
  35. package/dist/device/iosensor.d.ts +1 -1
  36. package/dist/device/iosensor.d.ts.map +1 -1
  37. package/dist/device/iosensor.js +41 -18
  38. package/dist/device/iosensor.js.map +1 -1
  39. package/dist/device/lightstrip.d.ts +1 -1
  40. package/dist/device/lightstrip.d.ts.map +1 -1
  41. package/dist/device/lightstrip.js +34 -17
  42. package/dist/device/lightstrip.js.map +1 -1
  43. package/dist/device/lock.d.ts +1 -1
  44. package/dist/device/lock.d.ts.map +1 -1
  45. package/dist/device/lock.js +38 -17
  46. package/dist/device/lock.js.map +1 -1
  47. package/dist/device/meter.d.ts +1 -9
  48. package/dist/device/meter.d.ts.map +1 -1
  49. package/dist/device/meter.js +47 -32
  50. package/dist/device/meter.js.map +1 -1
  51. package/dist/device/meterplus.d.ts +4 -7
  52. package/dist/device/meterplus.d.ts.map +1 -1
  53. package/dist/device/meterplus.js +69 -50
  54. package/dist/device/meterplus.js.map +1 -1
  55. package/dist/device/motion.d.ts +1 -1
  56. package/dist/device/motion.d.ts.map +1 -1
  57. package/dist/device/motion.js +38 -17
  58. package/dist/device/motion.js.map +1 -1
  59. package/dist/device/plug.d.ts +1 -1
  60. package/dist/device/plug.d.ts.map +1 -1
  61. package/dist/device/plug.js +35 -17
  62. package/dist/device/plug.js.map +1 -1
  63. package/dist/device/robotvacuumcleaner.d.ts +1 -1
  64. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  65. package/dist/device/robotvacuumcleaner.js +35 -17
  66. package/dist/device/robotvacuumcleaner.js.map +1 -1
  67. package/dist/irdevice/airconditioner.d.ts +1 -1
  68. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  69. package/dist/irdevice/airconditioner.js +28 -14
  70. package/dist/irdevice/airconditioner.js.map +1 -1
  71. package/dist/irdevice/airpurifier.d.ts +1 -1
  72. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  73. package/dist/irdevice/airpurifier.js +28 -14
  74. package/dist/irdevice/airpurifier.js.map +1 -1
  75. package/dist/irdevice/camera.d.ts +1 -1
  76. package/dist/irdevice/camera.d.ts.map +1 -1
  77. package/dist/irdevice/camera.js +28 -14
  78. package/dist/irdevice/camera.js.map +1 -1
  79. package/dist/irdevice/fan.d.ts +1 -1
  80. package/dist/irdevice/fan.d.ts.map +1 -1
  81. package/dist/irdevice/fan.js +28 -14
  82. package/dist/irdevice/fan.js.map +1 -1
  83. package/dist/irdevice/light.d.ts +1 -1
  84. package/dist/irdevice/light.d.ts.map +1 -1
  85. package/dist/irdevice/light.js +28 -14
  86. package/dist/irdevice/light.js.map +1 -1
  87. package/dist/irdevice/other.d.ts +1 -1
  88. package/dist/irdevice/other.d.ts.map +1 -1
  89. package/dist/irdevice/other.js +35 -14
  90. package/dist/irdevice/other.js.map +1 -1
  91. package/dist/irdevice/tv.d.ts +1 -1
  92. package/dist/irdevice/tv.d.ts.map +1 -1
  93. package/dist/irdevice/tv.js +28 -14
  94. package/dist/irdevice/tv.js.map +1 -1
  95. package/dist/irdevice/vacuumcleaner.d.ts +1 -1
  96. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  97. package/dist/irdevice/vacuumcleaner.js +28 -14
  98. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  99. package/dist/irdevice/waterheater.d.ts +1 -1
  100. package/dist/irdevice/waterheater.d.ts.map +1 -1
  101. package/dist/irdevice/waterheater.js +28 -14
  102. package/dist/irdevice/waterheater.js.map +1 -1
  103. package/dist/platform.d.ts.map +1 -1
  104. package/dist/platform.js +4 -0
  105. package/dist/platform.js.map +1 -1
  106. package/package.json +11 -11
  107. package/src/device/blindtilt.ts +38 -18
  108. package/src/device/bot.ts +55 -18
  109. package/src/device/ceilinglight.ts +34 -19
  110. package/src/device/colorbulb.ts +31 -18
  111. package/src/device/contact.ts +41 -19
  112. package/src/device/curtain.ts +38 -18
  113. package/src/device/hub.ts +188 -65
  114. package/src/device/humidifier.ts +37 -19
  115. package/src/device/iosensor.ts +41 -20
  116. package/src/device/lightstrip.ts +34 -18
  117. package/src/device/lock.ts +39 -20
  118. package/src/device/meter.ts +48 -40
  119. package/src/device/meterplus.ts +74 -60
  120. package/src/device/motion.ts +38 -20
  121. package/src/device/plug.ts +35 -19
  122. package/src/device/robotvacuumcleaner.ts +35 -18
  123. package/src/irdevice/airconditioner.ts +28 -15
  124. package/src/irdevice/airpurifier.ts +30 -15
  125. package/src/irdevice/camera.ts +30 -15
  126. package/src/irdevice/fan.ts +30 -15
  127. package/src/irdevice/light.ts +30 -15
  128. package/src/irdevice/other.ts +37 -15
  129. package/src/irdevice/tv.ts +30 -15
  130. package/src/irdevice/vacuumcleaner.ts +30 -15
  131. package/src/irdevice/waterheater.ts +30 -15
  132. package/src/platform.ts +4 -0
  133. package/.github/npm-version-script.js +0 -81
package/src/device/hub.ts CHANGED
@@ -1,10 +1,12 @@
1
+ import { connectAsync } from 'async-mqtt';
1
2
  import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
3
+ import { MqttClient } from 'mqtt';
4
+ import { hostname } from 'os';
2
5
  import { interval } from 'rxjs';
3
6
  import { request } from 'undici';
4
7
  import { Context } from 'vm';
5
8
  import { SwitchBotPlatform } from '../platform';
6
9
  import { Devices, device, deviceStatus, devicesConfig } from '../settings';
7
- import { sleep } from '../utils';
8
10
 
9
11
  export class Hub {
10
12
  // Services
@@ -27,6 +29,12 @@ export class Hub {
27
29
  // OpenAPI Others
28
30
  spaceBetweenLevels!: number;
29
31
 
32
+ //MQTT stuff
33
+ mqttClient: MqttClient | null = null;
34
+
35
+ // EVE history service handler
36
+ historyService?: any;
37
+
30
38
  // Config
31
39
  set_minStep!: number;
32
40
  updateRate!: number;
@@ -48,6 +56,9 @@ export class Hub {
48
56
  // default placeholders
49
57
  this.logs(device);
50
58
  this.refreshRate(device);
59
+ this.context(accessory, device);
60
+ this.setupHistoryService(device);
61
+ this.setupMqtt(device);
51
62
  this.config(device);
52
63
 
53
64
  this.CurrentRelativeHumidity = accessory.context.CurrentRelativeHumidity;
@@ -61,10 +72,20 @@ export class Hub {
61
72
  .getService(this.platform.Service.AccessoryInformation)!
62
73
  .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
63
74
  .setCharacteristic(this.platform.Characteristic.Model, accessory.context.model)
64
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
65
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.setFirmwareRevision(accessory, device))
66
- .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
67
- .updateValue(this.setFirmwareRevision(accessory, device));
75
+ .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!);
76
+
77
+ if (accessory.context.FirmwareRevision) {
78
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName}`
79
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
80
+ accessory
81
+ .getService(this.platform.Service.AccessoryInformation)!
82
+ .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
83
+ .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
84
+ .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
85
+ .updateValue(accessory.context.FirmwareRevision);
86
+ } else {
87
+ this.setFirmwareRevision(accessory, device);
88
+ }
68
89
 
69
90
  // Temperature Sensor Service
70
91
  if (device.hub?.hide_temperature) {
@@ -124,7 +145,7 @@ export class Hub {
124
145
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Humidity Sensor Service Not Added`);
125
146
  }
126
147
 
127
- // Humidity Sensor Service
148
+ // Light Sensor Service
128
149
  if (device.hub?.hide_lightsensor) {
129
150
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
130
151
  this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor);
@@ -141,13 +162,14 @@ export class Hub {
141
162
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
142
163
  }
143
164
 
144
- // Update Homekit
165
+ // Retrieve initial values and update Homekit
145
166
  this.updateHomeKitCharacteristics();
146
167
 
147
168
  // Start an update interval
148
- interval(this.deviceRefreshRate * 1000).subscribe(async () => {
149
- await this.refreshStatus();
150
- });
169
+ interval(this.deviceRefreshRate * 1000)
170
+ .subscribe(async () => {
171
+ await this.refreshStatus();
172
+ });
151
173
  }
152
174
 
153
175
  /**
@@ -166,14 +188,16 @@ export class Hub {
166
188
 
167
189
  async openAPIparseStatus(): Promise<void> {
168
190
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
169
- // Temperature
170
- if (!this.device.hub?.hide_temperature) {
171
- this.CurrentTemperature = Number(this.OpenAPI_CurrentTemperature);
172
- }
173
-
174
- // Humidity
191
+ // CurrentRelativeHumidity
175
192
  if (!this.device.hub?.hide_humidity) {
176
193
  this.CurrentRelativeHumidity = Number(this.OpenAPI_CurrentRelativeHumidity);
194
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
195
+ }
196
+
197
+ // CurrentTemperature
198
+ if (!this.device.hub?.hide_temperature) {
199
+ this.CurrentTemperature = Number(this.OpenAPI_CurrentTemperature);
200
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
177
201
  }
178
202
 
179
203
  // Brightness
@@ -191,7 +215,7 @@ export class Hub {
191
215
  this.CurrentAmbientLightLevel = (this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels;
192
216
  this.debugLog(
193
217
  `${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.OpenAPI_CurrentAmbientLightLevel},` +
194
- ` Calculation: ${(this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels}`,
218
+ ` Calculation: ${(this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels}`,
195
219
  );
196
220
  break;
197
221
  case 3:
@@ -269,7 +293,7 @@ export class Hub {
269
293
  }
270
294
  this.debugLog(
271
295
  `${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.OpenAPI_CurrentAmbientLightLevel},` +
272
- ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
296
+ ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
273
297
  );
274
298
  }
275
299
  if (!this.device.hub?.hide_lightsensor) {
@@ -325,60 +349,65 @@ export class Hub {
325
349
  }
326
350
  }
327
351
 
328
- async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
329
- return fn().catch(async (e: any) => {
330
- if (max === 0) {
331
- throw e;
332
- }
333
- this.infoLog(e);
334
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
335
- await sleep(1000);
336
- return this.retry({ max: max - 1, fn });
337
- });
338
- }
339
-
340
- maxRetry(): number {
341
- if (this.device.maxRetry) {
342
- return this.device.maxRetry;
343
- } else {
344
- return 5;
345
- }
346
- }
347
-
348
352
  /**
349
353
  * Handle requests to set the value of the "Target Position" characteristic
350
354
  */
351
355
 
352
356
  async updateHomeKitCharacteristics(): Promise<void> {
353
- if (!this.device.hub?.hide_temperature) {
354
- if (this.CurrentTemperature === undefined) {
355
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
356
- } else {
357
- this.accessory.context.CurrentTemperature = this.CurrentTemperature;
358
- this.temperatureService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
359
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
360
- }
361
- }
357
+ const mqttmessage: string[] = [];
358
+ const entry = { time: Math.round(new Date().valueOf() / 1000) };
359
+
360
+ // CurrentRelativeHumidity
362
361
  if (!this.device.hub?.hide_humidity) {
363
362
  if (this.CurrentRelativeHumidity === undefined) {
364
363
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
365
364
  } else {
365
+ if (this.device.mqttURL) {
366
+ mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
367
+ }
368
+ if (this.device.history) {
369
+ entry['humidity'] = this.CurrentRelativeHumidity;
370
+ }
366
371
  this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
367
372
  this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
368
373
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
369
- + `updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
370
- );
374
+ + `updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
371
375
  }
372
376
  }
377
+
378
+ // CurrentTemperature
379
+ if (!this.device.hub?.hide_temperature) {
380
+ if (this.CurrentTemperature === undefined) {
381
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
382
+ } else {
383
+ if (this.device.mqttURL) {
384
+ mqttmessage.push(`"temperature": ${this.CurrentTemperature}`);
385
+ }
386
+ if (this.device.history) {
387
+ entry['temp'] = this.CurrentTemperature;
388
+ }
389
+ this.accessory.context.CurrentTemperature = this.CurrentTemperature;
390
+ this.temperatureService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
391
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
392
+ }
393
+ }
394
+
395
+ // CurrentAmbientLightLevel
373
396
  if (!this.device.hub?.hide_lightsensor) {
374
397
  if (this.CurrentAmbientLightLevel === undefined) {
375
398
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
376
399
  } else {
400
+ if (this.device.mqttURL) {
401
+ mqttmessage.push(`"light": ${this.CurrentAmbientLightLevel}`);
402
+ }
403
+ if (this.device.history) {
404
+ entry['lux'] = this.CurrentAmbientLightLevel;
405
+ }
377
406
  this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
378
407
  this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
379
408
  this.debugLog(
380
409
  `${this.device.deviceType}: ${this.accessory.displayName} `
381
- + `updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
410
+ + `updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
382
411
  );
383
412
  }
384
413
  }
@@ -388,11 +417,79 @@ export class Hub {
388
417
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} FirmwareRevision: ${this.FirmwareRevision}`);
389
418
  } else {
390
419
  this.accessory.context.FirmwareRevision = this.FirmwareRevision;
391
- this.accessory.getService(this.platform.Service.AccessoryInformation)!
392
- .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
420
+
421
+ if (!this.device.hub?.hide_humidity) {
422
+ this.humidityService!.updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
423
+ }
424
+ if (!this.device.hub?.hide_temperature) {
425
+ this.temperatureService!.updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
426
+ }
427
+ if (!this.device.hub?.hide_lightsensor) {
428
+ this.lightSensorService!.updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
429
+ }
393
430
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
394
431
  + `updateCharacteristic FirmwareRevision: ${this.FirmwareRevision}`);
395
432
  }
433
+
434
+ // MQTT
435
+ if (this.device.mqttURL) {
436
+ this.mqttPublish(`{${mqttmessage.join(',')}}`);
437
+ }
438
+ if (Number(this.CurrentRelativeHumidity) > 0) {
439
+ // reject unreliable data
440
+ if (this.device.history) {
441
+ this.historyService?.addEntry(entry);
442
+ }
443
+ }
444
+ }
445
+
446
+ /*
447
+ * Publish MQTT message for topics of
448
+ * 'homebridge-switchbot/meter/xx:xx:xx:xx:xx:xx'
449
+ */
450
+ mqttPublish(message: any) {
451
+ const mac = this.device.deviceId
452
+ ?.toLowerCase()
453
+ .match(/[\s\S]{1,2}/g)
454
+ ?.join(':');
455
+ const options = this.device.mqttPubOptions || {};
456
+ this.mqttClient?.publish(`homebridge-switchbot/meter/${mac}`, `${message}`, options);
457
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${message} options:${JSON.stringify(options)}`);
458
+ }
459
+
460
+ /*
461
+ * Setup MQTT hadler if URL is specifed.
462
+ */
463
+ async setupMqtt(device: device & devicesConfig): Promise<void> {
464
+ if (device.mqttURL) {
465
+ try {
466
+ this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
467
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
468
+ this.mqttClient.on('error', (e: Error) => {
469
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
470
+ });
471
+ } catch (e) {
472
+ this.mqttClient = null;
473
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
474
+ }
475
+ }
476
+ }
477
+
478
+ /*
479
+ * Setup EVE history graph feature if enabled.
480
+ */
481
+ async setupHistoryService(device: device & devicesConfig): Promise<void> {
482
+ const mac = this.device
483
+ .deviceId!.match(/.{1,2}/g)!
484
+ .join(':')
485
+ .toLowerCase();
486
+ this.historyService = device.history
487
+ ? new this.platform.fakegatoAPI('custom', this.accessory, {
488
+ log: this.platform.log,
489
+ storage: 'fs',
490
+ filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
491
+ })
492
+ : null;
396
493
  }
397
494
 
398
495
  async statusCode(statusCode: number): Promise<void> {
@@ -439,6 +536,7 @@ export class Hub {
439
536
 
440
537
  async offlineOff(): Promise<void> {
441
538
  if (this.device.offline) {
539
+ await this.context(this.accessory, this.device);
442
540
  await this.updateHomeKitCharacteristics();
443
541
  }
444
542
  }
@@ -473,24 +571,49 @@ export class Hub {
473
571
  return this.set_maxLux;
474
572
  }
475
573
 
476
- setFirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
477
-
478
- this.debugLog(
479
- `${this.device.deviceType}: ${this.accessory.displayName}` + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`,
480
- );
481
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
482
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
574
+ async setFirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig) {
575
+ await this.refreshStatus();
483
576
  if (device.firmware) {
484
- this.FirmwareRevision = device.firmware;
485
- } else if (device.version) {
486
- this.FirmwareRevision = device.version;
487
- } else if (accessory.context.FirmwareRevision) {
577
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
578
+ accessory.context.FirmwareRevision = device.firmware;
488
579
  this.FirmwareRevision = accessory.context.FirmwareRevision;
580
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware, FirmwareRevision: ${this.FirmwareRevision}`);
489
581
  } else {
582
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version: ${device.version}`);
583
+ accessory.context.FirmwareRevision = device.version;
584
+ this.FirmwareRevision = accessory.context.FirmwareRevision;
585
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version, FirmwareRevision: ${this.FirmwareRevision}`);
586
+ }
587
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
588
+ accessory
589
+ .getService(this.platform.Service.AccessoryInformation)!
590
+ .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
591
+ .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
592
+ .updateValue(this.FirmwareRevision);
593
+ }
594
+
595
+ async context(accessory, device) {
596
+ if (this.CurrentRelativeHumidity === undefined) {
597
+ this.CurrentRelativeHumidity = 0;
598
+ } else {
599
+ this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
600
+ }
601
+ if (this.CurrentTemperature === undefined) {
602
+ this.CurrentTemperature = 0;
603
+ } else {
604
+ this.CurrentTemperature = this.accessory.context.CurrentTemperature;
605
+ }
606
+ if (this.CurrentAmbientLightLevel === undefined) {
607
+ this.CurrentAmbientLightLevel = this.set_minLux;
608
+ } else {
609
+ this.CurrentAmbientLightLevel = this.accessory.context.CurrentAmbientLightLevel;
610
+ }
611
+ if (this.FirmwareRevision === undefined) {
490
612
  this.FirmwareRevision = this.platform.version;
613
+ } else {
614
+ this.setFirmwareRevision(accessory, device);
615
+ this.FirmwareRevision = this.accessory.context.FirmwareRevision;
491
616
  }
492
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
493
- return this.FirmwareRevision;
494
617
  }
495
618
 
496
619
  async refreshRate(device: device & devicesConfig): Promise<void> {
@@ -81,10 +81,20 @@ export class Humidifier {
81
81
  .getService(this.platform.Service.AccessoryInformation)!
82
82
  .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
83
83
  .setCharacteristic(this.platform.Characteristic.Model, 'W0801800')
84
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!)
85
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.setFirmwareRevision(accessory, device))
86
- .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
87
- .updateValue(this.setFirmwareRevision(accessory, device));
84
+ .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!);
85
+
86
+ if (accessory.context.FirmwareRevision) {
87
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName}`
88
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
89
+ accessory
90
+ .getService(this.platform.Service.AccessoryInformation)!
91
+ .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
92
+ .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
93
+ .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
94
+ .updateValue(accessory.context.FirmwareRevision);
95
+ } else {
96
+ this.setFirmwareRevision(accessory, device);
97
+ }
88
98
 
89
99
  // get the service if it exists, otherwise create a new service
90
100
  // you can create multiple services for each accessory
@@ -743,8 +753,10 @@ export class Humidifier {
743
753
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} FirmwareRevision: ${this.FirmwareRevision}`);
744
754
  } else {
745
755
  this.accessory.context.FirmwareRevision = this.FirmwareRevision;
746
- this.accessory.getService(this.platform.Service.AccessoryInformation)!
747
- .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
756
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
757
+ if (!this.device.humidifier?.hide_temperature) {
758
+ this.temperatureservice!.updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
759
+ }
748
760
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
749
761
  + `updateCharacteristic FirmwareRevision: ${this.FirmwareRevision}`);
750
762
  }
@@ -883,24 +895,30 @@ export class Humidifier {
883
895
  }
884
896
  }
885
897
 
886
- setFirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
887
-
888
- this.debugLog(
889
- `${this.device.deviceType}: ${this.accessory.displayName}` + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`,
890
- );
891
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
892
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
898
+ async setFirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig) {
899
+ await this.refreshStatus();
893
900
  if (device.firmware) {
894
- this.FirmwareRevision = device.firmware;
901
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
902
+ accessory.context.FirmwareRevision = device.firmware;
903
+ this.FirmwareRevision = accessory.context.FirmwareRevision;
904
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware, FirmwareRevision: ${this.FirmwareRevision}`);
895
905
  } else if (device.version) {
896
- this.FirmwareRevision = device.version;
897
- } else if (accessory.context.FirmwareRevision) {
906
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version: ${device.version}`);
907
+ accessory.context.FirmwareRevision = device.version;
898
908
  this.FirmwareRevision = accessory.context.FirmwareRevision;
909
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version, FirmwareRevision: ${this.FirmwareRevision}`);
899
910
  } else {
900
- this.FirmwareRevision = this.platform.version;
911
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
912
+ accessory.context.FirmwareRevision = this.platform.version;
913
+ this.FirmwareRevision = accessory.context.FirmwareRevision;
914
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version, FirmwareRevision: ${this.FirmwareRevision}`);
901
915
  }
902
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
903
- return this.FirmwareRevision;
916
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
917
+ accessory
918
+ .getService(this.platform.Service.AccessoryInformation)!
919
+ .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
920
+ .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
921
+ .updateValue(this.FirmwareRevision);
904
922
  }
905
923
 
906
924
  async context() {
@@ -89,10 +89,20 @@ export class IOSensor {
89
89
  .getService(this.platform.Service.AccessoryInformation)!
90
90
  .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
91
91
  .setCharacteristic(this.platform.Characteristic.Model, 'WoIOSensor')
92
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!)
93
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.setFirmwareRevision(accessory, device))
94
- .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
95
- .updateValue(this.setFirmwareRevision(accessory, device));
92
+ .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!);
93
+
94
+ if (accessory.context.FirmwareRevision) {
95
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName}`
96
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
97
+ accessory
98
+ .getService(this.platform.Service.AccessoryInformation)!
99
+ .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
100
+ .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
101
+ .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
102
+ .updateValue(accessory.context.FirmwareRevision);
103
+ } else {
104
+ this.setFirmwareRevision(accessory, device);
105
+ }
96
106
 
97
107
  // Temperature Sensor Service
98
108
  if (device.meter?.hide_temperature) {
@@ -290,7 +300,7 @@ export class IOSensor {
290
300
  if (switchbot !== false) {
291
301
  switchbot
292
302
  .startScan({
293
- model: 'i',
303
+ model: 'w',
294
304
  id: this.device.bleMac,
295
305
  })
296
306
  .then(async () => {
@@ -453,8 +463,13 @@ export class IOSensor {
453
463
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} FirmwareRevision: ${this.FirmwareRevision}`);
454
464
  } else {
455
465
  this.accessory.context.FirmwareRevision = this.FirmwareRevision;
456
- this.accessory.getService(this.platform.Service.AccessoryInformation)!
457
- .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
466
+ this.batteryService.updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
467
+ if (this.device.meter?.hide_humidity) {
468
+ this.humidityservice!.updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
469
+ }
470
+ if (this.device.meter?.hide_temperature) {
471
+ this.temperatureservice!.updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
472
+ }
458
473
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
459
474
  + `updateCharacteristic FirmwareRevision: ${this.FirmwareRevision}`);
460
475
  }
@@ -619,24 +634,30 @@ export class IOSensor {
619
634
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
620
635
  }
621
636
 
622
- setFirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
623
-
624
- this.debugLog(
625
- `${this.device.deviceType}: ${this.accessory.displayName}` + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`,
626
- );
627
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
628
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
637
+ async setFirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig) {
638
+ await this.refreshStatus();
629
639
  if (device.firmware) {
630
- this.FirmwareRevision = device.firmware;
640
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
641
+ accessory.context.FirmwareRevision = device.firmware;
642
+ this.FirmwareRevision = accessory.context.FirmwareRevision;
643
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware, FirmwareRevision: ${this.FirmwareRevision}`);
631
644
  } else if (device.version) {
632
- this.FirmwareRevision = device.version;
633
- } else if (accessory.context.FirmwareRevision) {
645
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version: ${device.version}`);
646
+ accessory.context.FirmwareRevision = device.version;
634
647
  this.FirmwareRevision = accessory.context.FirmwareRevision;
648
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version, FirmwareRevision: ${this.FirmwareRevision}`);
635
649
  } else {
636
- this.FirmwareRevision = this.platform.version;
650
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
651
+ accessory.context.FirmwareRevision = this.platform.version;
652
+ this.FirmwareRevision = accessory.context.FirmwareRevision;
653
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version, FirmwareRevision: ${this.FirmwareRevision}`);
637
654
  }
638
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
639
- return this.FirmwareRevision;
655
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
656
+ accessory
657
+ .getService(this.platform.Service.AccessoryInformation)!
658
+ .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
659
+ .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
660
+ .updateValue(this.FirmwareRevision);
640
661
  }
641
662
 
642
663
  async context() {
@@ -86,10 +86,20 @@ export class StripLight {
86
86
  .getService(this.platform.Service.AccessoryInformation)!
87
87
  .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
88
88
  .setCharacteristic(this.platform.Characteristic.Model, 'W1701100')
89
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!)
90
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.setFirmwareRevision(accessory, device))
91
- .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
92
- .updateValue(this.setFirmwareRevision(accessory, device));
89
+ .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!);
90
+
91
+ if (accessory.context.FirmwareRevision) {
92
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName}`
93
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
94
+ accessory
95
+ .getService(this.platform.Service.AccessoryInformation)!
96
+ .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
97
+ .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
98
+ .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
99
+ .updateValue(accessory.context.FirmwareRevision);
100
+ } else {
101
+ this.setFirmwareRevision(accessory, device);
102
+ }
93
103
 
94
104
  // get the Lightbulb service if it exists, otherwise create a new Lightbulb service
95
105
  // you can create multiple services for each accessory
@@ -865,8 +875,7 @@ export class StripLight {
865
875
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} FirmwareRevision: ${this.FirmwareRevision}`);
866
876
  } else {
867
877
  this.accessory.context.FirmwareRevision = this.FirmwareRevision;
868
- this.accessory.getService(this.platform.Service.AccessoryInformation)!
869
- .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
878
+ this.lightBulbService.updateCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision);
870
879
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
871
880
  + `updateCharacteristic FirmwareRevision: ${this.FirmwareRevision}`);
872
881
  }
@@ -1025,23 +1034,30 @@ export class StripLight {
1025
1034
  this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, e);
1026
1035
  }
1027
1036
 
1028
- setFirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
1029
- this.debugLog(
1030
- `${this.device.deviceType}: ${this.accessory.displayName} ` + `accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`,
1031
- );
1032
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
1033
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
1037
+ async setFirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig) {
1038
+ await this.refreshStatus();
1034
1039
  if (device.firmware) {
1035
- this.FirmwareRevision = device.firmware;
1040
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
1041
+ accessory.context.FirmwareRevision = device.firmware;
1042
+ this.FirmwareRevision = accessory.context.FirmwareRevision;
1043
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware, FirmwareRevision: ${this.FirmwareRevision}`);
1036
1044
  } else if (device.version) {
1037
- this.FirmwareRevision = device.version;
1038
- } else if (accessory.context.FirmwareRevision) {
1045
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version: ${device.version}`);
1046
+ accessory.context.FirmwareRevision = device.version;
1039
1047
  this.FirmwareRevision = accessory.context.FirmwareRevision;
1048
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version, FirmwareRevision: ${this.FirmwareRevision}`);
1040
1049
  } else {
1041
- this.FirmwareRevision = this.platform.version;
1050
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
1051
+ accessory.context.FirmwareRevision = this.platform.version;
1052
+ this.FirmwareRevision = accessory.context.FirmwareRevision;
1053
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version, FirmwareRevision: ${this.FirmwareRevision}`);
1042
1054
  }
1043
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
1044
- return this.FirmwareRevision;
1055
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
1056
+ accessory
1057
+ .getService(this.platform.Service.AccessoryInformation)!
1058
+ .updateCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
1059
+ .getCharacteristic(this.platform.Characteristic.FirmwareRevision)
1060
+ .updateValue(this.FirmwareRevision);
1045
1061
  }
1046
1062
 
1047
1063
  async context() {