@switchbot/homebridge-switchbot 1.3.1-beta.9 → 1.4.1-beta.2

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 (103) hide show
  1. package/CHANGELOG.md +18 -2
  2. package/config.schema.json +82 -32
  3. package/dist/devices/bots.d.ts +15 -3
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +227 -165
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +14 -3
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +163 -105
  10. package/dist/devices/colorbulb.js.map +1 -1
  11. package/dist/devices/contact.d.ts +13 -3
  12. package/dist/devices/contact.d.ts.map +1 -1
  13. package/dist/devices/contact.js +102 -63
  14. package/dist/devices/contact.js.map +1 -1
  15. package/dist/devices/curtains.d.ts +14 -5
  16. package/dist/devices/curtains.d.ts.map +1 -1
  17. package/dist/devices/curtains.js +180 -140
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +14 -3
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +173 -133
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +17 -13
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +150 -105
  26. package/dist/devices/meters.js.map +1 -1
  27. package/dist/devices/motion.d.ts +13 -3
  28. package/dist/devices/motion.d.ts.map +1 -1
  29. package/dist/devices/motion.js +89 -45
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +15 -3
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +115 -53
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +13 -3
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +97 -46
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +13 -3
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +85 -31
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +12 -3
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +73 -24
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +12 -3
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +83 -35
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +12 -3
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +74 -25
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +12 -3
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +80 -31
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +12 -3
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +90 -42
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +12 -3
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +73 -24
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +12 -3
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +73 -24
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +10 -10
  72. package/dist/platform.d.ts.map +1 -1
  73. package/dist/platform.js +290 -307
  74. package/dist/platform.js.map +1 -1
  75. package/dist/settings.d.ts +9 -6
  76. package/dist/settings.d.ts.map +1 -1
  77. package/dist/settings.js.map +1 -1
  78. package/nodemon.json +1 -1
  79. package/package.json +6 -6
  80. package/src/devices/bots.ts +221 -152
  81. package/src/devices/colorbulb.ts +172 -108
  82. package/src/devices/contact.ts +109 -64
  83. package/src/devices/curtains.ts +190 -146
  84. package/src/devices/humidifiers.ts +181 -135
  85. package/src/devices/meters.ts +156 -105
  86. package/src/devices/motion.ts +95 -45
  87. package/src/devices/plugs.ts +121 -53
  88. package/src/irdevices/airconditioners.ts +102 -46
  89. package/src/irdevices/airpurifiers.ts +91 -31
  90. package/src/irdevices/cameras.ts +79 -24
  91. package/src/irdevices/fans.ts +89 -36
  92. package/src/irdevices/lights.ts +80 -25
  93. package/src/irdevices/others.ts +86 -31
  94. package/src/irdevices/tvs.ts +96 -42
  95. package/src/irdevices/vacuumcleaners.ts +79 -24
  96. package/src/irdevices/waterheaters.ts +79 -24
  97. package/src/platform.ts +285 -302
  98. package/src/settings.ts +10 -6
  99. package/dist/devices/indoorcam.d.ts +0 -64
  100. package/dist/devices/indoorcam.d.ts.map +0 -1
  101. package/dist/devices/indoorcam.js +0 -249
  102. package/dist/devices/indoorcam.js.map +0 -1
  103. package/src/devices/indoorcam.ts +0 -309
@@ -2,8 +2,8 @@ import Switchbot from 'node-switchbot';
2
2
  import { interval, Subject } from 'rxjs';
3
3
  import { skipWhile } from 'rxjs/operators';
4
4
  import { SwitchBotPlatform } from '../platform';
5
- import { Service, PlatformAccessory, Units, CharacteristicValue } from 'homebridge';
6
- import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceStatusResponse } from '../settings';
5
+ import { Service, PlatformAccessory, Units, CharacteristicValue, HAPStatus } from 'homebridge';
6
+ import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceStatusResponse, temperature } from '../settings';
7
7
 
8
8
  /**
9
9
  * Platform Accessory
@@ -18,8 +18,11 @@ export class Meter {
18
18
 
19
19
  // Characteristic Values
20
20
  CurrentRelativeHumidity!: CharacteristicValue;
21
+ CurrentRelativeHumidityCached!: CharacteristicValue;
21
22
  CurrentTemperature!: CharacteristicValue;
23
+ CurrentTemperatureCached!: CharacteristicValue;
22
24
  BatteryLevel!: CharacteristicValue;
25
+ BatteryLevelCached!: CharacteristicValue;
23
26
  ChargingState!: CharacteristicValue;
24
27
  StatusLowBattery!: CharacteristicValue;
25
28
  Active!: CharacteristicValue;
@@ -32,14 +35,13 @@ export class Meter {
32
35
  connected?: boolean;
33
36
  switchbot!: switchbot;
34
37
  serviceData!: serviceData;
38
+ temperature!: temperature['c'];
35
39
  battery!: serviceData['battery'];
36
40
  humidity!: serviceData['humidity'];
37
- fahrenheit!: serviceData['fahrenheit'];
38
- temperature!: serviceData['temperature'];
39
41
 
40
42
  // Config
41
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
42
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
43
+ deviceLogging!: string;
44
+ deviceRefreshRate!: number;
43
45
 
44
46
  // Updates
45
47
  meterUpdateInProgress!: boolean;
@@ -50,16 +52,34 @@ export class Meter {
50
52
  private accessory: PlatformAccessory,
51
53
  public device: device & devicesConfig,
52
54
  ) {
53
- // Meter Config
54
- this.platform.device(`Meter: ${this.accessory.displayName} Config: (ble: ${device.ble}, unit: ${device.meter?.unit},`
55
- + ` hide_temperature: ${device.meter?.hide_temperature}, hide_humidity: ${device.meter?.hide_humidity})`);
56
-
57
55
  // default placeholders
58
- this.BatteryLevel = 0;
59
- this.ChargingState = 2;
60
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
61
- this.CurrentRelativeHumidity = 0;
62
- this.CurrentTemperature = 0;
56
+ this.refreshRate();
57
+ this.logs();
58
+ if (this.BatteryLevel === undefined) {
59
+ this.BatteryLevel = 100;
60
+ } else {
61
+ this.BatteryLevel = this.accessory.context.BatteryLevel;
62
+ }
63
+ if (this.BatteryLevel < 15) {
64
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
65
+ } else {
66
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
67
+ }
68
+ this.ChargingState = this.platform.Characteristic.ChargingState.NOT_CHARGEABLE;
69
+ if (this.CurrentRelativeHumidity === undefined) {
70
+ this.CurrentRelativeHumidity = 0;
71
+ } else {
72
+ this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
73
+ }
74
+ if (this.CurrentTemperature === undefined) {
75
+ this.CurrentTemperature = 0;
76
+ } else {
77
+ this.CurrentTemperature = this.accessory.context.CurrentTemperature;
78
+ }
79
+
80
+ // Meter Config
81
+ this.debugLog(`Meter: ${this.accessory.displayName} Config: (ble: ${device.ble}, hide_temperature: ${device.meter?.hide_temperature},`
82
+ + ` hide_humidity: ${device.meter?.hide_humidity})`);
63
83
 
64
84
  // this is subject we use to track when we need to POST changes to the SwitchBot API
65
85
  this.doMeterUpdate = new Subject();
@@ -97,11 +117,11 @@ export class Meter {
97
117
 
98
118
  // Temperature Sensor Service
99
119
  if (device.meter?.hide_temperature) {
100
- this.platform.device(`Meter: ${accessory.displayName} Removing Temperature Sensor Service`);
120
+ this.debugLog(`Meter: ${accessory.displayName} Removing Temperature Sensor Service`);
101
121
  this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor);
102
122
  accessory.removeService(this.temperatureservice!);
103
123
  } else if (!this.temperatureservice) {
104
- this.platform.device(`Meter: ${accessory.displayName} Add Temperature Sensor Service`);
124
+ this.debugLog(`Meter: ${accessory.displayName} Add Temperature Sensor Service`);
105
125
  (this.temperatureservice =
106
126
  this.accessory.getService(this.platform.Service.TemperatureSensor) ||
107
127
  this.accessory.addService(this.platform.Service.TemperatureSensor)), `${accessory.displayName} Temperature Sensor`;
@@ -121,16 +141,16 @@ export class Meter {
121
141
  return this.CurrentTemperature;
122
142
  });
123
143
  } else {
124
- this.platform.device(`Meter: ${accessory.displayName} Temperature Sensor Service Not Added`);
144
+ this.debugLog(`Meter: ${accessory.displayName} Temperature Sensor Service Not Added`);
125
145
  }
126
146
 
127
147
  // Humidity Sensor Service
128
148
  if (device.meter?.hide_humidity) {
129
- this.platform.device(`Meter: ${accessory.displayName} Removing Humidity Sensor Service`);
149
+ this.debugLog(`Meter: ${accessory.displayName} Removing Humidity Sensor Service`);
130
150
  this.humidityservice = this.accessory.getService(this.platform.Service.HumiditySensor);
131
151
  accessory.removeService(this.humidityservice!);
132
152
  } else if (!this.humidityservice) {
133
- this.platform.device(`Meter: ${accessory.displayName} Add Humidity Sensor Service`);
153
+ this.debugLog(`Meter: ${accessory.displayName} Add Humidity Sensor Service`);
134
154
  (this.humidityservice =
135
155
  this.accessory.getService(this.platform.Service.HumiditySensor) ||
136
156
  this.accessory.addService(this.platform.Service.HumiditySensor)), `${accessory.displayName} Humidity Sensor`;
@@ -146,20 +166,38 @@ export class Meter {
146
166
  return this.CurrentRelativeHumidity;
147
167
  });
148
168
  } else {
149
- this.platform.device(`Meter: ${accessory.displayName} Humidity Sensor Service Not Added`);
169
+ this.debugLog(`Meter: ${accessory.displayName} Humidity Sensor Service Not Added`);
150
170
  }
151
171
 
152
172
  // Retrieve initial values and updateHomekit
153
173
  this.updateHomeKitCharacteristics();
154
174
 
155
175
  // Start an update interval
156
- interval(this.platform.config.options!.refreshRate! * 1000)
176
+ interval(this.deviceRefreshRate * 1000)
157
177
  .pipe(skipWhile(() => this.meterUpdateInProgress))
158
178
  .subscribe(() => {
159
179
  this.refreshStatus();
160
180
  });
161
181
  }
162
182
 
183
+ refreshRate() {
184
+ if (this.device.refreshRate) {
185
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
186
+ } else if (this.platform.config.options!.refreshRate) {
187
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
188
+ }
189
+ }
190
+
191
+ logs() {
192
+ if (this.device.logging) {
193
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
194
+ } else if (this.platform.config.options?.logging) {
195
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
196
+ } else {
197
+ this.deviceLogging = this.accessory.context.logging = 'standard';
198
+ }
199
+ }
200
+
163
201
  /**
164
202
  * Parse the device status from the SwitchBot api
165
203
  */
@@ -172,65 +210,54 @@ export class Meter {
172
210
  }
173
211
 
174
212
  private async BLEparseStatus() {
175
- this.platform.debug(`Meter: ${this.accessory.displayName} BLE parseStatus`);
213
+ this.debugLog(`Meter: ${this.accessory.displayName} BLE parseStatus`);
176
214
 
177
215
  // Battery
178
216
  this.BatteryLevel = Number(this.battery);
179
217
  if (this.BatteryLevel < 15) {
180
- this.StatusLowBattery = 1;
218
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
181
219
  } else {
182
- this.StatusLowBattery = 0;
220
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
183
221
  }
184
- this.platform.debug(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
222
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
185
223
 
186
224
  if (!this.device.meter?.hide_humidity) {
187
225
  // Humidity
188
226
  this.CurrentRelativeHumidity = Number(this.humidity);
189
- this.platform.debug(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
227
+ this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
190
228
  }
191
229
 
192
230
  // Current Temperature
193
231
  if (!this.device.meter?.hide_temperature) {
194
- if (this.device.meter?.unit === 1) {
195
- this.CurrentTemperature = this.toFahrenheit(this.temperature!.c);
196
- } else if (this.device.meter?.unit === 0) {
197
- this.CurrentTemperature = this.toCelsius(this.temperature!.c);
198
- } else {
199
- this.CurrentTemperature = Number(this.temperature?.c);
200
- }
201
- this.platform.debug(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
232
+ this.temperature < 0 ? 0 : this.temperature > 100 ? 100 : this.temperature;
233
+ this.CurrentTemperature = this.temperature;
234
+ this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
202
235
  }
203
236
  }
204
237
 
205
238
  private async openAPIparseStatus() {
206
239
  if (this.platform.config.credentials?.openToken) {
207
- this.platform.debug(`Meter: ${this.accessory.displayName} OpenAPI parseStatus`);
240
+ this.debugLog(`Meter: ${this.accessory.displayName} OpenAPI parseStatus`);
208
241
  if (this.deviceStatus.body) {
209
242
  this.BatteryLevel = 100;
210
243
  } else {
211
244
  this.BatteryLevel = 10;
212
245
  }
213
246
  if (this.BatteryLevel < 15) {
214
- this.StatusLowBattery = 1;
247
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
215
248
  } else {
216
- this.StatusLowBattery = 0;
249
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
217
250
  }
218
251
  // Current Relative Humidity
219
252
  if (!this.device.meter?.hide_humidity) {
220
253
  this.CurrentRelativeHumidity = this.deviceStatus.body.humidity!;
221
- this.platform.debug(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
254
+ this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
222
255
  }
223
256
 
224
257
  // Current Temperature
225
258
  if (!this.device.meter?.hide_temperature) {
226
- if (this.device.meter?.unit === 1) {
227
- this.CurrentTemperature = this.toFahrenheit(this.deviceStatus.body.temperature!);
228
- } else if (this.device.meter?.unit === 0) {
229
- this.CurrentTemperature = this.toCelsius(this.deviceStatus.body.temperature!);
230
- } else {
231
- this.CurrentTemperature = Number(this.deviceStatus.body.temperature);
232
- }
233
- this.platform.debug(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
259
+ this.CurrentTemperature = Number(this.deviceStatus.body.temperature);
260
+ this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
234
261
  }
235
262
  }
236
263
  }
@@ -240,21 +267,21 @@ export class Meter {
240
267
  */
241
268
  async refreshStatus() {
242
269
  if (this.device.ble) {
243
- await this.BLErefreshStatus();
270
+ this.BLErefreshStatus();
244
271
  } else {
245
- await this.openAPIRefreshStatus();
272
+ this.openAPIRefreshStatus();
246
273
  }
247
274
  }
248
275
 
249
276
  private connectBLE() {
250
277
  const switchbot = new Switchbot();
251
278
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
252
- this.platform.device(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
279
+ this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
253
280
  return switchbot;
254
281
  }
255
282
 
256
- private async BLErefreshStatus() {
257
- this.platform.debug(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
283
+ private BLErefreshStatus() {
284
+ this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
258
285
  // eslint-disable-next-line @typescript-eslint/no-var-requires
259
286
  const switchbot = this.connectBLE();
260
287
  // Start to monitor advertisement packets
@@ -265,21 +292,20 @@ export class Meter {
265
292
  // Set an event hander
266
293
  switchbot.onadvertisement = (ad: ad) => {
267
294
  this.serviceData = ad.serviceData;
268
- this.temperature = ad.serviceData.temperature;
269
- this.fahrenheit = ad.serviceData.fahrenheit;
295
+ this.temperature = ad.serviceData.temperature!.c;
270
296
  this.humidity = ad.serviceData.humidity;
271
297
  this.battery = ad.serviceData.battery;
272
- this.platform.device(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
273
- this.platform.device(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
274
- + `temperature: ${JSON.stringify(ad.serviceData.temperature)}, fahrenheit: ${ad.serviceData.fahrenheit}, `
275
- + `humidity: ${ad.serviceData.humidity}, battery: ${ad.serviceData.battery}`);
298
+ this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
299
+ this.debugLog(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
300
+ + `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, `
301
+ + `battery: ${ad.serviceData.battery}`);
276
302
 
277
303
  if (this.serviceData) {
278
304
  this.connected = true;
279
- this.platform.device(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
305
+ this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
280
306
  } else {
281
307
  this.connected = false;
282
- this.platform.device(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
308
+ this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
283
309
  }
284
310
  };
285
311
  // Wait 10 seconds
@@ -288,52 +314,64 @@ export class Meter {
288
314
  // Stop to monitor
289
315
  switchbot.stopScan();
290
316
  if (this.connected) {
317
+ this.CurrentTemperatureCached = this.temperature;
318
+ this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
319
+ this.CurrentRelativeHumidityCached = this.humidity!;
320
+ this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
321
+ this.BatteryLevelCached = this.battery!;
322
+ this.accessory.context.BatteryLevel = this.BatteryLevelCached;
291
323
  this.parseStatus();
292
324
  this.updateHomeKitCharacteristics();
293
325
  } else {
294
- this.platform.log.error(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
326
+ this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
295
327
  if (this.platform.config.credentials?.openToken) {
296
- this.platform.log.warn(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
297
- await this.openAPIRefreshStatus();
328
+ this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
329
+ this.openAPIRefreshStatus();
298
330
  }
299
331
  }
300
332
  }).catch(async (e: any) => {
301
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
302
- if (this.deviceDebug) {
303
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
333
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
334
+ if (this.deviceLogging === 'debug') {
335
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
304
336
  + ` Error Message: ${JSON.stringify(e.message)}`);
305
337
  }
306
- if (this.debugDebug) {
307
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
338
+ if (this.platform.debugMode) {
339
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
308
340
  + ` Error: ${JSON.stringify(e)}`);
309
341
  }
310
342
  if (this.platform.config.credentials?.openToken) {
311
- this.platform.log.warn(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
312
- await this.openAPIRefreshStatus();
343
+ this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
344
+ this.openAPIRefreshStatus();
313
345
  }
314
- this.apiError(e);
346
+ this.apiError();
315
347
  });
316
348
  }
317
349
 
318
350
  private async openAPIRefreshStatus() {
319
351
  if (this.platform.config.credentials?.openToken) {
320
- this.platform.debug(`Meter: ${this.accessory.displayName} OpenAPI RefreshStatus`);
352
+ this.debugLog(`Meter: ${this.accessory.displayName} OpenAPI RefreshStatus`);
321
353
  try {
322
354
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
323
- this.platform.debug(`Meter: ${this.accessory.displayName} openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`);
355
+ this.debugLog(`Meter: ${this.accessory.displayName} openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`);
356
+ this.CurrentTemperatureCached = this.deviceStatus.body.humidity!;
357
+ this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
358
+ this.CurrentRelativeHumidityCached = this.deviceStatus.body.humidity!;
359
+ this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
360
+ this.BatteryLevelCached = 100;
361
+ this.accessory.context.BatteryLevel = this.BatteryLevelCached;
324
362
  this.parseStatus();
325
363
  this.updateHomeKitCharacteristics();
326
364
  } catch (e: any) {
327
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
328
- if (this.deviceDebug) {
329
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
365
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
366
+ if (this.deviceLogging === 'debug') {
367
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
330
368
  + ` Error Message: ${JSON.stringify(e.message)}`);
331
369
  }
332
- if (this.debugDebug) {
333
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
370
+ if (this.platform.debugMode) {
371
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
334
372
  + ` Error: ${JSON.stringify(e)}`);
335
373
  }
336
- this.apiError(e);
374
+ this.apiError();
337
375
  }
338
376
  }
339
377
  }
@@ -343,58 +381,71 @@ export class Meter {
343
381
  */
344
382
  updateHomeKitCharacteristics() {
345
383
  if (this.StatusLowBattery === undefined) {
346
- this.platform.debug(`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
384
+ this.debugLog(`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
347
385
  } else {
348
386
  this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
349
- this.platform.device(`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
387
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
350
388
  }
351
389
  if (this.BatteryLevel === undefined) {
352
- this.platform.debug(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
390
+ this.debugLog(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
353
391
  } else {
354
392
  this.service.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
355
- this.platform.device(`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
393
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
356
394
  }
357
395
  if (!this.device.meter?.hide_humidity) {
358
396
  if (this.CurrentRelativeHumidity === undefined) {
359
- this.platform.debug(`Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
397
+ this.debugLog(`Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
360
398
  } else {
361
399
  this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
362
- this.platform.device(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
400
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
363
401
  }
364
402
  }
365
403
  if (!this.device.meter?.hide_temperature) {
366
404
  if (this.CurrentTemperature === undefined) {
367
- this.platform.debug(`Meter: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
405
+ this.debugLog(`Meter: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
368
406
  } else {
369
407
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
370
- this.platform.device(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
408
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
371
409
  }
372
410
  }
373
411
  }
374
412
 
375
- public apiError(e: any) {
376
- this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
377
- this.service.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
378
- if (!this.device.meter?.hide_humidity) {
379
- this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
413
+ public apiError() {
414
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
415
+ }
416
+
417
+ /**
418
+ * Logging for Device
419
+ */
420
+ infoLog(...log: any[]) {
421
+ if (this.enablingDeviceLogging()) {
422
+ this.platform.log.info(String(...log));
380
423
  }
381
- if (!this.device.meter?.hide_temperature) {
382
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
424
+ }
425
+
426
+ warnLog(...log: any[]) {
427
+ if (this.enablingDeviceLogging()) {
428
+ this.platform.log.warn(String(...log));
383
429
  }
384
430
  }
385
431
 
386
- /**
387
- * Converts the value to celsius if the temperature units are in Fahrenheit
388
- */
389
- toCelsius(value: number) {
390
- // celsius should be to the nearest 0.5 degree
391
- return Math.round((5 / 9) * (value - 32) * 2) / 2;
432
+ errorLog(...log: any[]) {
433
+ if (this.enablingDeviceLogging()) {
434
+ this.platform.log.error(String(...log));
435
+ }
392
436
  }
393
437
 
394
- /**
395
- * Converts the value to fahrenheit if the temperature units are in Fahrenheit
396
- */
397
- toFahrenheit(value: number) {
398
- return Math.round((value * 9) / 5 + 32);
438
+ debugLog(...log: any[]) {
439
+ if (this.enablingDeviceLogging()) {
440
+ if (this.deviceLogging === 'debug') {
441
+ this.platform.log.info('[DEBUG]', String(...log));
442
+ } else {
443
+ this.platform.log.debug(String(...log));
444
+ }
445
+ }
446
+ }
447
+
448
+ enablingDeviceLogging(): boolean {
449
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
399
450
  }
400
451
  }