@switchbot/homebridge-switchbot 1.8.2 → 1.8.3-beta.0
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/.eslintrc +1 -1
- package/.vscode/extensions.json +5 -0
- package/.vscode/settings.json +15 -0
- package/dist/devices/bots.d.ts +34 -35
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +241 -184
- package/dist/devices/bots.js.map +1 -1
- package/dist/devices/colorbulb.d.ts +50 -50
- package/dist/devices/colorbulb.d.ts.map +1 -1
- package/dist/devices/colorbulb.js +145 -131
- package/dist/devices/colorbulb.js.map +1 -1
- package/dist/devices/contact.d.ts +27 -29
- package/dist/devices/contact.d.ts.map +1 -1
- package/dist/devices/contact.js +138 -115
- package/dist/devices/contact.js.map +1 -1
- package/dist/devices/curtains.d.ts +29 -31
- package/dist/devices/curtains.d.ts.map +1 -1
- package/dist/devices/curtains.js +211 -176
- package/dist/devices/curtains.js.map +1 -1
- package/dist/devices/humidifiers.d.ts +33 -33
- package/dist/devices/humidifiers.d.ts.map +1 -1
- package/dist/devices/humidifiers.js +214 -189
- package/dist/devices/humidifiers.js.map +1 -1
- package/dist/devices/meters.d.ts +30 -31
- package/dist/devices/meters.d.ts.map +1 -1
- package/dist/devices/meters.js +124 -100
- package/dist/devices/meters.js.map +1 -1
- package/dist/devices/motion.d.ts +25 -26
- package/dist/devices/motion.d.ts.map +1 -1
- package/dist/devices/motion.js +120 -102
- package/dist/devices/motion.js.map +1 -1
- package/dist/devices/plugs.d.ts +23 -23
- package/dist/devices/plugs.d.ts.map +1 -1
- package/dist/devices/plugs.js +81 -63
- package/dist/devices/plugs.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -3
- package/dist/index.js.map +1 -1
- package/dist/irdevices/airconditioners.d.ts +6 -6
- package/dist/irdevices/airconditioners.d.ts.map +1 -1
- package/dist/irdevices/airconditioners.js +105 -79
- package/dist/irdevices/airconditioners.js.map +1 -1
- package/dist/irdevices/airpurifiers.d.ts +5 -5
- package/dist/irdevices/airpurifiers.d.ts.map +1 -1
- package/dist/irdevices/airpurifiers.js +56 -42
- package/dist/irdevices/airpurifiers.js.map +1 -1
- package/dist/irdevices/cameras.d.ts +5 -5
- package/dist/irdevices/cameras.d.ts.map +1 -1
- package/dist/irdevices/cameras.js +31 -27
- package/dist/irdevices/cameras.js.map +1 -1
- package/dist/irdevices/fans.d.ts +5 -5
- package/dist/irdevices/fans.d.ts.map +1 -1
- package/dist/irdevices/fans.js +45 -38
- package/dist/irdevices/fans.js.map +1 -1
- package/dist/irdevices/lights.d.ts +5 -5
- package/dist/irdevices/lights.d.ts.map +1 -1
- package/dist/irdevices/lights.js +37 -33
- package/dist/irdevices/lights.js.map +1 -1
- package/dist/irdevices/others.d.ts +5 -5
- package/dist/irdevices/others.d.ts.map +1 -1
- package/dist/irdevices/others.js +34 -29
- package/dist/irdevices/others.js.map +1 -1
- package/dist/irdevices/tvs.d.ts +5 -5
- package/dist/irdevices/tvs.d.ts.map +1 -1
- package/dist/irdevices/tvs.js +91 -76
- package/dist/irdevices/tvs.js.map +1 -1
- package/dist/irdevices/vacuumcleaners.d.ts +5 -5
- package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
- package/dist/irdevices/vacuumcleaners.js +31 -27
- package/dist/irdevices/vacuumcleaners.js.map +1 -1
- package/dist/irdevices/waterheaters.d.ts +5 -5
- package/dist/irdevices/waterheaters.d.ts.map +1 -1
- package/dist/irdevices/waterheaters.js +31 -27
- package/dist/irdevices/waterheaters.js.map +1 -1
- package/dist/platform.d.ts +12 -12
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +241 -172
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +1 -1
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +4 -4
- package/dist/settings.js.map +1 -1
- package/package.json +72 -71
- package/src/devices/bots.ts +927 -442
- package/src/devices/colorbulb.ts +473 -246
- package/src/devices/contact.ts +381 -208
- package/src/devices/curtains.ts +613 -321
- package/src/devices/humidifiers.ts +594 -323
- package/src/devices/meters.ts +331 -186
- package/src/devices/motion.ts +327 -184
- package/src/devices/plugs.ts +175 -107
- package/src/index.ts +5 -5
- package/src/irdevices/airconditioners.ts +360 -149
- package/src/irdevices/airpurifiers.ts +178 -80
- package/src/irdevices/cameras.ts +109 -51
- package/src/irdevices/fans.ts +174 -77
- package/src/irdevices/lights.ts +110 -57
- package/src/irdevices/others.ts +137 -62
- package/src/irdevices/tvs.ts +249 -124
- package/src/irdevices/vacuumcleaners.ts +112 -52
- package/src/irdevices/waterheaters.ts +115 -53
- package/src/platform.ts +768 -357
- package/src/settings.ts +17 -23
package/src/devices/meters.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import * as rxjs from "rxjs";
|
|
2
|
+
import * as operators from "rxjs/operators";
|
|
3
|
+
import * as platform_1 from "../platform";
|
|
4
|
+
import * as homebridge from "homebridge";
|
|
5
|
+
import * as settings from "../settings";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Platform Accessory
|
|
@@ -11,34 +11,34 @@ import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceSta
|
|
|
11
11
|
*/
|
|
12
12
|
export class Meter {
|
|
13
13
|
// Services
|
|
14
|
-
private batteryService?: Service;
|
|
15
|
-
private temperatureservice?: Service;
|
|
16
|
-
private humidityservice?: Service;
|
|
14
|
+
private batteryService?: homebridge.Service;
|
|
15
|
+
private temperatureservice?: homebridge.Service;
|
|
16
|
+
private humidityservice?: homebridge.Service;
|
|
17
17
|
|
|
18
18
|
// Characteristic Values
|
|
19
|
-
CurrentRelativeHumidity!: CharacteristicValue;
|
|
20
|
-
CurrentRelativeHumidityCached!: CharacteristicValue;
|
|
21
|
-
CurrentTemperature?: CharacteristicValue;
|
|
22
|
-
CurrentTemperatureCached?: CharacteristicValue;
|
|
23
|
-
BatteryLevel?: CharacteristicValue;
|
|
24
|
-
ChargingState?: CharacteristicValue;
|
|
25
|
-
StatusLowBattery?: CharacteristicValue;
|
|
26
|
-
Active!: CharacteristicValue;
|
|
27
|
-
WaterLevel!: CharacteristicValue;
|
|
19
|
+
CurrentRelativeHumidity!: homebridge.CharacteristicValue;
|
|
20
|
+
CurrentRelativeHumidityCached!: homebridge.CharacteristicValue;
|
|
21
|
+
CurrentTemperature?: homebridge.CharacteristicValue;
|
|
22
|
+
CurrentTemperatureCached?: homebridge.CharacteristicValue;
|
|
23
|
+
BatteryLevel?: homebridge.CharacteristicValue;
|
|
24
|
+
ChargingState?: homebridge.CharacteristicValue;
|
|
25
|
+
StatusLowBattery?: homebridge.CharacteristicValue;
|
|
26
|
+
Active!: homebridge.CharacteristicValue;
|
|
27
|
+
WaterLevel!: homebridge.CharacteristicValue;
|
|
28
28
|
|
|
29
29
|
// OpenAPI Others
|
|
30
|
-
Temperature: deviceStatus[
|
|
31
|
-
Humidity: deviceStatus[
|
|
32
|
-
deviceStatus!: deviceStatusResponse;
|
|
30
|
+
Temperature: settings.deviceStatus["temperature"];
|
|
31
|
+
Humidity: settings.deviceStatus["humidity"];
|
|
32
|
+
deviceStatus!: settings.deviceStatusResponse;
|
|
33
33
|
|
|
34
34
|
// BLE Others
|
|
35
35
|
connected?: boolean;
|
|
36
|
-
switchbot!: switchbot;
|
|
36
|
+
switchbot!: settings.switchbot;
|
|
37
37
|
SwitchToOpenAPI?: boolean;
|
|
38
|
-
serviceData!: serviceData;
|
|
39
|
-
temperature!: temperature[
|
|
40
|
-
battery!: serviceData[
|
|
41
|
-
humidity!: serviceData[
|
|
38
|
+
serviceData!: settings.serviceData;
|
|
39
|
+
temperature!: settings.temperature["c"];
|
|
40
|
+
battery!: settings.serviceData["battery"];
|
|
41
|
+
humidity!: settings.serviceData["humidity"];
|
|
42
42
|
|
|
43
43
|
// Config
|
|
44
44
|
scanDuration!: number;
|
|
@@ -47,12 +47,12 @@ export class Meter {
|
|
|
47
47
|
|
|
48
48
|
// Updates
|
|
49
49
|
meterUpdateInProgress!: boolean;
|
|
50
|
-
doMeterUpdate: Subject<void>;
|
|
50
|
+
doMeterUpdate: rxjs.Subject<void>;
|
|
51
51
|
|
|
52
52
|
constructor(
|
|
53
|
-
private readonly platform: SwitchBotPlatform,
|
|
54
|
-
private accessory: PlatformAccessory,
|
|
55
|
-
public device: device & devicesConfig,
|
|
53
|
+
private readonly platform: platform_1.SwitchBotPlatform,
|
|
54
|
+
private accessory: homebridge.PlatformAccessory,
|
|
55
|
+
public device: settings.device & settings.devicesConfig,
|
|
56
56
|
) {
|
|
57
57
|
// default placeholders
|
|
58
58
|
this.logs(device);
|
|
@@ -62,7 +62,8 @@ export class Meter {
|
|
|
62
62
|
if (this.CurrentRelativeHumidity === undefined) {
|
|
63
63
|
this.CurrentRelativeHumidity = 0;
|
|
64
64
|
} else {
|
|
65
|
-
this.CurrentRelativeHumidity =
|
|
65
|
+
this.CurrentRelativeHumidity =
|
|
66
|
+
this.accessory.context.CurrentRelativeHumidity;
|
|
66
67
|
}
|
|
67
68
|
if (this.CurrentTemperature === undefined) {
|
|
68
69
|
this.CurrentTemperature = 0;
|
|
@@ -70,12 +71,8 @@ export class Meter {
|
|
|
70
71
|
this.CurrentTemperature = this.accessory.context.CurrentTemperature;
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
// Meter Config
|
|
74
|
-
this.debugLog(`Meter: ${this.accessory.displayName} Config: (ble: ${device.ble}, hide_temperature: ${device.meter?.hide_temperature},`
|
|
75
|
-
+ ` hide_humidity: ${device.meter?.hide_humidity})`);
|
|
76
|
-
|
|
77
74
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
78
|
-
this.doMeterUpdate = new Subject();
|
|
75
|
+
this.doMeterUpdate = new rxjs.Subject();
|
|
79
76
|
this.meterUpdateInProgress = false;
|
|
80
77
|
|
|
81
78
|
// Retrieve initial values and updateHomekit
|
|
@@ -84,27 +81,43 @@ export class Meter {
|
|
|
84
81
|
// set accessory information
|
|
85
82
|
accessory
|
|
86
83
|
.getService(this.platform.Service.AccessoryInformation)!
|
|
87
|
-
.setCharacteristic(this.platform.Characteristic.Manufacturer,
|
|
88
|
-
.setCharacteristic(
|
|
89
|
-
|
|
84
|
+
.setCharacteristic(this.platform.Characteristic.Manufacturer, "SwitchBot")
|
|
85
|
+
.setCharacteristic(
|
|
86
|
+
this.platform.Characteristic.Model,
|
|
87
|
+
"SWITCHBOT-METERTH-S1",
|
|
88
|
+
)
|
|
89
|
+
.setCharacteristic(
|
|
90
|
+
this.platform.Characteristic.SerialNumber,
|
|
91
|
+
device.deviceId!,
|
|
92
|
+
);
|
|
90
93
|
|
|
91
94
|
// Temperature Sensor Service
|
|
92
95
|
if (device.meter?.hide_temperature) {
|
|
93
|
-
this.debugLog(
|
|
94
|
-
|
|
96
|
+
this.debugLog(
|
|
97
|
+
`Meter: ${accessory.displayName} Removing Temperature Sensor Service`,
|
|
98
|
+
);
|
|
99
|
+
this.temperatureservice = this.accessory.getService(
|
|
100
|
+
this.platform.Service.TemperatureSensor,
|
|
101
|
+
);
|
|
95
102
|
accessory.removeService(this.temperatureservice!);
|
|
96
103
|
} else if (!this.temperatureservice) {
|
|
97
|
-
this.debugLog(
|
|
104
|
+
this.debugLog(
|
|
105
|
+
`Meter: ${accessory.displayName} Add Temperature Sensor Service`,
|
|
106
|
+
);
|
|
98
107
|
(this.temperatureservice =
|
|
99
108
|
this.accessory.getService(this.platform.Service.TemperatureSensor) ||
|
|
100
|
-
this.accessory.addService(this.platform.Service.TemperatureSensor)),
|
|
109
|
+
this.accessory.addService(this.platform.Service.TemperatureSensor)),
|
|
110
|
+
`${accessory.displayName} Temperature Sensor`;
|
|
101
111
|
|
|
102
|
-
this.temperatureservice.setCharacteristic(
|
|
112
|
+
this.temperatureservice.setCharacteristic(
|
|
113
|
+
this.platform.Characteristic.Name,
|
|
114
|
+
`${accessory.displayName} Temperature Sensor`,
|
|
115
|
+
);
|
|
103
116
|
|
|
104
117
|
this.temperatureservice
|
|
105
118
|
.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
|
|
106
119
|
.setProps({
|
|
107
|
-
unit: Units[
|
|
120
|
+
unit: homebridge.Units["CELSIUS"],
|
|
108
121
|
validValueRanges: [-273.15, 100],
|
|
109
122
|
minValue: -273.15,
|
|
110
123
|
maxValue: 100,
|
|
@@ -114,21 +127,33 @@ export class Meter {
|
|
|
114
127
|
return this.CurrentTemperature!;
|
|
115
128
|
});
|
|
116
129
|
} else {
|
|
117
|
-
this.debugLog(
|
|
130
|
+
this.debugLog(
|
|
131
|
+
`Meter: ${accessory.displayName} Temperature Sensor Service Not Added`,
|
|
132
|
+
);
|
|
118
133
|
}
|
|
119
134
|
|
|
120
135
|
// Humidity Sensor Service
|
|
121
136
|
if (device.meter?.hide_humidity) {
|
|
122
|
-
this.debugLog(
|
|
123
|
-
|
|
137
|
+
this.debugLog(
|
|
138
|
+
`Meter: ${accessory.displayName} Removing Humidity Sensor Service`,
|
|
139
|
+
);
|
|
140
|
+
this.humidityservice = this.accessory.getService(
|
|
141
|
+
this.platform.Service.HumiditySensor,
|
|
142
|
+
);
|
|
124
143
|
accessory.removeService(this.humidityservice!);
|
|
125
144
|
} else if (!this.humidityservice) {
|
|
126
|
-
this.debugLog(
|
|
145
|
+
this.debugLog(
|
|
146
|
+
`Meter: ${accessory.displayName} Add Humidity Sensor Service`,
|
|
147
|
+
);
|
|
127
148
|
(this.humidityservice =
|
|
128
149
|
this.accessory.getService(this.platform.Service.HumiditySensor) ||
|
|
129
|
-
this.accessory.addService(this.platform.Service.HumiditySensor)),
|
|
150
|
+
this.accessory.addService(this.platform.Service.HumiditySensor)),
|
|
151
|
+
`${accessory.displayName} Humidity Sensor`;
|
|
130
152
|
|
|
131
|
-
this.humidityservice.setCharacteristic(
|
|
153
|
+
this.humidityservice.setCharacteristic(
|
|
154
|
+
this.platform.Characteristic.Name,
|
|
155
|
+
`${accessory.displayName} Humidity Sensor`,
|
|
156
|
+
);
|
|
132
157
|
|
|
133
158
|
this.humidityservice
|
|
134
159
|
.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
|
|
@@ -139,36 +164,47 @@ export class Meter {
|
|
|
139
164
|
return this.CurrentRelativeHumidity;
|
|
140
165
|
});
|
|
141
166
|
} else {
|
|
142
|
-
this.debugLog(
|
|
167
|
+
this.debugLog(
|
|
168
|
+
`Meter: ${accessory.displayName} Humidity Sensor Service Not Added`,
|
|
169
|
+
);
|
|
143
170
|
}
|
|
144
171
|
|
|
145
172
|
// Battery Service
|
|
146
173
|
if (!device.ble) {
|
|
147
174
|
this.debugLog(`Meter: ${accessory.displayName} Removing Battery Service`);
|
|
148
|
-
this.batteryService = this.accessory.getService(
|
|
175
|
+
this.batteryService = this.accessory.getService(
|
|
176
|
+
this.platform.Service.Battery,
|
|
177
|
+
);
|
|
149
178
|
accessory.removeService(this.batteryService!);
|
|
150
179
|
} else if (device.ble && !this.batteryService) {
|
|
151
180
|
this.debugLog(`Meter: ${accessory.displayName} Add Battery Service`);
|
|
152
181
|
(this.batteryService =
|
|
153
182
|
this.accessory.getService(this.platform.Service.Battery) ||
|
|
154
|
-
this.accessory.addService(this.platform.Service.Battery)),
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
183
|
+
this.accessory.addService(this.platform.Service.Battery)),
|
|
184
|
+
`${accessory.displayName} Battery`;
|
|
185
|
+
|
|
186
|
+
this.batteryService.setCharacteristic(
|
|
187
|
+
this.platform.Characteristic.Name,
|
|
188
|
+
`${accessory.displayName} Battery`,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
this.batteryService.setCharacteristic(
|
|
192
|
+
this.platform.Characteristic.ChargingState,
|
|
193
|
+
this.platform.Characteristic.ChargingState.NOT_CHARGEABLE,
|
|
194
|
+
);
|
|
160
195
|
} else {
|
|
161
|
-
this.debugLog(
|
|
196
|
+
this.debugLog(
|
|
197
|
+
`Meter: ${accessory.displayName} Battery Service Not Added`,
|
|
198
|
+
);
|
|
162
199
|
}
|
|
163
200
|
|
|
164
|
-
|
|
165
|
-
|
|
166
201
|
// Retrieve initial values and updateHomekit
|
|
167
202
|
this.updateHomeKitCharacteristics();
|
|
168
203
|
|
|
169
204
|
// Start an update interval
|
|
170
|
-
|
|
171
|
-
.
|
|
205
|
+
rxjs
|
|
206
|
+
.interval(this.deviceRefreshRate * 1000)
|
|
207
|
+
.pipe(operators.skipWhile(() => this.meterUpdateInProgress))
|
|
172
208
|
.subscribe(() => {
|
|
173
209
|
this.refreshStatus();
|
|
174
210
|
});
|
|
@@ -191,23 +227,35 @@ export class Meter {
|
|
|
191
227
|
// Battery
|
|
192
228
|
this.BatteryLevel = Number(this.battery);
|
|
193
229
|
if (this.BatteryLevel < 15) {
|
|
194
|
-
this.StatusLowBattery =
|
|
230
|
+
this.StatusLowBattery =
|
|
231
|
+
this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
195
232
|
} else {
|
|
196
|
-
this.StatusLowBattery =
|
|
233
|
+
this.StatusLowBattery =
|
|
234
|
+
this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
197
235
|
}
|
|
198
|
-
this.debugLog(
|
|
236
|
+
this.debugLog(
|
|
237
|
+
`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`,
|
|
238
|
+
);
|
|
199
239
|
|
|
200
240
|
// Humidity
|
|
201
241
|
if (!this.device.meter?.hide_humidity) {
|
|
202
242
|
this.CurrentRelativeHumidity = this.humidity!;
|
|
203
|
-
this.debugLog(
|
|
243
|
+
this.debugLog(
|
|
244
|
+
`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`,
|
|
245
|
+
);
|
|
204
246
|
}
|
|
205
247
|
|
|
206
248
|
// Current Temperature
|
|
207
249
|
if (!this.device.meter?.hide_temperature) {
|
|
208
|
-
this.temperature < 0
|
|
250
|
+
this.temperature < 0
|
|
251
|
+
? 0
|
|
252
|
+
: this.temperature > 100
|
|
253
|
+
? 100
|
|
254
|
+
: this.temperature;
|
|
209
255
|
this.CurrentTemperature = this.temperature;
|
|
210
|
-
this.debugLog(
|
|
256
|
+
this.debugLog(
|
|
257
|
+
`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`,
|
|
258
|
+
);
|
|
211
259
|
}
|
|
212
260
|
}
|
|
213
261
|
|
|
@@ -220,13 +268,17 @@ export class Meter {
|
|
|
220
268
|
// Current Relative Humidity
|
|
221
269
|
if (!this.device.meter?.hide_humidity) {
|
|
222
270
|
this.CurrentRelativeHumidity = this.Humidity!;
|
|
223
|
-
this.debugLog(
|
|
271
|
+
this.debugLog(
|
|
272
|
+
`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`,
|
|
273
|
+
);
|
|
224
274
|
}
|
|
225
275
|
|
|
226
276
|
// Current Temperature
|
|
227
277
|
if (!this.device.meter?.hide_temperature) {
|
|
228
278
|
this.CurrentTemperature = this.Temperature!;
|
|
229
|
-
this.debugLog(
|
|
279
|
+
this.debugLog(
|
|
280
|
+
`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`,
|
|
281
|
+
);
|
|
230
282
|
}
|
|
231
283
|
}
|
|
232
284
|
}
|
|
@@ -246,94 +298,123 @@ export class Meter {
|
|
|
246
298
|
this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
|
|
247
299
|
const switchbot = await this.platform.connectBLE();
|
|
248
300
|
// Convert to BLE Address
|
|
249
|
-
this.device.bleMac =
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
301
|
+
this.device.bleMac = this.device
|
|
302
|
+
.deviceId!.match(/.{1,2}/g)!
|
|
303
|
+
.join(":")
|
|
304
|
+
.toLowerCase();
|
|
305
|
+
this.debugLog(
|
|
306
|
+
`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`,
|
|
307
|
+
);
|
|
308
|
+
try {
|
|
309
|
+
await switchbot.discover({
|
|
310
|
+
model: "T",
|
|
311
|
+
quick: false,
|
|
312
|
+
});
|
|
313
|
+
// Start to monitor advertisement packets
|
|
314
|
+
await switchbot.startScan({
|
|
315
|
+
model: "T",
|
|
255
316
|
id: this.device.bleMac,
|
|
256
|
-
})
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
if (this.connected) {
|
|
282
|
-
this.parseStatus();
|
|
283
|
-
this.updateHomeKitCharacteristics();
|
|
317
|
+
});
|
|
318
|
+
// Set an event hander
|
|
319
|
+
switchbot.onadvertisement = (ad: settings.ad) => {
|
|
320
|
+
this.serviceData = ad.serviceData;
|
|
321
|
+
this.temperature = ad.serviceData.temperature!.c;
|
|
322
|
+
this.humidity = ad.serviceData.humidity;
|
|
323
|
+
this.battery = ad.serviceData.battery;
|
|
324
|
+
this.debugLog(
|
|
325
|
+
`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(
|
|
326
|
+
ad.serviceData,
|
|
327
|
+
)}`,
|
|
328
|
+
);
|
|
329
|
+
this.debugLog(
|
|
330
|
+
`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, ` +
|
|
331
|
+
`temperature: ${JSON.stringify(
|
|
332
|
+
ad.serviceData.temperature?.c,
|
|
333
|
+
)}, humidity: ${ad.serviceData.humidity}, ` +
|
|
334
|
+
`battery: ${ad.serviceData.battery}`,
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
if (this.serviceData) {
|
|
338
|
+
this.connected = true;
|
|
339
|
+
this.debugLog(
|
|
340
|
+
`Meter: ${this.accessory.displayName} connected: ${this.connected}`,
|
|
341
|
+
);
|
|
284
342
|
} else {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if (this.deviceLogging === 'debug') {
|
|
290
|
-
this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
291
|
-
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
292
|
-
}
|
|
293
|
-
if (this.platform.debugMode) {
|
|
294
|
-
this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
295
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
343
|
+
this.connected = false;
|
|
344
|
+
this.debugLog(
|
|
345
|
+
`Meter: ${this.accessory.displayName} connected: ${this.connected}`,
|
|
346
|
+
);
|
|
296
347
|
}
|
|
348
|
+
};
|
|
349
|
+
// Wait 10 seconds
|
|
350
|
+
await switchbot.wait(this.scanDuration * 1000);
|
|
351
|
+
// Stop to monitor
|
|
352
|
+
switchbot.stopScan();
|
|
353
|
+
if (this.connected) {
|
|
354
|
+
this.parseStatus();
|
|
355
|
+
this.updateHomeKitCharacteristics();
|
|
356
|
+
} else {
|
|
357
|
+
this.errorLog(
|
|
358
|
+
`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`,
|
|
359
|
+
);
|
|
297
360
|
if (this.platform.config.credentials?.openToken) {
|
|
298
|
-
this.warnLog(
|
|
361
|
+
this.warnLog(
|
|
362
|
+
`Meter: ${this.accessory.displayName} Using OpenAPI Connection`,
|
|
363
|
+
);
|
|
299
364
|
this.SwitchToOpenAPI = true;
|
|
300
|
-
this.openAPIRefreshStatus();
|
|
365
|
+
await this.openAPIRefreshStatus();
|
|
301
366
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
this.
|
|
314
|
-
|
|
367
|
+
}
|
|
368
|
+
} catch (e: any) {
|
|
369
|
+
this.errorLog(
|
|
370
|
+
`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`,
|
|
371
|
+
);
|
|
372
|
+
if (this.deviceLogging.includes("debug")) {
|
|
373
|
+
this.errorLog(
|
|
374
|
+
`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
|
|
375
|
+
` Error Message: ${JSON.stringify(e.message)}`,
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
if (this.platform.config.credentials?.openToken) {
|
|
379
|
+
this.warnLog(
|
|
380
|
+
`Meter: ${this.accessory.displayName} Using OpenAPI Connection`,
|
|
381
|
+
);
|
|
382
|
+
this.SwitchToOpenAPI = true;
|
|
383
|
+
this.openAPIRefreshStatus();
|
|
384
|
+
}
|
|
385
|
+
this.apiError(e);
|
|
315
386
|
}
|
|
316
387
|
}
|
|
317
388
|
|
|
318
389
|
private async openAPIRefreshStatus() {
|
|
319
390
|
if (this.platform.config.credentials?.openToken) {
|
|
320
|
-
this.debugLog(
|
|
391
|
+
this.debugLog(
|
|
392
|
+
`Meter: ${this.accessory.displayName} OpenAPI RefreshStatus`,
|
|
393
|
+
);
|
|
321
394
|
try {
|
|
322
|
-
this.deviceStatus = (
|
|
323
|
-
|
|
395
|
+
this.deviceStatus = (
|
|
396
|
+
await this.platform.axios.get(
|
|
397
|
+
`${settings.DeviceURL}/${this.device.deviceId}/status`,
|
|
398
|
+
)
|
|
399
|
+
).data;
|
|
400
|
+
this.debugLog(
|
|
401
|
+
`Meter: ${
|
|
402
|
+
this.accessory.displayName
|
|
403
|
+
} openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`,
|
|
404
|
+
);
|
|
324
405
|
this.Humidity = this.deviceStatus.body.humidity!;
|
|
325
406
|
this.Temperature = this.deviceStatus.body.temperature!;
|
|
326
407
|
this.parseStatus();
|
|
327
408
|
this.updateHomeKitCharacteristics();
|
|
328
409
|
} catch (e: any) {
|
|
329
|
-
this.errorLog(
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
410
|
+
this.errorLog(
|
|
411
|
+
`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`,
|
|
412
|
+
);
|
|
413
|
+
if (this.deviceLogging.includes("debug")) {
|
|
414
|
+
this.errorLog(
|
|
415
|
+
`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
|
|
416
|
+
` Error Message: ${JSON.stringify(e.message)}`,
|
|
417
|
+
);
|
|
337
418
|
}
|
|
338
419
|
this.apiError(e);
|
|
339
420
|
}
|
|
@@ -346,115 +427,177 @@ export class Meter {
|
|
|
346
427
|
updateHomeKitCharacteristics() {
|
|
347
428
|
if (!this.device.meter?.hide_humidity) {
|
|
348
429
|
if (this.CurrentRelativeHumidity === undefined) {
|
|
349
|
-
this.debugLog(
|
|
430
|
+
this.debugLog(
|
|
431
|
+
`Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
|
|
432
|
+
);
|
|
350
433
|
} else {
|
|
351
|
-
this.humidityservice?.updateCharacteristic(
|
|
352
|
-
|
|
434
|
+
this.humidityservice?.updateCharacteristic(
|
|
435
|
+
this.platform.Characteristic.CurrentRelativeHumidity,
|
|
436
|
+
this.CurrentRelativeHumidity,
|
|
437
|
+
);
|
|
438
|
+
this.debugLog(
|
|
439
|
+
`Meter: ${this.accessory.displayName} updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
|
|
440
|
+
);
|
|
353
441
|
}
|
|
354
442
|
}
|
|
355
443
|
if (!this.device.meter?.hide_temperature) {
|
|
356
444
|
if (this.CurrentTemperature === undefined) {
|
|
357
|
-
this.debugLog(
|
|
445
|
+
this.debugLog(
|
|
446
|
+
`Meter: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`,
|
|
447
|
+
);
|
|
358
448
|
} else {
|
|
359
|
-
this.temperatureservice?.updateCharacteristic(
|
|
360
|
-
|
|
449
|
+
this.temperatureservice?.updateCharacteristic(
|
|
450
|
+
this.platform.Characteristic.CurrentTemperature,
|
|
451
|
+
this.CurrentTemperature,
|
|
452
|
+
);
|
|
453
|
+
this.debugLog(
|
|
454
|
+
`Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`,
|
|
455
|
+
);
|
|
361
456
|
}
|
|
362
457
|
}
|
|
363
458
|
if (this.device.ble) {
|
|
364
459
|
if (this.BatteryLevel === undefined) {
|
|
365
|
-
this.debugLog(
|
|
460
|
+
this.debugLog(
|
|
461
|
+
`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`,
|
|
462
|
+
);
|
|
366
463
|
} else {
|
|
367
|
-
this.batteryService?.updateCharacteristic(
|
|
368
|
-
|
|
464
|
+
this.batteryService?.updateCharacteristic(
|
|
465
|
+
this.platform.Characteristic.BatteryLevel,
|
|
466
|
+
this.BatteryLevel,
|
|
467
|
+
);
|
|
468
|
+
this.debugLog(
|
|
469
|
+
`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`,
|
|
470
|
+
);
|
|
369
471
|
}
|
|
370
472
|
if (this.StatusLowBattery === undefined) {
|
|
371
|
-
this.debugLog(
|
|
473
|
+
this.debugLog(
|
|
474
|
+
`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`,
|
|
475
|
+
);
|
|
372
476
|
} else {
|
|
373
|
-
this.batteryService?.updateCharacteristic(
|
|
374
|
-
|
|
477
|
+
this.batteryService?.updateCharacteristic(
|
|
478
|
+
this.platform.Characteristic.StatusLowBattery,
|
|
479
|
+
this.StatusLowBattery,
|
|
480
|
+
);
|
|
481
|
+
this.debugLog(
|
|
482
|
+
`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`,
|
|
483
|
+
);
|
|
375
484
|
}
|
|
376
485
|
}
|
|
377
486
|
}
|
|
378
487
|
|
|
379
488
|
public apiError(e: any) {
|
|
380
489
|
if (!this.device.meter?.hide_humidity) {
|
|
381
|
-
this.humidityservice?.updateCharacteristic(
|
|
490
|
+
this.humidityservice?.updateCharacteristic(
|
|
491
|
+
this.platform.Characteristic.CurrentRelativeHumidity,
|
|
492
|
+
e,
|
|
493
|
+
);
|
|
382
494
|
}
|
|
383
495
|
if (!this.device.meter?.hide_temperature) {
|
|
384
|
-
this.temperatureservice?.updateCharacteristic(
|
|
496
|
+
this.temperatureservice?.updateCharacteristic(
|
|
497
|
+
this.platform.Characteristic.CurrentTemperature,
|
|
498
|
+
e,
|
|
499
|
+
);
|
|
385
500
|
}
|
|
386
501
|
if (this.device.ble) {
|
|
387
|
-
this.batteryService?.updateCharacteristic(
|
|
388
|
-
|
|
502
|
+
this.batteryService?.updateCharacteristic(
|
|
503
|
+
this.platform.Characteristic.BatteryLevel,
|
|
504
|
+
e,
|
|
505
|
+
);
|
|
506
|
+
this.batteryService?.updateCharacteristic(
|
|
507
|
+
this.platform.Characteristic.StatusLowBattery,
|
|
508
|
+
e,
|
|
509
|
+
);
|
|
389
510
|
}
|
|
390
511
|
//throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
391
512
|
}
|
|
392
513
|
|
|
393
|
-
config(device: device & devicesConfig) {
|
|
514
|
+
config(device: settings.device & settings.devicesConfig) {
|
|
394
515
|
let config = {};
|
|
395
516
|
if (device.meter) {
|
|
396
517
|
config = device.meter;
|
|
397
518
|
}
|
|
398
519
|
if (device.ble !== undefined) {
|
|
399
|
-
config[
|
|
520
|
+
config["ble"] = device.ble;
|
|
400
521
|
}
|
|
401
522
|
if (device.logging !== undefined) {
|
|
402
|
-
config[
|
|
523
|
+
config["logging"] = device.logging;
|
|
403
524
|
}
|
|
404
525
|
if (device.refreshRate !== undefined) {
|
|
405
|
-
config[
|
|
526
|
+
config["refreshRate"] = device.refreshRate;
|
|
406
527
|
}
|
|
407
528
|
if (device.scanDuration !== undefined) {
|
|
408
|
-
config[
|
|
529
|
+
config["scanDuration"] = device.scanDuration;
|
|
409
530
|
}
|
|
410
531
|
if (Object.entries(config).length !== 0) {
|
|
411
|
-
this.warnLog(
|
|
532
|
+
this.warnLog(
|
|
533
|
+
`Meter: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`,
|
|
534
|
+
);
|
|
412
535
|
}
|
|
413
536
|
}
|
|
414
537
|
|
|
415
|
-
refreshRate(device: device & devicesConfig) {
|
|
538
|
+
refreshRate(device: settings.device & settings.devicesConfig) {
|
|
416
539
|
if (device.refreshRate) {
|
|
417
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate =
|
|
418
|
-
|
|
540
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate =
|
|
541
|
+
device.refreshRate;
|
|
542
|
+
this.debugLog(
|
|
543
|
+
`Meter: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`,
|
|
544
|
+
);
|
|
419
545
|
} else if (this.platform.config.options!.refreshRate) {
|
|
420
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate =
|
|
421
|
-
|
|
546
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate =
|
|
547
|
+
this.platform.config.options!.refreshRate;
|
|
548
|
+
this.debugLog(
|
|
549
|
+
`Meter: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`,
|
|
550
|
+
);
|
|
422
551
|
}
|
|
423
552
|
}
|
|
424
553
|
|
|
425
|
-
scan(device: device & devicesConfig) {
|
|
554
|
+
scan(device: settings.device & settings.devicesConfig) {
|
|
426
555
|
if (device.scanDuration) {
|
|
427
|
-
this.scanDuration = this.accessory.context.scanDuration =
|
|
556
|
+
this.scanDuration = this.accessory.context.scanDuration =
|
|
557
|
+
device.scanDuration;
|
|
428
558
|
if (device.ble) {
|
|
429
|
-
this.debugLog(
|
|
559
|
+
this.debugLog(
|
|
560
|
+
`Meter: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`,
|
|
561
|
+
);
|
|
430
562
|
}
|
|
431
563
|
} else {
|
|
432
564
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
433
565
|
if (this.device.ble) {
|
|
434
|
-
this.debugLog(
|
|
566
|
+
this.debugLog(
|
|
567
|
+
`Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`,
|
|
568
|
+
);
|
|
435
569
|
}
|
|
436
570
|
}
|
|
437
571
|
}
|
|
438
572
|
|
|
439
|
-
logs(device: device & devicesConfig) {
|
|
573
|
+
logs(device: settings.device & settings.devicesConfig) {
|
|
440
574
|
if (this.platform.debugMode) {
|
|
441
|
-
this.deviceLogging = this.accessory.context.logging =
|
|
442
|
-
this.debugLog(
|
|
575
|
+
this.deviceLogging = this.accessory.context.logging = "debugMode";
|
|
576
|
+
this.debugLog(
|
|
577
|
+
`Meter: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`,
|
|
578
|
+
);
|
|
443
579
|
} else if (device.logging) {
|
|
444
580
|
this.deviceLogging = this.accessory.context.logging = device.logging;
|
|
445
|
-
this.debugLog(
|
|
581
|
+
this.debugLog(
|
|
582
|
+
`Meter: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`,
|
|
583
|
+
);
|
|
446
584
|
} else if (this.platform.config.options?.logging) {
|
|
447
|
-
this.deviceLogging = this.accessory.context.logging =
|
|
448
|
-
|
|
585
|
+
this.deviceLogging = this.accessory.context.logging =
|
|
586
|
+
this.platform.config.options?.logging;
|
|
587
|
+
this.debugLog(
|
|
588
|
+
`Meter: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`,
|
|
589
|
+
);
|
|
449
590
|
} else {
|
|
450
|
-
this.deviceLogging = this.accessory.context.logging =
|
|
451
|
-
this.debugLog(
|
|
591
|
+
this.deviceLogging = this.accessory.context.logging = "standard";
|
|
592
|
+
this.debugLog(
|
|
593
|
+
`Meter: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`,
|
|
594
|
+
);
|
|
452
595
|
}
|
|
453
596
|
}
|
|
454
597
|
|
|
455
598
|
/**
|
|
456
|
-
|
|
457
|
-
|
|
599
|
+
* Logging for Device
|
|
600
|
+
*/
|
|
458
601
|
infoLog(...log: any[]) {
|
|
459
602
|
if (this.enablingDeviceLogging()) {
|
|
460
603
|
this.platform.log.info(String(...log));
|
|
@@ -475,8 +618,8 @@ export class Meter {
|
|
|
475
618
|
|
|
476
619
|
debugLog(...log: any[]) {
|
|
477
620
|
if (this.enablingDeviceLogging()) {
|
|
478
|
-
if (this.deviceLogging ===
|
|
479
|
-
this.platform.log.info(
|
|
621
|
+
if (this.deviceLogging === "debug") {
|
|
622
|
+
this.platform.log.info("[DEBUG]", String(...log));
|
|
480
623
|
} else {
|
|
481
624
|
this.platform.log.debug(String(...log));
|
|
482
625
|
}
|
|
@@ -484,6 +627,8 @@ export class Meter {
|
|
|
484
627
|
}
|
|
485
628
|
|
|
486
629
|
enablingDeviceLogging(): boolean {
|
|
487
|
-
return
|
|
630
|
+
return (
|
|
631
|
+
this.deviceLogging.includes("debug") || this.deviceLogging === "standard"
|
|
632
|
+
);
|
|
488
633
|
}
|
|
489
634
|
}
|