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