@switchbot/homebridge-switchbot 2.9.0-beta.20 → 2.9.0-beta.21
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/config.schema.json +4 -4
- package/dist/device/hub.d.ts +7 -8
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +124 -45
- package/dist/device/hub.js.map +1 -1
- package/dist/device/meter.d.ts +0 -8
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +7 -15
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +1 -4
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +15 -19
- package/dist/device/meterplus.js.map +1 -1
- package/package.json +1 -1
- package/src/device/hub.ts +138 -52
- package/src/device/meter.ts +8 -22
- package/src/device/meterplus.ts +17 -26
package/src/device/hub.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { connectAsync } from 'async-mqtt';
|
|
1
2
|
import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
|
|
3
|
+
import { MqttClient } from 'mqtt';
|
|
4
|
+
import { hostname } from 'os';
|
|
2
5
|
import { interval } from 'rxjs';
|
|
3
6
|
import { request } from 'undici';
|
|
4
7
|
import { Context } from 'vm';
|
|
5
8
|
import { SwitchBotPlatform } from '../platform';
|
|
6
9
|
import { Devices, device, deviceStatus, devicesConfig } from '../settings';
|
|
7
|
-
import { sleep } from '../utils';
|
|
8
10
|
|
|
9
11
|
export class Hub {
|
|
10
12
|
// Services
|
|
@@ -27,6 +29,12 @@ export class Hub {
|
|
|
27
29
|
// OpenAPI Others
|
|
28
30
|
spaceBetweenLevels!: number;
|
|
29
31
|
|
|
32
|
+
//MQTT stuff
|
|
33
|
+
mqttClient: MqttClient | null = null;
|
|
34
|
+
|
|
35
|
+
// EVE history service handler
|
|
36
|
+
historyService?: any;
|
|
37
|
+
|
|
30
38
|
// Config
|
|
31
39
|
set_minStep!: number;
|
|
32
40
|
updateRate!: number;
|
|
@@ -48,6 +56,9 @@ export class Hub {
|
|
|
48
56
|
// default placeholders
|
|
49
57
|
this.logs(device);
|
|
50
58
|
this.refreshRate(device);
|
|
59
|
+
this.context(accessory, device);
|
|
60
|
+
this.setupHistoryService(device);
|
|
61
|
+
this.setupMqtt(device);
|
|
51
62
|
this.config(device);
|
|
52
63
|
|
|
53
64
|
this.CurrentRelativeHumidity = accessory.context.CurrentRelativeHumidity;
|
|
@@ -134,7 +145,7 @@ export class Hub {
|
|
|
134
145
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Humidity Sensor Service Not Added`);
|
|
135
146
|
}
|
|
136
147
|
|
|
137
|
-
//
|
|
148
|
+
// Light Sensor Service
|
|
138
149
|
if (device.hub?.hide_lightsensor) {
|
|
139
150
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
|
|
140
151
|
this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor);
|
|
@@ -151,13 +162,14 @@ export class Hub {
|
|
|
151
162
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
|
|
152
163
|
}
|
|
153
164
|
|
|
154
|
-
//
|
|
165
|
+
// Retrieve initial values and update Homekit
|
|
155
166
|
this.updateHomeKitCharacteristics();
|
|
156
167
|
|
|
157
168
|
// Start an update interval
|
|
158
|
-
interval(this.deviceRefreshRate * 1000)
|
|
159
|
-
|
|
160
|
-
|
|
169
|
+
interval(this.deviceRefreshRate * 1000)
|
|
170
|
+
.subscribe(async () => {
|
|
171
|
+
await this.refreshStatus();
|
|
172
|
+
});
|
|
161
173
|
}
|
|
162
174
|
|
|
163
175
|
/**
|
|
@@ -176,14 +188,16 @@ export class Hub {
|
|
|
176
188
|
|
|
177
189
|
async openAPIparseStatus(): Promise<void> {
|
|
178
190
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
179
|
-
//
|
|
180
|
-
if (!this.device.hub?.hide_temperature) {
|
|
181
|
-
this.CurrentTemperature = Number(this.OpenAPI_CurrentTemperature);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// Humidity
|
|
191
|
+
// CurrentRelativeHumidity
|
|
185
192
|
if (!this.device.hub?.hide_humidity) {
|
|
186
193
|
this.CurrentRelativeHumidity = Number(this.OpenAPI_CurrentRelativeHumidity);
|
|
194
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// CurrentTemperature
|
|
198
|
+
if (!this.device.hub?.hide_temperature) {
|
|
199
|
+
this.CurrentTemperature = Number(this.OpenAPI_CurrentTemperature);
|
|
200
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
|
|
187
201
|
}
|
|
188
202
|
|
|
189
203
|
// Brightness
|
|
@@ -201,7 +215,7 @@ export class Hub {
|
|
|
201
215
|
this.CurrentAmbientLightLevel = (this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels;
|
|
202
216
|
this.debugLog(
|
|
203
217
|
`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.OpenAPI_CurrentAmbientLightLevel},` +
|
|
204
|
-
|
|
218
|
+
` Calculation: ${(this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels}`,
|
|
205
219
|
);
|
|
206
220
|
break;
|
|
207
221
|
case 3:
|
|
@@ -279,7 +293,7 @@ export class Hub {
|
|
|
279
293
|
}
|
|
280
294
|
this.debugLog(
|
|
281
295
|
`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.OpenAPI_CurrentAmbientLightLevel},` +
|
|
282
|
-
|
|
296
|
+
` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
|
|
283
297
|
);
|
|
284
298
|
}
|
|
285
299
|
if (!this.device.hub?.hide_lightsensor) {
|
|
@@ -335,49 +349,46 @@ export class Hub {
|
|
|
335
349
|
}
|
|
336
350
|
}
|
|
337
351
|
|
|
338
|
-
async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
|
|
339
|
-
return fn().catch(async (e: any) => {
|
|
340
|
-
if (max === 0) {
|
|
341
|
-
throw e;
|
|
342
|
-
}
|
|
343
|
-
this.infoLog(e);
|
|
344
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
|
|
345
|
-
await sleep(1000);
|
|
346
|
-
return this.retry({ max: max - 1, fn });
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
maxRetry(): number {
|
|
351
|
-
if (this.device.maxRetry) {
|
|
352
|
-
return this.device.maxRetry;
|
|
353
|
-
} else {
|
|
354
|
-
return 5;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
352
|
/**
|
|
359
353
|
* Handle requests to set the value of the "Target Position" characteristic
|
|
360
354
|
*/
|
|
361
355
|
|
|
362
356
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
this.accessory.context.CurrentTemperature = this.CurrentTemperature;
|
|
368
|
-
this.temperatureService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
|
|
369
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
357
|
+
const mqttmessage: string[] = [];
|
|
358
|
+
const entry = { time: Math.round(new Date().valueOf() / 1000) };
|
|
359
|
+
|
|
360
|
+
// CurrentRelativeHumidity
|
|
372
361
|
if (!this.device.hub?.hide_humidity) {
|
|
373
362
|
if (this.CurrentRelativeHumidity === undefined) {
|
|
374
363
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
|
|
375
364
|
} else {
|
|
365
|
+
if (this.device.mqttURL) {
|
|
366
|
+
mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
|
|
367
|
+
}
|
|
368
|
+
if (this.device.history) {
|
|
369
|
+
entry['humidity'] = this.CurrentRelativeHumidity;
|
|
370
|
+
}
|
|
376
371
|
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
|
|
377
372
|
this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
|
|
378
373
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
379
|
-
+ `updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}
|
|
380
|
-
|
|
374
|
+
+ `updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// CurrentTemperature
|
|
379
|
+
if (!this.device.hub?.hide_temperature) {
|
|
380
|
+
if (this.CurrentTemperature === undefined) {
|
|
381
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
|
|
382
|
+
} else {
|
|
383
|
+
if (this.device.mqttURL) {
|
|
384
|
+
mqttmessage.push(`"temperature": ${this.CurrentTemperature}`);
|
|
385
|
+
}
|
|
386
|
+
if (this.device.history) {
|
|
387
|
+
entry['temp'] = this.CurrentTemperature;
|
|
388
|
+
}
|
|
389
|
+
this.accessory.context.CurrentTemperature = this.CurrentTemperature;
|
|
390
|
+
this.temperatureService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
|
|
391
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
|
|
381
392
|
}
|
|
382
393
|
}
|
|
383
394
|
if (!this.device.hub?.hide_lightsensor) {
|
|
@@ -388,7 +399,7 @@ export class Hub {
|
|
|
388
399
|
this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
|
|
389
400
|
this.debugLog(
|
|
390
401
|
`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
391
|
-
|
|
402
|
+
+ `updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
|
|
392
403
|
);
|
|
393
404
|
}
|
|
394
405
|
}
|
|
@@ -411,6 +422,66 @@ export class Hub {
|
|
|
411
422
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
412
423
|
+ `updateCharacteristic FirmwareRevision: ${this.FirmwareRevision}`);
|
|
413
424
|
}
|
|
425
|
+
|
|
426
|
+
// MQTT
|
|
427
|
+
if (this.device.mqttURL) {
|
|
428
|
+
this.mqttPublish(`{${mqttmessage.join(',')}}`);
|
|
429
|
+
}
|
|
430
|
+
if (Number(this.CurrentRelativeHumidity) > 0) {
|
|
431
|
+
// reject unreliable data
|
|
432
|
+
if (this.device.history) {
|
|
433
|
+
this.historyService?.addEntry(entry);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/*
|
|
439
|
+
* Publish MQTT message for topics of
|
|
440
|
+
* 'homebridge-switchbot/meter/xx:xx:xx:xx:xx:xx'
|
|
441
|
+
*/
|
|
442
|
+
mqttPublish(message: any) {
|
|
443
|
+
const mac = this.device.deviceId
|
|
444
|
+
?.toLowerCase()
|
|
445
|
+
.match(/[\s\S]{1,2}/g)
|
|
446
|
+
?.join(':');
|
|
447
|
+
const options = this.device.mqttPubOptions || {};
|
|
448
|
+
this.mqttClient?.publish(`homebridge-switchbot/meter/${mac}`, `${message}`, options);
|
|
449
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${message} options:${JSON.stringify(options)}`);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/*
|
|
453
|
+
* Setup MQTT hadler if URL is specifed.
|
|
454
|
+
*/
|
|
455
|
+
async setupMqtt(device: device & devicesConfig): Promise<void> {
|
|
456
|
+
if (device.mqttURL) {
|
|
457
|
+
try {
|
|
458
|
+
this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
|
|
459
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
|
|
460
|
+
this.mqttClient.on('error', (e: Error) => {
|
|
461
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
|
|
462
|
+
});
|
|
463
|
+
} catch (e) {
|
|
464
|
+
this.mqttClient = null;
|
|
465
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/*
|
|
471
|
+
* Setup EVE history graph feature if enabled.
|
|
472
|
+
*/
|
|
473
|
+
async setupHistoryService(device: device & devicesConfig): Promise<void> {
|
|
474
|
+
const mac = this.device
|
|
475
|
+
.deviceId!.match(/.{1,2}/g)!
|
|
476
|
+
.join(':')
|
|
477
|
+
.toLowerCase();
|
|
478
|
+
this.historyService = device.history
|
|
479
|
+
? new this.platform.fakegatoAPI('room', this.accessory, {
|
|
480
|
+
log: this.platform.log,
|
|
481
|
+
storage: 'fs',
|
|
482
|
+
filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
|
|
483
|
+
})
|
|
484
|
+
: null;
|
|
414
485
|
}
|
|
415
486
|
|
|
416
487
|
async statusCode(statusCode: number): Promise<void> {
|
|
@@ -457,6 +528,7 @@ export class Hub {
|
|
|
457
528
|
|
|
458
529
|
async offlineOff(): Promise<void> {
|
|
459
530
|
if (this.device.offline) {
|
|
531
|
+
await this.context(this.accessory, this.device);
|
|
460
532
|
await this.updateHomeKitCharacteristics();
|
|
461
533
|
}
|
|
462
534
|
}
|
|
@@ -498,16 +570,11 @@ export class Hub {
|
|
|
498
570
|
accessory.context.FirmwareRevision = device.firmware;
|
|
499
571
|
this.FirmwareRevision = accessory.context.FirmwareRevision;
|
|
500
572
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware, FirmwareRevision: ${this.FirmwareRevision}`);
|
|
501
|
-
} else
|
|
573
|
+
} else {
|
|
502
574
|
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version: ${device.version}`);
|
|
503
575
|
accessory.context.FirmwareRevision = device.version;
|
|
504
576
|
this.FirmwareRevision = accessory.context.FirmwareRevision;
|
|
505
577
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version, FirmwareRevision: ${this.FirmwareRevision}`);
|
|
506
|
-
} else {
|
|
507
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
508
|
-
accessory.context.FirmwareRevision = this.platform.version;
|
|
509
|
-
this.FirmwareRevision = accessory.context.FirmwareRevision;
|
|
510
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version, FirmwareRevision: ${this.FirmwareRevision}`);
|
|
511
578
|
}
|
|
512
579
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
|
|
513
580
|
accessory
|
|
@@ -517,6 +584,25 @@ export class Hub {
|
|
|
517
584
|
.updateValue(this.FirmwareRevision);
|
|
518
585
|
}
|
|
519
586
|
|
|
587
|
+
async context(accessory, device) {
|
|
588
|
+
if (this.CurrentRelativeHumidity === undefined) {
|
|
589
|
+
this.CurrentRelativeHumidity = 0;
|
|
590
|
+
} else {
|
|
591
|
+
this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
|
|
592
|
+
}
|
|
593
|
+
if (this.CurrentTemperature === undefined) {
|
|
594
|
+
this.CurrentTemperature = 0;
|
|
595
|
+
} else {
|
|
596
|
+
this.CurrentTemperature = this.accessory.context.CurrentTemperature;
|
|
597
|
+
}
|
|
598
|
+
if (this.FirmwareRevision === undefined) {
|
|
599
|
+
this.FirmwareRevision = this.platform.version;
|
|
600
|
+
} else {
|
|
601
|
+
this.setFirmwareRevision(accessory, device);
|
|
602
|
+
this.FirmwareRevision = this.accessory.context.FirmwareRevision;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
520
606
|
async refreshRate(device: device & devicesConfig): Promise<void> {
|
|
521
607
|
// refreshRate
|
|
522
608
|
if (device.refreshRate) {
|
package/src/device/meter.ts
CHANGED
|
@@ -2,19 +2,13 @@ import { connectAsync } from 'async-mqtt';
|
|
|
2
2
|
import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
|
|
3
3
|
import { MqttClient } from 'mqtt';
|
|
4
4
|
import { hostname } from 'os';
|
|
5
|
-
import {
|
|
6
|
-
import { skipWhile } from 'rxjs/operators';
|
|
5
|
+
import { interval } from 'rxjs';
|
|
7
6
|
import { request } from 'undici';
|
|
8
7
|
import { Context } from 'vm';
|
|
9
8
|
import { SwitchBotPlatform } from '../platform';
|
|
10
9
|
import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature } from '../settings';
|
|
11
10
|
import { sleep } from '../utils';
|
|
12
11
|
|
|
13
|
-
/**
|
|
14
|
-
* Platform Accessory
|
|
15
|
-
* An instance of this class is created for each accessory your platform registers
|
|
16
|
-
* Each accessory may expose multiple services of different service types.
|
|
17
|
-
*/
|
|
18
12
|
export class Meter {
|
|
19
13
|
// Services
|
|
20
14
|
batteryService: Service;
|
|
@@ -55,10 +49,6 @@ export class Meter {
|
|
|
55
49
|
deviceLogging!: string;
|
|
56
50
|
deviceRefreshRate!: number;
|
|
57
51
|
|
|
58
|
-
// Updates
|
|
59
|
-
meterUpdateInProgress!: boolean;
|
|
60
|
-
doMeterUpdate: Subject<void>;
|
|
61
|
-
|
|
62
52
|
// Connection
|
|
63
53
|
private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
|
|
64
54
|
private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
|
|
@@ -77,9 +67,8 @@ export class Meter {
|
|
|
77
67
|
this.setupMqtt(device);
|
|
78
68
|
this.config(device);
|
|
79
69
|
|
|
80
|
-
|
|
81
|
-
this.
|
|
82
|
-
this.meterUpdateInProgress = false;
|
|
70
|
+
this.CurrentRelativeHumidity = accessory.context.CurrentRelativeHumidity;
|
|
71
|
+
this.CurrentTemperature = accessory.context.CurrentTemperature;
|
|
83
72
|
|
|
84
73
|
// Retrieve initial values and updateHomekit
|
|
85
74
|
this.refreshStatus();
|
|
@@ -172,12 +161,11 @@ export class Meter {
|
|
|
172
161
|
}
|
|
173
162
|
this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
|
|
174
163
|
|
|
175
|
-
// Retrieve initial values and
|
|
164
|
+
// Retrieve initial values and update Homekit
|
|
176
165
|
this.updateHomeKitCharacteristics();
|
|
177
166
|
|
|
178
167
|
// Start an update interval
|
|
179
168
|
interval(this.deviceRefreshRate * 1000)
|
|
180
|
-
.pipe(skipWhile(() => this.meterUpdateInProgress))
|
|
181
169
|
.subscribe(async () => {
|
|
182
170
|
await this.refreshStatus();
|
|
183
171
|
});
|
|
@@ -402,18 +390,16 @@ export class Meter {
|
|
|
402
390
|
if (this.CurrentRelativeHumidity === undefined) {
|
|
403
391
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
|
|
404
392
|
} else {
|
|
405
|
-
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
|
|
406
|
-
this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
|
|
407
|
-
this.debugLog(
|
|
408
|
-
`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic` +
|
|
409
|
-
` CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
|
|
410
|
-
);
|
|
411
393
|
if (this.device.mqttURL) {
|
|
412
394
|
mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
|
|
413
395
|
}
|
|
414
396
|
if (this.device.history) {
|
|
415
397
|
entry['humidity'] = this.CurrentRelativeHumidity;
|
|
416
398
|
}
|
|
399
|
+
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
|
|
400
|
+
this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
|
|
401
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
402
|
+
+ `updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
|
|
417
403
|
}
|
|
418
404
|
}
|
|
419
405
|
// CurrentTemperature
|
package/src/device/meterplus.ts
CHANGED
|
@@ -2,8 +2,7 @@ import { connectAsync } from 'async-mqtt';
|
|
|
2
2
|
import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
|
|
3
3
|
import { MqttClient } from 'mqtt';
|
|
4
4
|
import { hostname } from 'os';
|
|
5
|
-
import {
|
|
6
|
-
import { skipWhile } from 'rxjs/operators';
|
|
5
|
+
import { interval } from 'rxjs';
|
|
7
6
|
import { request } from 'undici';
|
|
8
7
|
import { Context } from 'vm';
|
|
9
8
|
import { SwitchBotPlatform } from '../platform';
|
|
@@ -55,10 +54,6 @@ export class MeterPlus {
|
|
|
55
54
|
deviceLogging!: string;
|
|
56
55
|
deviceRefreshRate!: number;
|
|
57
56
|
|
|
58
|
-
// Updates
|
|
59
|
-
meterUpdateInProgress!: boolean;
|
|
60
|
-
doMeterUpdate: Subject<void>;
|
|
61
|
-
|
|
62
57
|
// Connection
|
|
63
58
|
private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
|
|
64
59
|
private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
|
|
@@ -72,15 +67,11 @@ export class MeterPlus {
|
|
|
72
67
|
this.logs(device);
|
|
73
68
|
this.scan(device);
|
|
74
69
|
this.refreshRate(device);
|
|
75
|
-
this.context();
|
|
70
|
+
this.context(accessory, device);
|
|
76
71
|
this.setupHistoryService(device);
|
|
77
72
|
this.setupMqtt(device);
|
|
78
73
|
this.config(device);
|
|
79
74
|
|
|
80
|
-
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
81
|
-
this.doMeterUpdate = new Subject();
|
|
82
|
-
this.meterUpdateInProgress = false;
|
|
83
|
-
|
|
84
75
|
// Retrieve initial values and updateHomekit
|
|
85
76
|
this.refreshStatus();
|
|
86
77
|
|
|
@@ -177,7 +168,6 @@ export class MeterPlus {
|
|
|
177
168
|
|
|
178
169
|
// Start an update interval
|
|
179
170
|
interval(this.deviceRefreshRate * 1000)
|
|
180
|
-
.pipe(skipWhile(() => this.meterUpdateInProgress))
|
|
181
171
|
.subscribe(async () => {
|
|
182
172
|
await this.refreshStatus();
|
|
183
173
|
});
|
|
@@ -402,18 +392,18 @@ export class MeterPlus {
|
|
|
402
392
|
if (this.CurrentRelativeHumidity === undefined) {
|
|
403
393
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
|
|
404
394
|
} else {
|
|
405
|
-
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
|
|
406
|
-
this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
|
|
407
|
-
this.debugLog(
|
|
408
|
-
`${this.device.deviceType}: ${this.accessory.displayName}` +
|
|
409
|
-
` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
|
|
410
|
-
);
|
|
411
395
|
if (this.device.mqttURL) {
|
|
412
396
|
mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
|
|
413
397
|
}
|
|
414
398
|
if (this.device.history) {
|
|
415
399
|
entry['humidity'] = this.CurrentRelativeHumidity;
|
|
416
400
|
}
|
|
401
|
+
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
|
|
402
|
+
this.humidityService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
|
|
403
|
+
this.debugLog(
|
|
404
|
+
`${this.device.deviceType}: ${this.accessory.displayName}` +
|
|
405
|
+
` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
|
|
406
|
+
);
|
|
417
407
|
}
|
|
418
408
|
}
|
|
419
409
|
// CurrentTemperature
|
|
@@ -639,7 +629,7 @@ export class MeterPlus {
|
|
|
639
629
|
|
|
640
630
|
async offlineOff(): Promise<void> {
|
|
641
631
|
if (this.device.offline) {
|
|
642
|
-
await this.context();
|
|
632
|
+
await this.context(this.accessory, this.device);
|
|
643
633
|
await this.updateHomeKitCharacteristics();
|
|
644
634
|
}
|
|
645
635
|
}
|
|
@@ -662,16 +652,11 @@ export class MeterPlus {
|
|
|
662
652
|
accessory.context.FirmwareRevision = device.firmware;
|
|
663
653
|
this.FirmwareRevision = accessory.context.FirmwareRevision;
|
|
664
654
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware, FirmwareRevision: ${this.FirmwareRevision}`);
|
|
665
|
-
} else
|
|
655
|
+
} else {
|
|
666
656
|
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version: ${device.version}`);
|
|
667
657
|
accessory.context.FirmwareRevision = device.version;
|
|
668
658
|
this.FirmwareRevision = accessory.context.FirmwareRevision;
|
|
669
659
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} device.version, FirmwareRevision: ${this.FirmwareRevision}`);
|
|
670
|
-
} else {
|
|
671
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
672
|
-
accessory.context.FirmwareRevision = this.platform.version;
|
|
673
|
-
this.FirmwareRevision = accessory.context.FirmwareRevision;
|
|
674
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version, FirmwareRevision: ${this.FirmwareRevision}`);
|
|
675
660
|
}
|
|
676
661
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} setFirmwareRevision: ${this.FirmwareRevision}`);
|
|
677
662
|
accessory
|
|
@@ -681,7 +666,7 @@ export class MeterPlus {
|
|
|
681
666
|
.updateValue(this.FirmwareRevision);
|
|
682
667
|
}
|
|
683
668
|
|
|
684
|
-
async context() {
|
|
669
|
+
async context(accessory, device) {
|
|
685
670
|
if (this.CurrentRelativeHumidity === undefined) {
|
|
686
671
|
this.CurrentRelativeHumidity = 0;
|
|
687
672
|
} else {
|
|
@@ -703,6 +688,12 @@ export class MeterPlus {
|
|
|
703
688
|
} else {
|
|
704
689
|
this.StatusLowBattery = this.accessory.context.StatusLowBattery;
|
|
705
690
|
}
|
|
691
|
+
if (this.FirmwareRevision === undefined) {
|
|
692
|
+
this.FirmwareRevision = this.platform.version;
|
|
693
|
+
} else {
|
|
694
|
+
this.setFirmwareRevision(accessory, device);
|
|
695
|
+
this.FirmwareRevision = this.accessory.context.FirmwareRevision;
|
|
696
|
+
}
|
|
706
697
|
}
|
|
707
698
|
|
|
708
699
|
async refreshRate(device: device & devicesConfig): Promise<void> {
|