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