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