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