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