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