@switchbot/homebridge-switchbot 2.0.0-beta.4 → 2.0.0-beta.41

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