@switchbot/homebridge-switchbot 3.5.0-beta.5 → 3.5.0-beta.51
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 +2 -0
- package/config.schema.json +120 -17
- package/dist/device/blindtilt.d.ts +21 -94
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +352 -757
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +25 -71
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +588 -761
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +13 -67
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +214 -588
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +13 -74
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +256 -634
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +14 -65
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +209 -532
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +17 -86
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +340 -787
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +59 -0
- package/dist/device/device.d.ts.map +1 -0
- package/dist/device/device.js +682 -0
- package/dist/device/device.js.map +1 -0
- package/dist/device/fan.d.ts +51 -0
- package/dist/device/fan.d.ts.map +1 -0
- package/dist/device/fan.js +505 -0
- package/dist/device/fan.js.map +1 -0
- package/dist/device/hub.d.ts +15 -59
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +247 -597
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +10 -65
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +218 -579
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +11 -64
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +179 -585
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +10 -68
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +257 -569
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +15 -62
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +215 -543
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +13 -63
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +175 -578
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +13 -64
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +164 -578
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +13 -69
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +142 -520
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +9 -57
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +87 -453
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +11 -62
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +145 -452
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +9 -70
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +126 -665
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/homebridge-ui/public/index.html +2 -2
- package/dist/index.js +1 -1
- package/dist/irdevice/airconditioner.d.ts +8 -48
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +309 -419
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +7 -38
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +72 -304
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +6 -35
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +34 -278
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +6 -39
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +67 -311
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +40 -0
- package/dist/irdevice/irdevice.d.ts.map +1 -0
- package/dist/irdevice/irdevice.js +512 -0
- package/dist/irdevice/irdevice.js.map +1 -0
- package/dist/irdevice/light.d.ts +6 -34
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +23 -272
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +18 -47
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +352 -546
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +7 -39
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +82 -321
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +6 -35
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +41 -284
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +6 -35
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +42 -287
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +14 -1
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +291 -147
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +41 -11
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +0 -552
- package/dist/settings.js.map +1 -1
- package/dist/utils.d.ts +77 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +638 -1
- package/dist/utils.js.map +1 -1
- package/eslint.config.js +87 -0
- package/package.json +16 -15
- package/src/custom.d.ts +2 -1
- package/src/device/blindtilt.ts +378 -783
- package/src/device/bot.ts +621 -845
- package/src/device/ceilinglight.ts +227 -608
- package/src/device/colorbulb.ts +269 -657
- package/src/device/contact.ts +235 -555
- package/src/device/curtain.ts +360 -811
- package/src/device/device.ts +710 -0
- package/src/device/fan.ts +554 -0
- package/src/device/hub.ts +274 -625
- package/src/device/humidifier.ts +246 -624
- package/src/device/iosensor.ts +205 -611
- package/src/device/lightstrip.ts +278 -597
- package/src/device/lock.ts +252 -583
- package/src/device/meter.ts +200 -601
- package/src/device/meterplus.ts +189 -603
- package/src/device/motion.ts +161 -542
- package/src/device/plug.ts +97 -481
- package/src/device/robotvacuumcleaner.ts +166 -478
- package/src/device/waterdetector.ts +143 -689
- package/src/homebridge-ui/public/index.html +2 -2
- package/src/index.ts +1 -1
- package/src/irdevice/airconditioner.ts +330 -446
- package/src/irdevice/airpurifier.ts +89 -321
- package/src/irdevice/camera.ts +42 -293
- package/src/irdevice/fan.ts +78 -327
- package/src/irdevice/irdevice.ts +528 -0
- package/src/irdevice/light.ts +32 -287
- package/src/irdevice/other.ts +396 -615
- package/src/irdevice/tv.ts +100 -339
- package/src/irdevice/vacuumcleaner.ts +49 -305
- package/src/irdevice/waterheater.ts +50 -310
- package/src/platform.ts +301 -155
- package/src/settings.ts +57 -566
- package/src/utils.ts +642 -1
package/dist/device/meter.js
CHANGED
|
@@ -1,97 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
|
|
2
|
+
*
|
|
3
|
+
* meter.ts: @switchbot/homebridge-switchbot.
|
|
4
|
+
*/
|
|
5
|
+
import { deviceBase } from './device.js';
|
|
6
|
+
import { convertUnits } from '../utils.js';
|
|
7
|
+
import { Subject, interval, skipWhile } from 'rxjs';
|
|
5
8
|
import { Devices } from '../settings.js';
|
|
6
|
-
|
|
7
|
-
export class Meter {
|
|
9
|
+
export class Meter extends deviceBase {
|
|
8
10
|
platform;
|
|
9
|
-
accessory;
|
|
10
|
-
device;
|
|
11
|
-
api;
|
|
12
|
-
log;
|
|
13
|
-
config;
|
|
14
|
-
hap;
|
|
15
11
|
// Services
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
StatusLowBattery;
|
|
23
|
-
CurrentTemperature;
|
|
24
|
-
CurrentRelativeHumidity;
|
|
25
|
-
// OpenAPI Status
|
|
26
|
-
OpenAPI_BatteryLevel;
|
|
27
|
-
OpenAPI_FirmwareRevision;
|
|
28
|
-
OpenAPI_CurrentTemperature;
|
|
29
|
-
OpenAPI_CurrentRelativeHumidity;
|
|
30
|
-
// BLE Status
|
|
31
|
-
BLE_Celsius;
|
|
32
|
-
BLE_Fahrenheit;
|
|
33
|
-
BLE_BatteryLevel;
|
|
34
|
-
BLE_CurrentTemperature;
|
|
35
|
-
BLE_CurrentRelativeHumidity;
|
|
36
|
-
// BLE Others
|
|
37
|
-
BLE_IsConnected;
|
|
38
|
-
//MQTT stuff
|
|
39
|
-
mqttClient = null;
|
|
40
|
-
// EVE history service handler
|
|
41
|
-
historyService;
|
|
42
|
-
// Config
|
|
43
|
-
scanDuration;
|
|
44
|
-
deviceLogging;
|
|
45
|
-
deviceRefreshRate;
|
|
46
|
-
// Connection
|
|
47
|
-
OpenAPI;
|
|
48
|
-
BLE;
|
|
12
|
+
Battery;
|
|
13
|
+
HumiditySensor;
|
|
14
|
+
TemperatureSensor;
|
|
15
|
+
// Updates
|
|
16
|
+
meterUpdateInProgress;
|
|
17
|
+
doMeterUpdate;
|
|
49
18
|
constructor(platform, accessory, device) {
|
|
19
|
+
super(platform, accessory, device);
|
|
50
20
|
this.platform = platform;
|
|
51
|
-
this
|
|
52
|
-
this.
|
|
53
|
-
this.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
this.
|
|
21
|
+
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
22
|
+
this.doMeterUpdate = new Subject();
|
|
23
|
+
this.meterUpdateInProgress = false;
|
|
24
|
+
// Initialize Temperature Sensor property
|
|
25
|
+
if (!device.meter?.hide_temperature) {
|
|
26
|
+
this.TemperatureSensor = {
|
|
27
|
+
Service: accessory.getService(this.hap.Service.TemperatureSensor),
|
|
28
|
+
CurrentTemperature: accessory.context.CurrentTemperature || 30,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
// Initialize Humidity Sensor property
|
|
32
|
+
if (!device.meter?.hide_humidity) {
|
|
33
|
+
this.HumiditySensor = {
|
|
34
|
+
Service: accessory.getService(this.hap.Service.HumiditySensor),
|
|
35
|
+
CurrentRelativeHumidity: accessory.context.CurrentRelativeHumidity || 50,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// Initialize Battery property
|
|
39
|
+
this.Battery = {
|
|
40
|
+
Service: accessory.getService(this.hap.Service.Battery),
|
|
41
|
+
BatteryLevel: accessory.context.BatteryLevel || 100,
|
|
42
|
+
StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
|
|
43
|
+
};
|
|
70
44
|
// Retrieve initial values and updateHomekit
|
|
71
45
|
this.refreshStatus();
|
|
72
|
-
// set accessory information
|
|
73
|
-
accessory
|
|
74
|
-
.getService(this.hap.Service.AccessoryInformation)
|
|
75
|
-
.setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
|
|
76
|
-
.setCharacteristic(this.hap.Characteristic.Model, 'METERTH-S1')
|
|
77
|
-
.setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
|
|
78
|
-
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
|
|
79
46
|
// Temperature Sensor Service
|
|
80
47
|
if (device.meter?.hide_temperature) {
|
|
81
48
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
|
|
82
|
-
this.
|
|
83
|
-
accessory.removeService(this.
|
|
49
|
+
this.TemperatureSensor.Service = this.accessory.getService(this.hap.Service.TemperatureSensor);
|
|
50
|
+
accessory.removeService(this.TemperatureSensor.Service);
|
|
84
51
|
}
|
|
85
|
-
else if (!this.
|
|
52
|
+
else if (!this.TemperatureSensor?.Service) {
|
|
86
53
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
|
|
87
|
-
const
|
|
88
|
-
(this.
|
|
89
|
-
|| this.accessory.addService(this.hap.Service.TemperatureSensor)),
|
|
90
|
-
this.
|
|
91
|
-
|
|
92
|
-
this.temperatureService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
|
|
93
|
-
}
|
|
94
|
-
this.temperatureService
|
|
54
|
+
const TemperatureSensorService = `${accessory.displayName} Temperature Sensor`;
|
|
55
|
+
(this.TemperatureSensor.Service = this.accessory.getService(this.hap.Service.TemperatureSensor)
|
|
56
|
+
|| this.accessory.addService(this.hap.Service.TemperatureSensor)), TemperatureSensorService;
|
|
57
|
+
this.TemperatureSensor.Service.setCharacteristic(this.hap.Characteristic.Name, TemperatureSensorService);
|
|
58
|
+
this.TemperatureSensor.Service
|
|
95
59
|
.getCharacteristic(this.hap.Characteristic.CurrentTemperature)
|
|
96
60
|
.setProps({
|
|
97
61
|
unit: "celsius" /* Units['CELSIUS'] */,
|
|
@@ -101,7 +65,7 @@ export class Meter {
|
|
|
101
65
|
minStep: 0.1,
|
|
102
66
|
})
|
|
103
67
|
.onGet(() => {
|
|
104
|
-
return this.CurrentTemperature;
|
|
68
|
+
return this.TemperatureSensor.CurrentTemperature;
|
|
105
69
|
});
|
|
106
70
|
}
|
|
107
71
|
else {
|
|
@@ -110,63 +74,65 @@ export class Meter {
|
|
|
110
74
|
// Humidity Sensor Service
|
|
111
75
|
if (device.meter?.hide_humidity) {
|
|
112
76
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
|
|
113
|
-
this.
|
|
114
|
-
accessory.removeService(this.
|
|
77
|
+
this.HumiditySensor.Service = this.accessory.getService(this.hap.Service.HumiditySensor);
|
|
78
|
+
accessory.removeService(this.HumiditySensor.Service);
|
|
115
79
|
}
|
|
116
|
-
else if (!this.
|
|
80
|
+
else if (!this.HumiditySensor?.Service) {
|
|
117
81
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
|
|
118
|
-
const
|
|
119
|
-
(this.
|
|
120
|
-
|| this.accessory.addService(this.hap.Service.HumiditySensor)),
|
|
121
|
-
this.
|
|
122
|
-
|
|
123
|
-
this.humidityService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
|
|
124
|
-
}
|
|
125
|
-
this.humidityService
|
|
82
|
+
const HumiditySensorService = `${accessory.displayName} Humidity Sensor`;
|
|
83
|
+
(this.HumiditySensor.Service = this.accessory.getService(this.hap.Service.HumiditySensor)
|
|
84
|
+
|| this.accessory.addService(this.hap.Service.HumiditySensor)), HumiditySensorService;
|
|
85
|
+
this.HumiditySensor.Service.setCharacteristic(this.hap.Characteristic.Name, HumiditySensorService);
|
|
86
|
+
this.HumiditySensor.Service
|
|
126
87
|
.getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
|
|
127
88
|
.setProps({
|
|
128
89
|
minStep: 0.1,
|
|
129
90
|
})
|
|
130
91
|
.onGet(() => {
|
|
131
|
-
return this.CurrentRelativeHumidity;
|
|
92
|
+
return this.HumiditySensor.CurrentRelativeHumidity;
|
|
132
93
|
});
|
|
133
94
|
}
|
|
134
95
|
else {
|
|
135
96
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Humidity Sensor Service Not Added`);
|
|
136
97
|
}
|
|
137
98
|
// Battery Service
|
|
138
|
-
const
|
|
139
|
-
(this.
|
|
140
|
-
|| accessory.addService(this.hap.Service.Battery)),
|
|
141
|
-
this.
|
|
142
|
-
|
|
143
|
-
this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
|
|
144
|
-
}
|
|
145
|
-
this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
|
|
99
|
+
const BatteryService = `${accessory.displayName} Battery`;
|
|
100
|
+
(this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
|
|
101
|
+
|| accessory.addService(this.hap.Service.Battery)), BatteryService;
|
|
102
|
+
this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
|
|
103
|
+
this.Battery.Service.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
|
|
146
104
|
// Retrieve initial values and update Homekit
|
|
147
105
|
this.updateHomeKitCharacteristics();
|
|
148
106
|
// Start an update interval
|
|
149
107
|
interval(this.deviceRefreshRate * 1000)
|
|
108
|
+
.pipe(skipWhile(() => this.meterUpdateInProgress))
|
|
150
109
|
.subscribe(async () => {
|
|
151
110
|
await this.refreshStatus();
|
|
152
111
|
});
|
|
153
112
|
//regisiter webhook event handler
|
|
154
|
-
if (
|
|
155
|
-
this.
|
|
113
|
+
if (device.webhook) {
|
|
114
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
|
|
156
115
|
this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
|
|
157
116
|
try {
|
|
158
117
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
118
|
+
const { temperature, humidity } = context;
|
|
119
|
+
const { CurrentTemperature } = this.TemperatureSensor || { CurrentTemperature: undefined };
|
|
120
|
+
const { CurrentRelativeHumidity } = this.HumiditySensor || { CurrentRelativeHumidity: undefined };
|
|
121
|
+
if (context.scale !== 'CELCIUS' && device.meter?.convertUnitTo === undefined) {
|
|
122
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook scale: `
|
|
123
|
+
+ `${context.scale}, instead of CELCIUS. Use the *convertUnitsTo* config under Meter settings, if displaying incorrectly in HomeKit.`);
|
|
124
|
+
}
|
|
125
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
|
|
126
|
+
'(scale, temperature, humidity) = '
|
|
127
|
+
+ `Webhook:(${context.scale}, ${convertUnits(temperature, context.scale, device.meter?.convertUnitTo)}, ${humidity}), `
|
|
128
|
+
+ `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
|
|
129
|
+
if (this.device.meter?.hide_humidity) {
|
|
130
|
+
this.HumiditySensor.CurrentRelativeHumidity = humidity;
|
|
131
|
+
}
|
|
132
|
+
if (this.device.meter?.hide_temperature) {
|
|
133
|
+
this.TemperatureSensor.CurrentTemperature = convertUnits(temperature, context.scale, device.meter?.convertUnitTo);
|
|
169
134
|
}
|
|
135
|
+
this.updateHomeKitCharacteristics();
|
|
170
136
|
}
|
|
171
137
|
catch (e) {
|
|
172
138
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
@@ -175,84 +141,65 @@ export class Meter {
|
|
|
175
141
|
};
|
|
176
142
|
}
|
|
177
143
|
}
|
|
178
|
-
|
|
179
|
-
* Parse the device status from the SwitchBot api
|
|
180
|
-
*/
|
|
181
|
-
async parseStatus() {
|
|
182
|
-
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
183
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
184
|
-
}
|
|
185
|
-
else if (this.BLE) {
|
|
186
|
-
await this.BLEparseStatus();
|
|
187
|
-
}
|
|
188
|
-
else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
189
|
-
await this.openAPIparseStatus();
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
await this.offlineOff();
|
|
193
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
async BLEparseStatus() {
|
|
144
|
+
async BLEparseStatus(serviceData) {
|
|
197
145
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
198
146
|
// BatteryLevel
|
|
199
|
-
this.BatteryLevel = Number(
|
|
200
|
-
if (this.BatteryLevel < 15) {
|
|
201
|
-
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
147
|
+
this.Battery.BatteryLevel = Number(serviceData.battery);
|
|
148
|
+
if (this.Battery.BatteryLevel < 15) {
|
|
149
|
+
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
202
150
|
}
|
|
203
151
|
else {
|
|
204
|
-
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
152
|
+
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
205
153
|
}
|
|
206
|
-
this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
|
|
154
|
+
this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
207
155
|
// CurrentRelativeHumidity
|
|
208
156
|
if (!this.device.meter?.hide_humidity) {
|
|
209
|
-
this.CurrentRelativeHumidity =
|
|
210
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
|
|
157
|
+
this.HumiditySensor.CurrentRelativeHumidity = serviceData.humidity;
|
|
158
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
|
|
211
159
|
}
|
|
212
160
|
// CurrentTemperature
|
|
213
161
|
if (!this.device.meter?.hide_temperature) {
|
|
214
|
-
|
|
215
|
-
this.CurrentTemperature =
|
|
216
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
|
|
162
|
+
serviceData.temperature.c < 0 ? 0 : serviceData.temperature.c > 100 ? 100 : serviceData.temperature.c;
|
|
163
|
+
this.TemperatureSensor.CurrentTemperature = serviceData.temperature.c;
|
|
164
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
|
|
217
165
|
}
|
|
218
166
|
}
|
|
219
|
-
async openAPIparseStatus() {
|
|
167
|
+
async openAPIparseStatus(deviceStatus) {
|
|
220
168
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
221
|
-
//
|
|
222
|
-
this.BatteryLevel = Number(
|
|
223
|
-
if (this.BatteryLevel <
|
|
224
|
-
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
169
|
+
// Battery
|
|
170
|
+
this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
|
|
171
|
+
if (this.Battery.BatteryLevel < 10) {
|
|
172
|
+
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
225
173
|
}
|
|
226
174
|
else {
|
|
227
|
-
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
175
|
+
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
228
176
|
}
|
|
229
|
-
|
|
177
|
+
if (Number.isNaN(this.Battery.BatteryLevel)) {
|
|
178
|
+
this.Battery.BatteryLevel = 100;
|
|
179
|
+
}
|
|
180
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
|
|
181
|
+
+ ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
230
182
|
// CurrentRelativeHumidity
|
|
231
183
|
if (!this.device.meter?.hide_humidity) {
|
|
232
|
-
this.CurrentRelativeHumidity =
|
|
233
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
|
|
184
|
+
this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.body.humidity;
|
|
185
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
|
|
234
186
|
}
|
|
235
187
|
// CurrentTemperature
|
|
236
188
|
if (!this.device.meter?.hide_temperature) {
|
|
237
|
-
this.CurrentTemperature =
|
|
238
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
|
|
189
|
+
this.TemperatureSensor.CurrentTemperature = deviceStatus.body.temperature;
|
|
190
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
|
|
191
|
+
}
|
|
192
|
+
// Firmware Version
|
|
193
|
+
const version = deviceStatus.body.version?.toString();
|
|
194
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
|
|
195
|
+
if (deviceStatus.body.version) {
|
|
196
|
+
this.accessory.context.version = version?.replace(/^V|-.*$/g, '');
|
|
197
|
+
this.accessory
|
|
198
|
+
.getService(this.hap.Service.AccessoryInformation)
|
|
199
|
+
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, this.accessory.context.version)
|
|
200
|
+
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
201
|
+
.updateValue(this.accessory.context.version);
|
|
239
202
|
}
|
|
240
|
-
// Battery
|
|
241
|
-
this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
|
|
242
|
-
if (this.BatteryLevel < 10) {
|
|
243
|
-
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
244
|
-
}
|
|
245
|
-
else {
|
|
246
|
-
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
247
|
-
}
|
|
248
|
-
if (Number.isNaN(this.BatteryLevel)) {
|
|
249
|
-
this.BatteryLevel = 100;
|
|
250
|
-
}
|
|
251
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
|
|
252
|
-
+ ` StatusLowBattery: ${this.StatusLowBattery}`);
|
|
253
|
-
// FirmwareRevision
|
|
254
|
-
this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
|
|
255
|
-
this.accessory.context.FirmwareRevision = this.FirmwareRevision;
|
|
256
203
|
}
|
|
257
204
|
/**
|
|
258
205
|
* Asks the SwitchBot API for the latest device information
|
|
@@ -285,108 +232,43 @@ export class Meter {
|
|
|
285
232
|
this.getCustomBLEAddress(switchbot);
|
|
286
233
|
// Start to monitor advertisement packets
|
|
287
234
|
(async () => {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
id: this.device.bleMac,
|
|
291
|
-
});
|
|
235
|
+
// Start to monitor advertisement packets
|
|
236
|
+
await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
|
|
292
237
|
// Set an event handler
|
|
293
238
|
switchbot.onadvertisement = (ad) => {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
239
|
+
if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
|
|
240
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
|
|
241
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
297
242
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
298
|
-
this.BLE_CurrentRelativeHumidity = ad.serviceData.humidity < 0 ? 0 : ad.serviceData.humidity > 100 ? 100 : ad.serviceData.humidity;
|
|
299
|
-
this.BLE_CurrentTemperature = ad.serviceData.temperature;
|
|
300
|
-
this.BLE_Celsius = ad.serviceData.temperature.c;
|
|
301
|
-
this.BLE_Fahrenheit = ad.serviceData.temperature.f;
|
|
302
|
-
this.BLE_BatteryLevel = ad.serviceData.battery;
|
|
303
243
|
}
|
|
304
244
|
else {
|
|
305
245
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
306
246
|
}
|
|
307
247
|
};
|
|
308
|
-
// Wait
|
|
248
|
+
// Wait 10 seconds
|
|
309
249
|
await switchbot.wait(this.scanDuration * 1000);
|
|
310
250
|
// Stop to monitor
|
|
311
251
|
await switchbot.stopScan();
|
|
312
252
|
// Update HomeKit
|
|
313
|
-
await this.BLEparseStatus();
|
|
253
|
+
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
314
254
|
await this.updateHomeKitCharacteristics();
|
|
315
255
|
})();
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
model: 'T',
|
|
320
|
-
id: this.device.bleMac,
|
|
321
|
-
})
|
|
322
|
-
.then(async () => {
|
|
323
|
-
// Set an event handler
|
|
324
|
-
switchbot.onadvertisement = async (ad: ad) => {
|
|
325
|
-
this.debugLog(
|
|
326
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
|
|
327
|
-
` BLE Address Found: ${ad.address}`,
|
|
328
|
-
);
|
|
329
|
-
if (ad.serviceData.humidity! > 0) {
|
|
330
|
-
// reject unreliable data
|
|
331
|
-
this.BLE_CurrentRelativeHumidity = ad.serviceData.humidity;
|
|
332
|
-
}
|
|
333
|
-
this.BLE_CurrentTemperature = ad.serviceData.temperature;
|
|
334
|
-
this.BLE_Celsius = ad.serviceData.temperature!.c;
|
|
335
|
-
this.BLE_Fahrenheit = ad.serviceData.temperature!.f;
|
|
336
|
-
this.BLE_BatteryLevel = ad.serviceData.battery;
|
|
337
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
338
|
-
this.debugLog(
|
|
339
|
-
`${this.device.deviceType}: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, ` +
|
|
340
|
-
`temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, ` +
|
|
341
|
-
`battery: ${ad.serviceData.battery}`,
|
|
342
|
-
);
|
|
343
|
-
|
|
344
|
-
if (ad.serviceData) {
|
|
345
|
-
this.BLE_IsConnected = true;
|
|
346
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
|
|
347
|
-
await this.stopScanning(switchbot);
|
|
348
|
-
} else {
|
|
349
|
-
this.BLE_IsConnected = false;
|
|
350
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
|
|
351
|
-
}
|
|
352
|
-
};
|
|
353
|
-
// Wait
|
|
354
|
-
return await sleep(this.scanDuration * 1000);
|
|
355
|
-
})
|
|
356
|
-
.then(async () => {
|
|
357
|
-
// Stop to monitor
|
|
358
|
-
await this.stopScanning(switchbot);
|
|
359
|
-
})
|
|
360
|
-
.catch(async (e: any) => {
|
|
361
|
-
this.apiError(e);
|
|
362
|
-
this.errorLog(
|
|
363
|
-
`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
|
|
364
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
365
|
-
);
|
|
366
|
-
await this.BLERefreshConnection(switchbot);
|
|
367
|
-
});
|
|
368
|
-
} else {
|
|
369
|
-
await this.BLERefreshConnection(switchbot);
|
|
370
|
-
}*/
|
|
256
|
+
if (switchbot === undefined) {
|
|
257
|
+
await this.BLERefreshConnection(switchbot);
|
|
258
|
+
}
|
|
371
259
|
}
|
|
372
260
|
async openAPIRefreshStatus() {
|
|
373
261
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
374
262
|
try {
|
|
375
|
-
const { body, statusCode } = await
|
|
376
|
-
headers: this.platform.generateHeaders(),
|
|
377
|
-
});
|
|
263
|
+
const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
378
264
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
379
265
|
const deviceStatus = await body.json();
|
|
380
266
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
381
267
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
382
268
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
383
|
-
this.
|
|
269
|
+
this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
384
270
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
385
|
-
this.
|
|
386
|
-
this.OpenAPI_CurrentTemperature = deviceStatus.body.temperature;
|
|
387
|
-
this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
|
|
388
|
-
this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
|
|
389
|
-
this.openAPIparseStatus();
|
|
271
|
+
this.openAPIparseStatus(deviceStatus);
|
|
390
272
|
this.updateHomeKitCharacteristics();
|
|
391
273
|
}
|
|
392
274
|
else {
|
|
@@ -408,148 +290,77 @@ export class Meter {
|
|
|
408
290
|
const entry = { time: Math.round(new Date().valueOf() / 1000) };
|
|
409
291
|
// CurrentRelativeHumidity
|
|
410
292
|
if (!this.device.meter?.hide_humidity) {
|
|
411
|
-
if (this.CurrentRelativeHumidity === undefined) {
|
|
412
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
293
|
+
if (this.HumiditySensor.CurrentRelativeHumidity === undefined) {
|
|
294
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
295
|
+
+ ` CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`);
|
|
413
296
|
}
|
|
414
297
|
else {
|
|
298
|
+
this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor.CurrentRelativeHumidity;
|
|
299
|
+
this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.HumiditySensor.CurrentRelativeHumidity);
|
|
300
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
301
|
+
+ ` updateCharacteristic CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`);
|
|
415
302
|
if (this.device.mqttURL) {
|
|
416
|
-
mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
|
|
303
|
+
mqttmessage.push(`"humidity": ${this.HumiditySensor.CurrentRelativeHumidity}`);
|
|
417
304
|
}
|
|
418
305
|
if (this.device.history) {
|
|
419
|
-
entry['humidity'] = this.CurrentRelativeHumidity;
|
|
306
|
+
entry['humidity'] = this.HumiditySensor.CurrentRelativeHumidity;
|
|
420
307
|
}
|
|
421
|
-
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
|
|
422
|
-
this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
|
|
423
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
424
|
-
+ `updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
|
|
425
308
|
}
|
|
426
309
|
}
|
|
427
310
|
// CurrentTemperature
|
|
428
311
|
if (!this.device.meter?.hide_temperature) {
|
|
429
|
-
if (this.CurrentTemperature === undefined) {
|
|
430
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
|
|
312
|
+
if (this.TemperatureSensor.CurrentTemperature === undefined) {
|
|
313
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
|
|
431
314
|
}
|
|
432
315
|
else {
|
|
316
|
+
this.accessory.context.CurrentTemperature = this.TemperatureSensor.CurrentTemperature;
|
|
317
|
+
this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor.CurrentTemperature);
|
|
318
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
319
|
+
+ ` CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
|
|
433
320
|
if (this.device.mqttURL) {
|
|
434
|
-
mqttmessage.push(`"temperature": ${this.CurrentTemperature}`);
|
|
321
|
+
mqttmessage.push(`"temperature": ${this.TemperatureSensor.CurrentTemperature}`);
|
|
435
322
|
}
|
|
436
323
|
if (this.device.history) {
|
|
437
|
-
entry['temp'] = this.CurrentTemperature;
|
|
324
|
+
entry['temp'] = this.TemperatureSensor.CurrentTemperature;
|
|
438
325
|
}
|
|
439
|
-
this.accessory.context.CurrentTemperature = this.CurrentTemperature;
|
|
440
|
-
this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
|
|
441
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
|
|
442
326
|
}
|
|
443
327
|
}
|
|
444
328
|
// BatteryLevel
|
|
445
|
-
if (this.BatteryLevel === undefined) {
|
|
446
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
329
|
+
if (this.Battery.BatteryLevel === undefined) {
|
|
330
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
|
|
447
331
|
}
|
|
448
332
|
else {
|
|
333
|
+
this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
|
|
334
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
|
|
335
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
|
|
449
336
|
if (this.device.mqttURL) {
|
|
450
|
-
mqttmessage.push(`"battery": ${this.BatteryLevel}`);
|
|
337
|
+
mqttmessage.push(`"battery": ${this.Battery.BatteryLevel}`);
|
|
451
338
|
}
|
|
452
|
-
this.accessory.context.BatteryLevel = this.BatteryLevel;
|
|
453
|
-
this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
|
|
454
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
|
|
455
339
|
}
|
|
456
|
-
if (this.StatusLowBattery === undefined) {
|
|
457
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
|
|
340
|
+
if (this.Battery.StatusLowBattery === undefined) {
|
|
341
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
458
342
|
}
|
|
459
343
|
else {
|
|
344
|
+
this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
|
|
345
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
|
|
346
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
347
|
+
+ ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
460
348
|
if (this.device.mqttURL) {
|
|
461
|
-
mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
|
|
349
|
+
mqttmessage.push(`"lowBattery": ${this.Battery.StatusLowBattery}`);
|
|
462
350
|
}
|
|
463
|
-
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
|
|
464
|
-
this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
|
|
465
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
|
|
466
351
|
}
|
|
467
|
-
// MQTT
|
|
352
|
+
// MQTT Publish
|
|
468
353
|
if (this.device.mqttURL) {
|
|
469
354
|
this.mqttPublish(`{${mqttmessage.join(',')}}`);
|
|
470
355
|
}
|
|
471
|
-
|
|
356
|
+
// History Service
|
|
357
|
+
if (!this.device.meter?.hide_humidity && (Number(this.HumiditySensor.CurrentRelativeHumidity) > 0)) {
|
|
472
358
|
// reject unreliable data
|
|
473
359
|
if (this.device.history) {
|
|
474
360
|
this.historyService?.addEntry(entry);
|
|
475
361
|
}
|
|
476
362
|
}
|
|
477
363
|
}
|
|
478
|
-
/*
|
|
479
|
-
* Publish MQTT message for topics of
|
|
480
|
-
* 'homebridge-switchbot/meter/xx:xx:xx:xx:xx:xx'
|
|
481
|
-
*/
|
|
482
|
-
mqttPublish(message) {
|
|
483
|
-
const mac = this.device.deviceId
|
|
484
|
-
?.toLowerCase()
|
|
485
|
-
.match(/[\s\S]{1,2}/g)
|
|
486
|
-
?.join(':');
|
|
487
|
-
const options = this.device.mqttPubOptions || {};
|
|
488
|
-
this.mqttClient?.publish(`homebridge-switchbot/meter/${mac}`, `${message}`, options);
|
|
489
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${message} options:${JSON.stringify(options)}`);
|
|
490
|
-
}
|
|
491
|
-
/*
|
|
492
|
-
* Setup MQTT hadler if URL is specified.
|
|
493
|
-
*/
|
|
494
|
-
async setupMqtt(device) {
|
|
495
|
-
if (device.mqttURL) {
|
|
496
|
-
try {
|
|
497
|
-
const { connectAsync } = asyncmqtt;
|
|
498
|
-
this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
|
|
499
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
|
|
500
|
-
this.mqttClient.on('error', (e) => {
|
|
501
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
catch (e) {
|
|
505
|
-
this.mqttClient = null;
|
|
506
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
/*
|
|
511
|
-
* Setup EVE history graph feature if enabled.
|
|
512
|
-
*/
|
|
513
|
-
async setupHistoryService(device) {
|
|
514
|
-
const mac = this.device
|
|
515
|
-
.deviceId.match(/.{1,2}/g)
|
|
516
|
-
.join(':')
|
|
517
|
-
.toLowerCase();
|
|
518
|
-
this.historyService = device.history
|
|
519
|
-
? new this.platform.fakegatoAPI('room', this.accessory, {
|
|
520
|
-
log: this.platform.log,
|
|
521
|
-
storage: 'fs',
|
|
522
|
-
filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
|
|
523
|
-
})
|
|
524
|
-
: null;
|
|
525
|
-
}
|
|
526
|
-
async stopScanning(switchbot) {
|
|
527
|
-
switchbot.stopScan();
|
|
528
|
-
if (this.BLE_IsConnected) {
|
|
529
|
-
await this.BLEparseStatus();
|
|
530
|
-
await this.updateHomeKitCharacteristics();
|
|
531
|
-
}
|
|
532
|
-
else {
|
|
533
|
-
await this.BLERefreshConnection(switchbot);
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
async getCustomBLEAddress(switchbot) {
|
|
537
|
-
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
538
|
-
(async () => {
|
|
539
|
-
// Start to monitor advertisement packets
|
|
540
|
-
await switchbot.startScan({
|
|
541
|
-
model: 'T',
|
|
542
|
-
});
|
|
543
|
-
// Set an event handler
|
|
544
|
-
switchbot.onadvertisement = (ad) => {
|
|
545
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
|
|
546
|
-
};
|
|
547
|
-
await sleep(10000);
|
|
548
|
-
// Stop to monitor
|
|
549
|
-
switchbot.stopScan();
|
|
550
|
-
})();
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
364
|
async BLERefreshConnection(switchbot) {
|
|
554
365
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
|
|
555
366
|
+ ` ${JSON.stringify(switchbot)}`);
|
|
@@ -558,240 +369,26 @@ export class Meter {
|
|
|
558
369
|
await this.openAPIRefreshStatus();
|
|
559
370
|
}
|
|
560
371
|
}
|
|
561
|
-
async scan(device) {
|
|
562
|
-
if (device.scanDuration) {
|
|
563
|
-
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
564
|
-
if (this.BLE) {
|
|
565
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
else {
|
|
569
|
-
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
570
|
-
if (this.BLE) {
|
|
571
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
async statusCode(statusCode) {
|
|
576
|
-
switch (statusCode) {
|
|
577
|
-
case 151:
|
|
578
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
|
|
579
|
-
break;
|
|
580
|
-
case 152:
|
|
581
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
|
|
582
|
-
break;
|
|
583
|
-
case 160:
|
|
584
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
|
|
585
|
-
break;
|
|
586
|
-
case 161:
|
|
587
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
|
|
588
|
-
this.offlineOff();
|
|
589
|
-
break;
|
|
590
|
-
case 171:
|
|
591
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
|
|
592
|
-
`Hub: ${this.device.hubDeviceId}`);
|
|
593
|
-
this.offlineOff();
|
|
594
|
-
break;
|
|
595
|
-
case 190:
|
|
596
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
597
|
-
` Or command format is invalid, statusCode: ${statusCode}`);
|
|
598
|
-
break;
|
|
599
|
-
case 100:
|
|
600
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
|
|
601
|
-
break;
|
|
602
|
-
case 200:
|
|
603
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
|
|
604
|
-
break;
|
|
605
|
-
case 400:
|
|
606
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
|
|
607
|
-
+ `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
|
|
608
|
-
break;
|
|
609
|
-
case 401:
|
|
610
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
|
|
611
|
-
+ `but the request has not been authenticated, statusCode: ${statusCode}`);
|
|
612
|
-
break;
|
|
613
|
-
case 403:
|
|
614
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
|
|
615
|
-
+ `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
|
|
616
|
-
break;
|
|
617
|
-
case 404:
|
|
618
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
|
|
619
|
-
+ `statusCode: ${statusCode}`);
|
|
620
|
-
break;
|
|
621
|
-
case 406:
|
|
622
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
|
|
623
|
-
+ `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
|
|
624
|
-
break;
|
|
625
|
-
case 415:
|
|
626
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
|
|
627
|
-
+ `header that is not supported by the server, statusCode: ${statusCode}`);
|
|
628
|
-
break;
|
|
629
|
-
case 422:
|
|
630
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
|
|
631
|
-
+ `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
|
|
632
|
-
break;
|
|
633
|
-
case 429:
|
|
634
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
|
|
635
|
-
+ `requests allowed for a given time window, statusCode: ${statusCode}`);
|
|
636
|
-
break;
|
|
637
|
-
case 500:
|
|
638
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
|
|
639
|
-
+ `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
|
|
640
|
-
break;
|
|
641
|
-
default:
|
|
642
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
|
|
643
|
-
`${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
372
|
async offlineOff() {
|
|
647
373
|
if (this.device.offline) {
|
|
648
|
-
|
|
649
|
-
|
|
374
|
+
if (!this.device.meter?.hide_humidity) {
|
|
375
|
+
this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, 50);
|
|
376
|
+
}
|
|
377
|
+
if (!this.device.meter?.hide_temperature) {
|
|
378
|
+
this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, 30);
|
|
379
|
+
}
|
|
380
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, 100);
|
|
650
381
|
}
|
|
651
382
|
}
|
|
652
383
|
async apiError(e) {
|
|
653
384
|
if (!this.device.meter?.hide_humidity) {
|
|
654
|
-
this.
|
|
385
|
+
this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
|
|
655
386
|
}
|
|
656
387
|
if (!this.device.meter?.hide_temperature) {
|
|
657
|
-
this.
|
|
658
|
-
}
|
|
659
|
-
this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
|
|
660
|
-
this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
|
|
661
|
-
}
|
|
662
|
-
async deviceContext() {
|
|
663
|
-
if (this.CurrentRelativeHumidity === undefined) {
|
|
664
|
-
this.CurrentRelativeHumidity = 0;
|
|
665
|
-
}
|
|
666
|
-
else {
|
|
667
|
-
this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
|
|
668
|
-
}
|
|
669
|
-
if (this.CurrentTemperature === undefined) {
|
|
670
|
-
this.CurrentTemperature = 0;
|
|
671
|
-
}
|
|
672
|
-
else {
|
|
673
|
-
this.CurrentTemperature = this.accessory.context.CurrentTemperature;
|
|
674
|
-
}
|
|
675
|
-
if (this.BatteryLevel === undefined) {
|
|
676
|
-
this.BatteryLevel = 100;
|
|
677
|
-
}
|
|
678
|
-
else {
|
|
679
|
-
this.BatteryLevel = this.accessory.context.BatteryLevel;
|
|
680
|
-
}
|
|
681
|
-
if (this.StatusLowBattery === undefined) {
|
|
682
|
-
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
683
|
-
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
|
|
684
|
-
}
|
|
685
|
-
else {
|
|
686
|
-
this.StatusLowBattery = this.accessory.context.StatusLowBattery;
|
|
687
|
-
}
|
|
688
|
-
if (this.FirmwareRevision === undefined) {
|
|
689
|
-
this.FirmwareRevision = this.platform.version;
|
|
690
|
-
this.accessory.context.FirmwareRevision = this.FirmwareRevision;
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
async refreshRate(device) {
|
|
694
|
-
if (device.refreshRate) {
|
|
695
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
|
|
696
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
697
|
-
}
|
|
698
|
-
else if (this.platform.config.options.refreshRate) {
|
|
699
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
|
|
700
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
async deviceConfig(device) {
|
|
704
|
-
let config = {};
|
|
705
|
-
if (device.meter) {
|
|
706
|
-
config = device.meter;
|
|
707
|
-
}
|
|
708
|
-
if (device.connectionType !== undefined) {
|
|
709
|
-
config['connectionType'] = device.connectionType;
|
|
710
|
-
}
|
|
711
|
-
if (device.external !== undefined) {
|
|
712
|
-
config['external'] = device.external;
|
|
713
|
-
}
|
|
714
|
-
if (device.mqttURL !== undefined) {
|
|
715
|
-
config['mqttURL'] = device.mqttURL;
|
|
716
|
-
}
|
|
717
|
-
if (device.logging !== undefined) {
|
|
718
|
-
config['logging'] = device.logging;
|
|
388
|
+
this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
|
|
719
389
|
}
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
}
|
|
723
|
-
if (device.scanDuration !== undefined) {
|
|
724
|
-
config['scanDuration'] = device.scanDuration;
|
|
725
|
-
}
|
|
726
|
-
if (device.webhook !== undefined) {
|
|
727
|
-
config['webhook'] = device.webhook;
|
|
728
|
-
}
|
|
729
|
-
if (Object.entries(config).length !== 0) {
|
|
730
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
async deviceLogs(device) {
|
|
734
|
-
if (this.platform.debugMode) {
|
|
735
|
-
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
736
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
|
|
737
|
-
}
|
|
738
|
-
else if (device.logging) {
|
|
739
|
-
this.deviceLogging = this.accessory.context.logging = device.logging;
|
|
740
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
|
|
741
|
-
}
|
|
742
|
-
else if (this.platform.config.options?.logging) {
|
|
743
|
-
this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
|
|
744
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
|
|
745
|
-
}
|
|
746
|
-
else {
|
|
747
|
-
this.deviceLogging = this.accessory.context.logging = 'standard';
|
|
748
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
/**
|
|
752
|
-
* Logging for Device
|
|
753
|
-
*/
|
|
754
|
-
infoLog(...log) {
|
|
755
|
-
if (this.enablingDeviceLogging()) {
|
|
756
|
-
this.platform.log.info(String(...log));
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
warnLog(...log) {
|
|
760
|
-
if (this.enablingDeviceLogging()) {
|
|
761
|
-
this.platform.log.warn(String(...log));
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
debugWarnLog(...log) {
|
|
765
|
-
if (this.enablingDeviceLogging()) {
|
|
766
|
-
if (this.deviceLogging?.includes('debug')) {
|
|
767
|
-
this.platform.log.warn('[DEBUG]', String(...log));
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
errorLog(...log) {
|
|
772
|
-
if (this.enablingDeviceLogging()) {
|
|
773
|
-
this.platform.log.error(String(...log));
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
debugErrorLog(...log) {
|
|
777
|
-
if (this.enablingDeviceLogging()) {
|
|
778
|
-
if (this.deviceLogging?.includes('debug')) {
|
|
779
|
-
this.platform.log.error('[DEBUG]', String(...log));
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
debugLog(...log) {
|
|
784
|
-
if (this.enablingDeviceLogging()) {
|
|
785
|
-
if (this.deviceLogging === 'debug') {
|
|
786
|
-
this.platform.log.info('[DEBUG]', String(...log));
|
|
787
|
-
}
|
|
788
|
-
else {
|
|
789
|
-
this.platform.log.debug(String(...log));
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
enablingDeviceLogging() {
|
|
794
|
-
return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
|
|
390
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
|
|
391
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
|
|
795
392
|
}
|
|
796
393
|
}
|
|
797
394
|
//# sourceMappingURL=meter.js.map
|