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