@switchbot/homebridge-switchbot 2.0.0-beta.2 → 2.0.0-beta.20

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 (113) hide show
  1. package/CHANGELOG.md +8 -1
  2. package/README.md +1 -2
  3. package/config.schema.json +75 -20
  4. package/dist/device/bot.d.ts +32 -28
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +646 -648
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/colorbulb.d.ts +34 -19
  9. package/dist/device/colorbulb.d.ts.map +1 -1
  10. package/dist/device/colorbulb.js +429 -304
  11. package/dist/device/colorbulb.js.map +1 -1
  12. package/dist/device/contact.d.ts +16 -7
  13. package/dist/device/contact.d.ts.map +1 -1
  14. package/dist/device/contact.js +288 -230
  15. package/dist/device/contact.js.map +1 -1
  16. package/dist/device/curtain.d.ts +21 -16
  17. package/dist/device/curtain.d.ts.map +1 -1
  18. package/dist/device/curtain.js +519 -489
  19. package/dist/device/curtain.js.map +1 -1
  20. package/dist/device/humidifier.d.ts +22 -18
  21. package/dist/device/humidifier.d.ts.map +1 -1
  22. package/dist/device/humidifier.js +415 -360
  23. package/dist/device/humidifier.js.map +1 -1
  24. package/dist/device/lock.d.ts +22 -8
  25. package/dist/device/lock.d.ts.map +1 -1
  26. package/dist/device/lock.js +253 -147
  27. package/dist/device/lock.js.map +1 -1
  28. package/dist/device/meter.d.ts +21 -16
  29. package/dist/device/meter.d.ts.map +1 -1
  30. package/dist/device/meter.js +325 -255
  31. package/dist/device/meter.js.map +1 -1
  32. package/dist/device/meterplus.d.ts +23 -12
  33. package/dist/device/meterplus.d.ts.map +1 -1
  34. package/dist/device/meterplus.js +346 -208
  35. package/dist/device/meterplus.js.map +1 -1
  36. package/dist/device/motion.d.ts +17 -8
  37. package/dist/device/motion.d.ts.map +1 -1
  38. package/dist/device/motion.js +261 -209
  39. package/dist/device/motion.js.map +1 -1
  40. package/dist/device/plug.d.ts +24 -16
  41. package/dist/device/plug.d.ts.map +1 -1
  42. package/dist/device/plug.js +267 -242
  43. package/dist/device/plug.js.map +1 -1
  44. package/dist/device/striplight.d.ts +51 -16
  45. package/dist/device/striplight.d.ts.map +1 -1
  46. package/dist/device/striplight.js +527 -185
  47. package/dist/device/striplight.js.map +1 -1
  48. package/dist/irdevice/airconditioner.d.ts +7 -12
  49. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  50. package/dist/irdevice/airconditioner.js +261 -230
  51. package/dist/irdevice/airconditioner.js.map +1 -1
  52. package/dist/irdevice/airpurifier.d.ts +3 -3
  53. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  54. package/dist/irdevice/airpurifier.js +137 -104
  55. package/dist/irdevice/airpurifier.js.map +1 -1
  56. package/dist/irdevice/camera.d.ts +9 -8
  57. package/dist/irdevice/camera.d.ts.map +1 -1
  58. package/dist/irdevice/camera.js +116 -86
  59. package/dist/irdevice/camera.js.map +1 -1
  60. package/dist/irdevice/fan.d.ts +8 -7
  61. package/dist/irdevice/fan.d.ts.map +1 -1
  62. package/dist/irdevice/fan.js +143 -109
  63. package/dist/irdevice/fan.js.map +1 -1
  64. package/dist/irdevice/light.d.ts +9 -8
  65. package/dist/irdevice/light.d.ts.map +1 -1
  66. package/dist/irdevice/light.js +120 -103
  67. package/dist/irdevice/light.js.map +1 -1
  68. package/dist/irdevice/other.d.ts +9 -8
  69. package/dist/irdevice/other.d.ts.map +1 -1
  70. package/dist/irdevice/other.js +128 -98
  71. package/dist/irdevice/other.js.map +1 -1
  72. package/dist/irdevice/tv.d.ts +10 -9
  73. package/dist/irdevice/tv.d.ts.map +1 -1
  74. package/dist/irdevice/tv.js +126 -93
  75. package/dist/irdevice/tv.js.map +1 -1
  76. package/dist/irdevice/vacuumcleaner.d.ts +7 -10
  77. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  78. package/dist/irdevice/vacuumcleaner.js +115 -89
  79. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  80. package/dist/irdevice/waterheater.d.ts +6 -9
  81. package/dist/irdevice/waterheater.d.ts.map +1 -1
  82. package/dist/irdevice/waterheater.js +115 -89
  83. package/dist/irdevice/waterheater.js.map +1 -1
  84. package/dist/platform.d.ts +3 -5
  85. package/dist/platform.d.ts.map +1 -1
  86. package/dist/platform.js +203 -200
  87. package/dist/platform.js.map +1 -1
  88. package/dist/settings.d.ts +15 -11
  89. package/dist/settings.d.ts.map +1 -1
  90. package/dist/settings.js.map +1 -1
  91. package/package.json +12 -10
  92. package/src/device/bot.ts +641 -657
  93. package/src/device/colorbulb.ts +440 -339
  94. package/src/device/contact.ts +289 -233
  95. package/src/device/curtain.ts +523 -503
  96. package/src/device/humidifier.ts +421 -396
  97. package/src/device/lock.ts +257 -147
  98. package/src/device/meter.ts +335 -268
  99. package/src/device/meterplus.ts +354 -213
  100. package/src/device/motion.ts +264 -213
  101. package/src/device/plug.ts +270 -251
  102. package/src/device/striplight.ts +552 -207
  103. package/src/irdevice/airconditioner.ts +263 -241
  104. package/src/irdevice/airpurifier.ts +132 -103
  105. package/src/irdevice/camera.ts +112 -85
  106. package/src/irdevice/fan.ts +138 -106
  107. package/src/irdevice/light.ts +116 -103
  108. package/src/irdevice/other.ts +124 -97
  109. package/src/irdevice/tv.ts +123 -93
  110. package/src/irdevice/vacuumcleaner.ts +112 -89
  111. package/src/irdevice/waterheater.ts +111 -89
  112. package/src/platform.ts +195 -216
  113. package/src/settings.ts +15 -11
@@ -6,9 +6,11 @@ import { MqttClient } from 'mqtt';
6
6
  import { connectAsync } from 'async-mqtt';
7
7
  import { interval, Subject } from 'rxjs';
8
8
  import { skipWhile } from 'rxjs/operators';
9
+ import superStringify from 'super-stringify';
9
10
  import { SwitchBotPlatform } from '../platform';
10
11
  import { Service, PlatformAccessory, Units, CharacteristicValue } from 'homebridge';
11
12
  import { device, devicesConfig, serviceData, ad, switchbot, temperature, deviceStatus, HostDomain, DevicePath } from '../settings';
13
+ import { IncomingMessage } from 'http';
12
14
 
13
15
  /**
14
16
  * Platform Accessory
@@ -23,14 +25,10 @@ export class Meter {
23
25
 
24
26
  // Characteristic Values
25
27
  CurrentRelativeHumidity!: CharacteristicValue;
26
- CurrentRelativeHumidityCached!: CharacteristicValue;
27
28
  CurrentTemperature?: CharacteristicValue;
28
- CurrentTemperatureCached?: CharacteristicValue;
29
29
  BatteryLevel?: CharacteristicValue;
30
30
  ChargingState?: CharacteristicValue;
31
31
  StatusLowBattery?: CharacteristicValue;
32
- Active!: CharacteristicValue;
33
- WaterLevel!: CharacteristicValue;
34
32
 
35
33
  // OpenAPI Others
36
34
  Temperature: deviceStatus['temperature'];
@@ -40,13 +38,19 @@ export class Meter {
40
38
  // BLE Others
41
39
  connected?: boolean;
42
40
  switchbot!: switchbot;
43
- SwitchToOpenAPI?: boolean;
44
41
  serviceData!: serviceData;
45
42
  address!: ad['address'];
43
+ SwitchToOpenAPI?: boolean;
46
44
  temperature!: temperature['c'];
47
45
  battery!: serviceData['battery'];
48
46
  humidity!: serviceData['humidity'];
49
47
 
48
+ //MQTT stuff
49
+ mqttClient: MqttClient | null = null;
50
+
51
+ // EVE history service handler
52
+ historyService: any;
53
+
50
54
  // Config
51
55
  scanDuration!: number;
52
56
  deviceLogging!: string;
@@ -56,30 +60,19 @@ export class Meter {
56
60
  meterUpdateInProgress!: boolean;
57
61
  doMeterUpdate: Subject<void>;
58
62
 
59
- //MQTT stuff
60
- mqttClient: MqttClient | null = null;
61
-
62
- // EVE history service handler
63
- historyService: any;
63
+ // Connection
64
+ private readonly BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'Both');
65
+ private readonly OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'Both');
64
66
 
65
67
  constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
66
68
  // default placeholders
67
69
  this.logs(device);
68
70
  this.scan(device);
69
- this.setupHistoryService(device);
70
71
  this.refreshRate(device);
71
72
  this.config(device);
73
+ this.context();
74
+ this.setupHistoryService(device);
72
75
  this.setupMqtt(device);
73
- if (this.CurrentRelativeHumidity === undefined) {
74
- this.CurrentRelativeHumidity = 0;
75
- } else {
76
- this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
77
- }
78
- if (this.CurrentTemperature === undefined) {
79
- this.CurrentTemperature = 0;
80
- } else {
81
- this.CurrentTemperature = this.accessory.context.CurrentTemperature;
82
- }
83
76
 
84
77
  // this is subject we use to track when we need to POST changes to the SwitchBot API
85
78
  this.doMeterUpdate = new Subject();
@@ -100,11 +93,11 @@ export class Meter {
100
93
 
101
94
  // Temperature Sensor Service
102
95
  if (device.meter?.hide_temperature) {
103
- this.debugLog(`Meter: ${accessory.displayName} Removing Temperature Sensor Service`);
96
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
104
97
  this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor);
105
98
  accessory.removeService(this.temperatureservice!);
106
99
  } else if (!this.temperatureservice) {
107
- this.debugLog(`Meter: ${accessory.displayName} Add Temperature Sensor Service`);
100
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
108
101
  (this.temperatureservice =
109
102
  this.accessory.getService(this.platform.Service.TemperatureSensor) || this.accessory.addService(this.platform.Service.TemperatureSensor)),
110
103
  `${accessory.displayName} Temperature Sensor`;
@@ -124,16 +117,16 @@ export class Meter {
124
117
  return this.CurrentTemperature!;
125
118
  });
126
119
  } else {
127
- this.debugLog(`Meter: ${accessory.displayName} Temperature Sensor Service Not Added`);
120
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Temperature Sensor Service Not Added`);
128
121
  }
129
122
 
130
123
  // Humidity Sensor Service
131
124
  if (device.meter?.hide_humidity) {
132
- this.debugLog(`Meter: ${accessory.displayName} Removing Humidity Sensor Service`);
125
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
133
126
  this.humidityservice = this.accessory.getService(this.platform.Service.HumiditySensor);
134
127
  accessory.removeService(this.humidityservice!);
135
128
  } else if (!this.humidityservice) {
136
- this.debugLog(`Meter: ${accessory.displayName} Add Humidity Sensor Service`);
129
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
137
130
  (this.humidityservice =
138
131
  this.accessory.getService(this.platform.Service.HumiditySensor) || this.accessory.addService(this.platform.Service.HumiditySensor)),
139
132
  `${accessory.displayName} Humidity Sensor`;
@@ -149,16 +142,16 @@ export class Meter {
149
142
  return this.CurrentRelativeHumidity;
150
143
  });
151
144
  } else {
152
- this.debugLog(`Meter: ${accessory.displayName} Humidity Sensor Service Not Added`);
145
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Humidity Sensor Service Not Added`);
153
146
  }
154
147
 
155
148
  // Battery Service
156
- if (!device.ble) {
157
- this.debugLog(`Meter: ${accessory.displayName} Removing Battery Service`);
149
+ if (!this.BLE) {
150
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
158
151
  this.batteryService = this.accessory.getService(this.platform.Service.Battery);
159
152
  accessory.removeService(this.batteryService!);
160
- } else if (device.ble && !this.batteryService) {
161
- this.debugLog(`Meter: ${accessory.displayName} Add Battery Service`);
153
+ } else if (this.BLE && !this.batteryService) {
154
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
162
155
  (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
163
156
  `${accessory.displayName} Battery`;
164
157
 
@@ -166,7 +159,7 @@ export class Meter {
166
159
 
167
160
  this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
168
161
  } else {
169
- this.debugLog(`Meter: ${accessory.displayName} Battery Service Not Added`);
162
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Battery Service Not Added`);
170
163
  }
171
164
 
172
165
  // Retrieve initial values and updateHomekit
@@ -180,68 +173,23 @@ export class Meter {
180
173
  });
181
174
  }
182
175
 
183
- /*
184
- * Publish MQTT message for topics of
185
- * 'homebridge-switchbot/meter/xx:xx:xx:xx:xx:xx'
186
- */
187
- mqttPublish(message: any) {
188
- const mac = this.device.deviceId
189
- ?.toLowerCase()
190
- .match(/[\s\S]{1,2}/g)
191
- ?.join(':');
192
- const options = this.device.mqttPubOptions || {};
193
- this.mqttClient?.publish(`homebridge-switchbot/meter/${mac}`, `${message}`, options);
194
- this.debugLog(`Meter: ${this.accessory.displayName} MQTT message: ${message} options:${JSON.stringify(options)}`);
195
- }
196
-
197
- /*
198
- * Setup MQTT hadler if URL is specifed.
199
- */
200
- async setupMqtt(device: device & devicesConfig): Promise<void> {
201
- if (device.mqttURL) {
202
- try {
203
- this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
204
- this.debugLog(`Meter: ${this.accessory.displayName} MQTT connection has been established successfully.`);
205
- this.mqttClient.on('error', (e: Error) => {
206
- this.errorLog(`Meter: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
207
- });
208
- } catch (e) {
209
- this.mqttClient = null;
210
- this.errorLog(`Meter: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
211
- }
212
- }
213
- }
214
-
215
- /*
216
- * Setup EVE history graph feature if enabled.
217
- */
218
- async setupHistoryService(device: device & devicesConfig): Promise<void> {
219
- const mac = this.device
220
- .deviceId!.match(/.{1,2}/g)!
221
- .join(':')
222
- .toLowerCase();
223
- this.historyService = device.history
224
- ? new this.platform.fakegatoAPI('room', this.accessory, {
225
- log: this.platform.log,
226
- storage: 'fs',
227
- filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
228
- })
229
- : null;
230
- }
231
-
232
176
  /**
233
177
  * Parse the device status from the SwitchBot api
234
178
  */
235
179
  async parseStatus(): Promise<void> {
236
- if (this.SwitchToOpenAPI || !this.device.ble) {
180
+ if (this.BLE && !this.SwitchToOpenAPI) {
181
+ await this.BLEparseStatus();
182
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
183
+ this.SwitchToOpenAPI = false;
237
184
  await this.openAPIparseStatus();
238
185
  } else {
239
- await this.BLEparseStatus();
186
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
187
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
240
188
  }
241
189
  }
242
190
 
243
191
  async BLEparseStatus(): Promise<void> {
244
- this.debugLog(`Meter: ${this.accessory.displayName} BLE parseStatus`);
192
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
245
193
 
246
194
  // Battery
247
195
  this.BatteryLevel = Number(this.battery);
@@ -250,39 +198,34 @@ export class Meter {
250
198
  } else {
251
199
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
252
200
  }
253
- this.debugLog(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
201
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
254
202
 
255
203
  // Humidity
256
204
  if (!this.device.meter?.hide_humidity) {
257
205
  this.CurrentRelativeHumidity = this.humidity!;
258
- this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
206
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
259
207
  }
260
208
 
261
209
  // Current Temperature
262
210
  if (!this.device.meter?.hide_temperature) {
263
211
  this.temperature < 0 ? 0 : this.temperature > 100 ? 100 : this.temperature;
264
212
  this.CurrentTemperature = this.temperature;
265
- this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
213
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
266
214
  }
267
215
  }
268
216
 
269
217
  async openAPIparseStatus(): Promise<void> {
270
- if (this.device.ble) {
271
- this.SwitchToOpenAPI = false;
218
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
219
+ // Current Relative Humidity
220
+ if (!this.device.meter?.hide_humidity) {
221
+ this.CurrentRelativeHumidity = this.Humidity!;
222
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
272
223
  }
273
- if (this.platform.config.credentials?.token) {
274
- this.debugLog(`Meter: ${this.accessory.displayName} OpenAPI parseStatus`);
275
- // Current Relative Humidity
276
- if (!this.device.meter?.hide_humidity) {
277
- this.CurrentRelativeHumidity = this.Humidity!;
278
- this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
279
- }
280
224
 
281
- // Current Temperature
282
- if (!this.device.meter?.hide_temperature) {
283
- this.CurrentTemperature = this.Temperature!;
284
- this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
285
- }
225
+ // Current Temperature
226
+ if (!this.device.meter?.hide_temperature) {
227
+ this.CurrentTemperature = this.Temperature!;
228
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
286
229
  }
287
230
  }
288
231
 
@@ -290,22 +233,25 @@ export class Meter {
290
233
  * Asks the SwitchBot API for the latest device information
291
234
  */
292
235
  async refreshStatus(): Promise<void> {
293
- if (this.device.ble) {
294
- this.BLErefreshStatus();
236
+ if (this.BLE) {
237
+ await this.BLERefreshStatus();
238
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
239
+ await this.openAPIRefreshStatus();
295
240
  } else {
296
- this.openAPIRefreshStatus();
241
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
242
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
297
243
  }
298
244
  }
299
245
 
300
- async BLErefreshStatus(): Promise<void> {
301
- this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
246
+ async BLERefreshStatus(): Promise<void> {
247
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
302
248
  const switchbot = await this.platform.connectBLE();
303
249
  // Convert to BLE Address
304
250
  this.device.bleMac = this.device
305
251
  .deviceId!.match(/.{1,2}/g)!
306
252
  .join(':')
307
253
  .toLowerCase();
308
- this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
254
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
309
255
  this.getCustomBLEAddress(switchbot);
310
256
  // Start to monitor advertisement packets
311
257
  if (switchbot !== false) {
@@ -321,29 +267,29 @@ export class Meter {
321
267
  if (this.deviceLogging.includes('debug')) {
322
268
  this.infoLog(this.address);
323
269
  this.infoLog(this.device.bleMac);
324
- this.infoLog(`Meter: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
325
- this.infoLog(`Meter: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
270
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
271
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
326
272
  }
327
273
  if (ad.serviceData.humidity! > 0) {
328
274
  // reject unreliable data
329
- this.serviceData = ad.serviceData;
330
- this.temperature = ad.serviceData.temperature!.c;
331
275
  this.humidity = ad.serviceData.humidity;
332
- this.battery = ad.serviceData.battery;
333
276
  }
334
- this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
277
+ this.serviceData = ad.serviceData;
278
+ this.temperature = ad.serviceData.temperature!.c;
279
+ this.battery = ad.serviceData.battery;
280
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
335
281
  this.debugLog(
336
- `Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, ` +
337
- `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, ` +
282
+ `${this.device.deviceType}: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, ` +
283
+ `temperature: ${superStringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, ` +
338
284
  `battery: ${ad.serviceData.battery}`,
339
285
  );
340
286
 
341
287
  if (this.serviceData) {
342
288
  this.connected = true;
343
- this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
289
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
344
290
  } else {
345
291
  this.connected = false;
346
- this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
292
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
347
293
  }
348
294
  };
349
295
  // Wait 10 seconds
@@ -360,107 +306,66 @@ export class Meter {
360
306
  }
361
307
  })
362
308
  .catch(async (e: any) => {
363
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
364
- if (this.deviceLogging.includes('debug')) {
365
- this.errorLog(
366
- `Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
367
- );
368
- }
369
- if (this.platform.config.credentials?.token) {
370
- this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
371
- this.SwitchToOpenAPI = true;
372
- this.openAPIRefreshStatus();
373
- }
374
309
  this.apiError(e);
310
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
311
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
312
+ await this.BLEconnection(switchbot);
375
313
  });
376
314
  } else {
377
315
  await this.BLEconnection(switchbot);
378
316
  }
379
317
  }
380
318
 
381
- async getCustomBLEAddress(switchbot: any) {
382
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
383
- (async () => {
384
- // Start to monitor advertisement packets
385
- await switchbot.startScan({
386
- model: 'T',
387
- });
388
- // Set an event handler
389
- switchbot.onadvertisement = (ad: any) => {
390
- this.warnLog(`Meter: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
391
- };
392
- await switchbot.wait(10000);
393
- // Stop to monitor
394
- switchbot.stopScan();
395
- })();
396
- }
397
- }
398
-
399
- async BLEconnection(switchbot: any): Promise<void> {
400
- this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
401
- if (this.platform.config.credentials?.token) {
402
- this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
403
- this.SwitchToOpenAPI = true;
404
- await this.openAPIRefreshStatus();
405
- }
406
- }
407
-
408
319
  async openAPIRefreshStatus(): Promise<void> {
409
- if (this.platform.config.credentials?.token) {
410
- this.debugLog(`Meter: ${this.accessory.displayName} OpenAPI RefreshStatus`);
411
- try {
412
- const t = Date.now();
413
- const nonce = 'requestID';
414
- const data = this.platform.config.credentials?.token + t + nonce;
415
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
416
- const sign = signTerm.toString('base64');
417
- this.debugLog(`Meter: ${this.accessory.displayName} sign: ${sign}`);
418
- const options = {
419
- hostname: HostDomain,
420
- port: 443,
421
- path: `${DevicePath}/${this.device.deviceId}/status`,
422
- method: 'GET',
423
- headers: {
424
- Authorization: this.platform.config.credentials?.token,
425
- sign: sign,
426
- nonce: nonce,
427
- t: t,
428
- 'Content-Type': 'application/json',
429
- },
430
- };
431
- const req = https.request(options, (res) => {
432
- this.debugLog(`Meter: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
433
- let rawData = '';
434
- res.on('data', (d) => {
435
- rawData += d;
436
- this.debugLog(`Meter: ${this.accessory.displayName} d: ${d}`);
437
- });
438
- res.on('end', () => {
439
- try {
440
- this.deviceStatus = JSON.parse(rawData);
441
- this.debugLog(`Meter: ${this.accessory.displayName} openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`);
442
- this.Humidity = this.deviceStatus.body.humidity!;
443
- this.Temperature = this.deviceStatus.body.temperature!;
444
- this.parseStatus();
445
- this.updateHomeKitCharacteristics();
446
- } catch (e: any) {
447
- this.errorLog(`Meter: ${this.accessory.displayName} error message: ${e.message}`);
448
- }
449
- });
320
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
321
+ try {
322
+ const t = Date.now();
323
+ const nonce = 'requestID';
324
+ const data = this.platform.config.credentials?.token + t + nonce;
325
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
326
+ const sign = signTerm.toString('base64');
327
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
328
+ const options = {
329
+ hostname: HostDomain,
330
+ port: 443,
331
+ path: `${DevicePath}/${this.device.deviceId}/status`,
332
+ method: 'GET',
333
+ headers: {
334
+ Authorization: this.platform.config.credentials?.token,
335
+ sign: sign,
336
+ nonce: nonce,
337
+ t: t,
338
+ 'Content-Type': 'application/json',
339
+ },
340
+ };
341
+ const req = https.request(options, (res) => {
342
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
343
+ let rawData = '';
344
+ res.on('data', (d) => {
345
+ rawData += d;
346
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
450
347
  });
451
- req.on('error', (e: any) => {
452
- this.errorLog(`Meter: ${this.accessory.displayName} error message: ${e.message}`);
348
+ res.on('end', () => {
349
+ try {
350
+ this.deviceStatus = JSON.parse(rawData);
351
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${superStringify(this.deviceStatus)}`);
352
+ this.Humidity = this.deviceStatus.body.humidity!;
353
+ this.Temperature = this.deviceStatus.body.temperature!;
354
+ this.parseStatus();
355
+ this.updateHomeKitCharacteristics();
356
+ } catch (e: any) {
357
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
358
+ }
453
359
  });
454
- req.end();
455
- } catch (e: any) {
456
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
457
- if (this.deviceLogging.includes('debug')) {
458
- this.errorLog(
459
- `Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
460
- );
461
- }
462
- this.apiError(e);
463
- }
360
+ });
361
+ req.on('error', (e: any) => {
362
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
363
+ });
364
+ req.end();
365
+ } catch (e: any) {
366
+ this.apiError(e);
367
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
368
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
464
369
  }
465
370
  }
466
371
 
@@ -472,38 +377,44 @@ export class Meter {
472
377
  const entry = { time: Math.round(new Date().valueOf() / 1000) };
473
378
  if (!this.device.meter?.hide_humidity) {
474
379
  if (this.CurrentRelativeHumidity === undefined) {
475
- this.debugLog(`Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
380
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
476
381
  } else {
382
+ this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
477
383
  this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
478
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
384
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
385
+ + ` CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
479
386
  mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
480
387
  entry['humidity'] = this.CurrentRelativeHumidity;
481
388
  }
482
389
  }
483
390
  if (!this.device.meter?.hide_temperature) {
484
391
  if (this.CurrentTemperature === undefined) {
485
- this.debugLog(`Meter: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
392
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
486
393
  } else {
487
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
488
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
489
394
  mqttmessage.push(`"temperature": ${this.CurrentTemperature}`);
490
395
  entry['temp'] = this.CurrentTemperature;
396
+ this.accessory.context.CurrentTemperature = this.CurrentTemperature;
397
+ this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
398
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
399
+
491
400
  }
492
401
  }
493
- if (this.device.ble) {
402
+ if (this.BLE) {
494
403
  if (this.BatteryLevel === undefined) {
495
- this.debugLog(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
404
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
496
405
  } else {
497
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
498
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
499
406
  mqttmessage.push(`"battery": ${this.BatteryLevel}`);
407
+ this.accessory.context.BatteryLevel = this.BatteryLevel;
408
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
409
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
500
410
  }
501
411
  if (this.StatusLowBattery === undefined) {
502
- this.debugLog(`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
412
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
503
413
  } else {
504
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
505
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
506
414
  mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
415
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
416
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
417
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
507
418
  }
508
419
  }
509
420
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
@@ -513,6 +424,150 @@ export class Meter {
513
424
  }
514
425
  }
515
426
 
427
+ /*
428
+ * Publish MQTT message for topics of
429
+ * 'homebridge-switchbot/meter/xx:xx:xx:xx:xx:xx'
430
+ */
431
+ mqttPublish(message: any) {
432
+ const mac = this.device.deviceId
433
+ ?.toLowerCase()
434
+ .match(/[\s\S]{1,2}/g)
435
+ ?.join(':');
436
+ const options = this.device.mqttPubOptions || {};
437
+ this.mqttClient?.publish(`homebridge-switchbot/meter/${mac}`, `${message}`, options);
438
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${message} options:${superStringify(options)}`);
439
+ }
440
+
441
+ /*
442
+ * Setup MQTT hadler if URL is specifed.
443
+ */
444
+ async setupMqtt(device: device & devicesConfig): Promise<void> {
445
+ if (device.mqttURL) {
446
+ try {
447
+ this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
448
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
449
+ this.mqttClient.on('error', (e: Error) => {
450
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
451
+ });
452
+ } catch (e) {
453
+ this.mqttClient = null;
454
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
455
+ }
456
+ }
457
+ }
458
+
459
+ /*
460
+ * Setup EVE history graph feature if enabled.
461
+ */
462
+ async setupHistoryService(device: device & devicesConfig): Promise<void> {
463
+ const mac = this.device
464
+ .deviceId!.match(/.{1,2}/g)!
465
+ .join(':')
466
+ .toLowerCase();
467
+ this.historyService = device.history
468
+ ? new this.platform.fakegatoAPI('room', this.accessory, {
469
+ log: this.platform.log,
470
+ storage: 'fs',
471
+ filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
472
+ })
473
+ : null;
474
+ }
475
+
476
+ async getCustomBLEAddress(switchbot: any) {
477
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
478
+ (async () => {
479
+ // Start to monitor advertisement packets
480
+ await switchbot.startScan({
481
+ model: 'T',
482
+ });
483
+ // Set an event handler
484
+ switchbot.onadvertisement = (ad: any) => {
485
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${superStringify(ad, null, ' ')}`);
486
+ };
487
+ await switchbot.wait(10000);
488
+ // Stop to monitor
489
+ switchbot.stopScan();
490
+ })();
491
+ }
492
+ }
493
+
494
+ async BLEconnection(switchbot: any): Promise<void> {
495
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
496
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
497
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
498
+ this.SwitchToOpenAPI = true;
499
+ await this.openAPIRefreshStatus();
500
+ }
501
+ }
502
+
503
+ async scan(device: device & devicesConfig): Promise<void> {
504
+ if (device.scanDuration) {
505
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
506
+ if (this.BLE) {
507
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
508
+ }
509
+ } else {
510
+ this.scanDuration = this.accessory.context.scanDuration = 1;
511
+ if (this.BLE) {
512
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
513
+ }
514
+ }
515
+ }
516
+
517
+ async statusCode({ res }: { res: IncomingMessage }): Promise<void> {
518
+ switch (res.statusCode) {
519
+ case 151:
520
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
521
+ break;
522
+ case 152:
523
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
524
+ break;
525
+ case 160:
526
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
527
+ break;
528
+ case 161:
529
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
530
+ this.offlineOff();
531
+ break;
532
+ case 171:
533
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
534
+ this.offlineOff();
535
+ break;
536
+ case 190:
537
+ this.errorLog(
538
+ `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
539
+ ` Or command: ${superStringify(res)} format is invalid`,
540
+ );
541
+ break;
542
+ case 100:
543
+ if (this.platform.debugMode) {
544
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
545
+ }
546
+ break;
547
+ default:
548
+ if (this.platform.debugMode) {
549
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
550
+ }
551
+ }
552
+ }
553
+
554
+ async offlineOff(): Promise<void> {
555
+ if (this.device.offline) {
556
+ if (!this.device.meter?.hide_humidity) {
557
+ this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity,
558
+ this.accessory.context.CurrentRelativeHumidity);
559
+ }
560
+ if (!this.device.meter?.hide_temperature) {
561
+ this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.accessory.context.CurrentTemperature);
562
+ }
563
+ if (this.BLE) {
564
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.accessory.context.BatteryLevel);
565
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.accessory.context.StatusLowBattery);
566
+ }
567
+ await this.updateHomeKitCharacteristics();
568
+ }
569
+ }
570
+
516
571
  async apiError(e: any): Promise<void> {
517
572
  if (!this.device.meter?.hide_humidity) {
518
573
  this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
@@ -520,11 +575,62 @@ export class Meter {
520
575
  if (!this.device.meter?.hide_temperature) {
521
576
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
522
577
  }
523
- if (this.device.ble) {
578
+ if (this.BLE) {
524
579
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
525
580
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
526
581
  }
527
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
582
+ }
583
+
584
+ FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
585
+ let FirmwareRevision: string;
586
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
587
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
588
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
589
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
590
+ if (accessory.context.FirmwareRevision) {
591
+ FirmwareRevision = accessory.context.FirmwareRevision;
592
+ } else if (device.firmware) {
593
+ FirmwareRevision = device.firmware;
594
+ } else {
595
+ FirmwareRevision = this.platform.version;
596
+ }
597
+ return FirmwareRevision;
598
+ }
599
+
600
+ async context() {
601
+ if (this.CurrentRelativeHumidity === undefined) {
602
+ this.CurrentRelativeHumidity = 0;
603
+ } else {
604
+ this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
605
+ }
606
+ if (this.CurrentTemperature === undefined) {
607
+ this.CurrentTemperature = 0;
608
+ } else {
609
+ this.CurrentTemperature = this.accessory.context.CurrentTemperature;
610
+ }
611
+ if (this.BLE) {
612
+ if (this.BatteryLevel === undefined) {
613
+ this.BatteryLevel = 100;
614
+ } else {
615
+ this.BatteryLevel = this.accessory.context.BatteryLevel;
616
+ }
617
+ if (this.StatusLowBattery === undefined) {
618
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
619
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
620
+ } else {
621
+ this.StatusLowBattery = this.accessory.context.StatusLowBattery;
622
+ }
623
+ }
624
+ }
625
+
626
+ async refreshRate(device: device & devicesConfig): Promise<void> {
627
+ if (device.refreshRate) {
628
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
629
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
630
+ } else if (this.platform.config.options!.refreshRate) {
631
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
632
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
633
+ }
528
634
  }
529
635
 
530
636
  async config(device: device & devicesConfig): Promise<void> {
@@ -532,8 +638,8 @@ export class Meter {
532
638
  if (device.meter) {
533
639
  config = device.meter;
534
640
  }
535
- if (device.ble !== undefined) {
536
- config['ble'] = device.ble;
641
+ if (device.connectionType !== undefined) {
642
+ config['connectionType'] = device.connectionType;
537
643
  }
538
644
  if (device.logging !== undefined) {
539
645
  config['logging'] = device.logging;
@@ -545,65 +651,26 @@ export class Meter {
545
651
  config['scanDuration'] = device.scanDuration;
546
652
  }
547
653
  if (Object.entries(config).length !== 0) {
548
- this.infoLog(`Meter: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
549
- }
550
- }
551
-
552
- async refreshRate(device: device & devicesConfig): Promise<void> {
553
- if (device.refreshRate) {
554
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
555
- this.debugLog(`Meter: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
556
- } else if (this.platform.config.options!.refreshRate) {
557
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
558
- this.debugLog(`Meter: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
559
- }
560
- }
561
-
562
- async scan(device: device & devicesConfig): Promise<void> {
563
- if (device.scanDuration) {
564
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
565
- if (device.ble) {
566
- this.debugLog(`Meter: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
567
- }
568
- } else {
569
- this.scanDuration = this.accessory.context.scanDuration = 1;
570
- if (this.device.ble) {
571
- this.debugLog(`Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
572
- }
654
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${superStringify(config)}`);
573
655
  }
574
656
  }
575
657
 
576
658
  async logs(device: device & devicesConfig): Promise<void> {
577
659
  if (this.platform.debugMode) {
578
660
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
579
- this.debugLog(`Meter: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
661
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
580
662
  } else if (device.logging) {
581
663
  this.deviceLogging = this.accessory.context.logging = device.logging;
582
- this.debugLog(`Meter: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
664
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
583
665
  } else if (this.platform.config.options?.logging) {
584
666
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
585
- this.debugLog(`Meter: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
667
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
586
668
  } else {
587
669
  this.deviceLogging = this.accessory.context.logging = 'standard';
588
- this.debugLog(`Meter: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
670
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
589
671
  }
590
672
  }
591
673
 
592
- FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
593
- let FirmwareRevision: string;
594
- this.debugLog(`Meter: ${this.accessory.displayName} accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
595
- this.debugLog(`Meter: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
596
- this.debugLog(`Meter: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
597
- if (accessory.context.FirmwareRevision) {
598
- FirmwareRevision = accessory.context.FirmwareRevision;
599
- } else if (device.firmware) {
600
- FirmwareRevision = device.firmware;
601
- } else {
602
- FirmwareRevision = this.platform.version;
603
- }
604
- return FirmwareRevision;
605
- }
606
-
607
674
  /**
608
675
  * Logging for Device
609
676
  */