@switchbot/homebridge-switchbot 3.5.1-beta.0 → 3.5.1-beta.10
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/README.md +16 -0
- package/dist/device/blindtilt.d.ts +15 -6
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +224 -370
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +9 -4
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +197 -221
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +10 -5
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +214 -292
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +10 -5
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +335 -421
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +9 -4
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +121 -200
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +20 -7
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +229 -403
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +41 -22
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +212 -181
- package/dist/device/device.js.map +1 -1
- package/dist/device/fan.d.ts +9 -4
- package/dist/device/fan.d.ts.map +1 -1
- package/dist/device/fan.js +204 -249
- package/dist/device/fan.js.map +1 -1
- package/dist/device/hub.d.ts +9 -5
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +107 -233
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +12 -4
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +193 -235
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +9 -4
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +92 -159
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +10 -5
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +288 -370
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +10 -5
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +172 -219
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +9 -4
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +94 -160
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +9 -4
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +95 -157
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +9 -4
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +85 -144
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +9 -4
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +100 -127
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +9 -6
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +172 -259
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +12 -4
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +106 -131
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +2 -1
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +2 -3
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +2 -1
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +2 -3
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +2 -1
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +2 -3
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +2 -1
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +2 -3
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +3 -1
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +5 -1
- package/dist/irdevice/irdevice.js.map +1 -1
- package/dist/irdevice/light.d.ts +2 -1
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +2 -3
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +2 -1
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +2 -3
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +2 -1
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +2 -3
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +2 -1
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +2 -3
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +2 -1
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +2 -3
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +14 -14
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +102 -86
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +49 -190
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +19 -0
- package/dist/settings.js.map +1 -1
- package/dist/types/bledevicestatus.d.ts +279 -0
- package/dist/types/bledevicestatus.d.ts.map +1 -0
- package/dist/types/bledevicestatus.js +2 -0
- package/dist/types/bledevicestatus.js.map +1 -0
- package/dist/types/devicelist.d.ts +89 -0
- package/dist/types/devicelist.d.ts.map +1 -0
- package/dist/types/devicelist.js +6 -0
- package/dist/types/devicelist.js.map +1 -0
- package/dist/types/deviceresponse.d.ts +13 -0
- package/dist/types/deviceresponse.d.ts.map +1 -0
- package/dist/types/deviceresponse.js +2 -0
- package/dist/types/deviceresponse.js.map +1 -0
- package/dist/types/devicestatus.d.ts +149 -0
- package/dist/types/devicestatus.d.ts.map +1 -0
- package/dist/types/devicestatus.js +3 -0
- package/dist/types/devicestatus.js.map +1 -0
- package/dist/types/devicewebhookstatus.d.ts +156 -0
- package/dist/types/devicewebhookstatus.d.ts.map +1 -0
- package/dist/types/devicewebhookstatus.js +2 -0
- package/dist/types/devicewebhookstatus.js.map +1 -0
- package/dist/types/irdevicelist.d.ts +10 -0
- package/dist/types/irdevicelist.d.ts.map +1 -0
- package/dist/types/irdevicelist.js +2 -0
- package/dist/types/irdevicelist.js.map +1 -0
- package/dist/types/pushbody.d.ts +6 -0
- package/dist/types/pushbody.d.ts.map +1 -0
- package/dist/types/pushbody.js +2 -0
- package/dist/types/pushbody.js.map +1 -0
- package/dist/utils.d.ts +25 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +28 -0
- package/dist/utils.js.map +1 -1
- package/package.json +11 -11
- package/src/device/blindtilt.ts +254 -376
- package/src/device/bot.ts +209 -224
- package/src/device/ceilinglight.ts +227 -297
- package/src/device/colorbulb.ts +345 -435
- package/src/device/contact.ts +139 -200
- package/src/device/curtain.ts +254 -414
- package/src/device/device.ts +221 -190
- package/src/device/fan.ts +222 -253
- package/src/device/hub.ts +127 -248
- package/src/device/humidifier.ts +217 -246
- package/src/device/iosensor.ts +115 -168
- package/src/device/lightstrip.ts +303 -380
- package/src/device/lock.ts +188 -224
- package/src/device/meter.ts +117 -179
- package/src/device/meterplus.ts +118 -176
- package/src/device/motion.ts +100 -146
- package/src/device/plug.ts +111 -131
- package/src/device/robotvacuumcleaner.ts +191 -258
- package/src/device/waterdetector.ts +124 -141
- package/src/irdevice/airconditioner.ts +4 -4
- package/src/irdevice/airpurifier.ts +4 -4
- package/src/irdevice/camera.ts +4 -4
- package/src/irdevice/fan.ts +4 -4
- package/src/irdevice/irdevice.ts +8 -2
- package/src/irdevice/light.ts +4 -4
- package/src/irdevice/other.ts +4 -4
- package/src/irdevice/tv.ts +4 -4
- package/src/irdevice/vacuumcleaner.ts +4 -4
- package/src/irdevice/waterheater.ts +4 -4
- package/src/platform.ts +104 -87
- package/src/settings.ts +30 -238
- package/src/types/bledevicestatus.ts +305 -0
- package/src/types/devicelist.ts +124 -0
- package/src/types/deviceresponse.ts +15 -0
- package/src/types/devicestatus.ts +178 -0
- package/src/types/devicewebhookstatus.ts +196 -0
- package/src/types/irdevicelist.ts +15 -0
- package/src/types/pushbody.ts +9 -0
- package/src/utils.ts +33 -0
package/src/device/hub.ts
CHANGED
|
@@ -5,11 +5,16 @@
|
|
|
5
5
|
import { Units } from 'homebridge';
|
|
6
6
|
import { deviceBase } from './device.js';
|
|
7
7
|
import { convertUnits } from '../utils.js';
|
|
8
|
-
import { Subject, interval, skipWhile
|
|
8
|
+
import { Subject, interval, skipWhile} from 'rxjs';
|
|
9
|
+
import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
|
|
9
10
|
|
|
11
|
+
import type { devicesConfig } from '../settings.js';
|
|
12
|
+
import type { device } from '../types/devicelist.js';
|
|
10
13
|
import type { SwitchBotPlatform } from '../platform.js';
|
|
14
|
+
import type { hub2ServiceData } from '../types/bledevicestatus.js';
|
|
15
|
+
import type { hub2Status } from '../types/devicestatus.js';
|
|
16
|
+
import type { hub2WebhookContext } from '../types/devicewebhookstatus.js';
|
|
11
17
|
import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
|
|
12
|
-
import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
|
|
13
18
|
|
|
14
19
|
export class Hub extends deviceBase {
|
|
15
20
|
// Services
|
|
@@ -139,6 +144,9 @@ export class Hub extends deviceBase {
|
|
|
139
144
|
// Retrieve initial values and update Homekit
|
|
140
145
|
this.updateHomeKitCharacteristics();
|
|
141
146
|
|
|
147
|
+
//regisiter webhook event handler
|
|
148
|
+
this.registerWebhook();
|
|
149
|
+
|
|
142
150
|
// Start an update interval
|
|
143
151
|
interval(this.deviceRefreshRate * 1000)
|
|
144
152
|
.pipe(skipWhile(() => this.hubUpdateInProgress))
|
|
@@ -146,51 +154,58 @@ export class Hub extends deviceBase {
|
|
|
146
154
|
await this.refreshStatus();
|
|
147
155
|
});
|
|
148
156
|
|
|
149
|
-
//regisiter webhook event handler
|
|
150
|
-
this.registerWebhook(accessory, device);
|
|
151
|
-
|
|
152
157
|
}
|
|
153
158
|
|
|
154
|
-
async BLEparseStatus(serviceData:
|
|
155
|
-
this.debugLog(
|
|
156
|
-
|
|
159
|
+
async BLEparseStatus(serviceData: hub2ServiceData): Promise<void> {
|
|
160
|
+
await this.debugLog('BLEparseStatus');
|
|
161
|
+
// CurrentTemperature
|
|
162
|
+
if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
|
|
163
|
+
this.TemperatureSensor.CurrentTemperature = serviceData.temperature;
|
|
164
|
+
await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
|
|
165
|
+
}
|
|
166
|
+
// CurrentRelativeHumidity
|
|
167
|
+
if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
|
|
168
|
+
this.HumiditySensor!.CurrentRelativeHumidity = Number(serviceData.humidity);
|
|
169
|
+
await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
|
|
170
|
+
}
|
|
171
|
+
// CurrentAmbientLightLevel
|
|
172
|
+
if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
|
|
173
|
+
const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
|
|
174
|
+
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
|
|
175
|
+
const lightLevel = Number(serviceData.lightLevel);
|
|
176
|
+
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19);
|
|
177
|
+
await this.debugLog(`LightLevel: ${serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
|
|
178
|
+
}
|
|
157
179
|
}
|
|
158
180
|
|
|
159
|
-
async openAPIparseStatus(deviceStatus:
|
|
160
|
-
this.debugLog(
|
|
181
|
+
async openAPIparseStatus(deviceStatus: hub2Status): Promise<void> {
|
|
182
|
+
await this.debugLog('openAPIparseStatus');
|
|
161
183
|
// CurrentRelativeHumidity
|
|
162
|
-
if (!this.device.hub?.hide_humidity) {
|
|
163
|
-
this.HumiditySensor
|
|
164
|
-
this.debugLog(
|
|
184
|
+
if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
|
|
185
|
+
this.HumiditySensor.CurrentRelativeHumidity = deviceStatus.humidity;
|
|
186
|
+
await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
|
|
165
187
|
}
|
|
166
188
|
|
|
167
189
|
// CurrentTemperature
|
|
168
|
-
if (!this.device.hub?.hide_temperature) {
|
|
169
|
-
this.TemperatureSensor
|
|
170
|
-
this.debugLog(
|
|
190
|
+
if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
|
|
191
|
+
this.TemperatureSensor.CurrentTemperature = deviceStatus.temperature;
|
|
192
|
+
await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}°c`);
|
|
171
193
|
}
|
|
172
194
|
|
|
173
|
-
//
|
|
174
|
-
if (!this.device.hub?.hide_lightsensor) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
await this.getLightLevel(lightLevel, set_minLux, set_maxLux, spaceBetweenLevels);
|
|
181
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${deviceStatus.body.lightLevel},`
|
|
182
|
-
+ ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
183
|
-
}
|
|
184
|
-
if (!this.device.hub?.hide_lightsensor) {
|
|
185
|
-
this.LightSensor!.Service.setCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor!.CurrentAmbientLightLevel);
|
|
186
|
-
}
|
|
195
|
+
// LightSensor
|
|
196
|
+
if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
|
|
197
|
+
const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
|
|
198
|
+
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
|
|
199
|
+
const lightLevel = Number(deviceStatus.lightLevel);
|
|
200
|
+
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 19);
|
|
201
|
+
await this.debugLog(`LightLevel: ${deviceStatus.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
187
202
|
}
|
|
188
203
|
|
|
189
204
|
// Firmware Version
|
|
190
|
-
const version = deviceStatus.
|
|
191
|
-
this.debugLog(
|
|
192
|
-
if (deviceStatus.
|
|
193
|
-
const deviceVersion = version
|
|
205
|
+
const version = deviceStatus.version.toString();
|
|
206
|
+
await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
|
|
207
|
+
if (deviceStatus.version) {
|
|
208
|
+
const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
|
|
194
209
|
this.accessory
|
|
195
210
|
.getService(this.hap.Service.AccessoryInformation)!
|
|
196
211
|
.setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
|
|
@@ -198,7 +213,36 @@ export class Hub extends deviceBase {
|
|
|
198
213
|
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
199
214
|
.updateValue(deviceVersion);
|
|
200
215
|
this.accessory.context.deviceVersion = deviceVersion;
|
|
201
|
-
this.debugSuccessLog(
|
|
216
|
+
await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async parseStatusWebhook(context: hub2WebhookContext): Promise<void> {
|
|
221
|
+
await this.debugLog('parseStatusWebhook');
|
|
222
|
+
await this.debugLog(`(scale, temperature, humidity, lightLevel) = Webhook:(${context.scale}, ${convertUnits(context.temperature, context.scale,
|
|
223
|
+
this.device.hub?.convertUnitTo)}, ${context.humidity}, ${context.lightLevel}), current:(${this.TemperatureSensor?.CurrentTemperature},`
|
|
224
|
+
+ ` ${this.HumiditySensor?.CurrentRelativeHumidity}, ${this.LightSensor?.CurrentAmbientLightLevel})`);
|
|
225
|
+
// Check if the scale is not CELSIUS
|
|
226
|
+
if (context.scale !== 'CELSIUS' && this.device.hub?.convertUnitTo === undefined) {
|
|
227
|
+
await this.warnLog(`received a non-CELSIUS Webhook scale: ${context.scale}, Use the *convertUnitsTo* config under Hub settings,`
|
|
228
|
+
+ ' if displaying incorrectly in HomeKit.');
|
|
229
|
+
}
|
|
230
|
+
// CurrentRelativeHumidity
|
|
231
|
+
if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
|
|
232
|
+
this.HumiditySensor.CurrentRelativeHumidity = context.humidity;
|
|
233
|
+
await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}`);
|
|
234
|
+
}
|
|
235
|
+
// CurrentTemperature
|
|
236
|
+
if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
|
|
237
|
+
this.TemperatureSensor.CurrentTemperature = convertUnits(context.temperature, context.scale, this.device.hub?.convertUnitTo);
|
|
238
|
+
await this.debugLog(`CurrentTemperature: ${this.TemperatureSensor.CurrentTemperature}`);
|
|
239
|
+
}
|
|
240
|
+
// CurrentAmbientLightLevel
|
|
241
|
+
if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
|
|
242
|
+
const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
|
|
243
|
+
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
|
|
244
|
+
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(context.lightLevel, set_minLux, set_maxLux, 19);
|
|
245
|
+
await this.debugLog(`CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
|
|
202
246
|
}
|
|
203
247
|
}
|
|
204
248
|
|
|
@@ -207,20 +251,19 @@ export class Hub extends deviceBase {
|
|
|
207
251
|
*/
|
|
208
252
|
async refreshStatus(): Promise<void> {
|
|
209
253
|
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
210
|
-
this.errorLog(
|
|
254
|
+
await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
211
255
|
} else if (this.BLE) {
|
|
212
256
|
await this.BLERefreshStatus();
|
|
213
257
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
214
258
|
await this.openAPIRefreshStatus();
|
|
215
259
|
} else {
|
|
216
260
|
await this.offlineOff();
|
|
217
|
-
this.debugWarnLog(
|
|
218
|
-
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
261
|
+
await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
219
262
|
}
|
|
220
263
|
}
|
|
221
264
|
|
|
222
265
|
async BLERefreshStatus(): Promise<void> {
|
|
223
|
-
this.debugLog(
|
|
266
|
+
await this.debugLog('BLERefreshStatus');
|
|
224
267
|
const switchbot = await this.switchbotBLE();
|
|
225
268
|
|
|
226
269
|
if (switchbot === undefined) {
|
|
@@ -229,13 +272,13 @@ export class Hub extends deviceBase {
|
|
|
229
272
|
// Start to monitor advertisement packets
|
|
230
273
|
(async () => {
|
|
231
274
|
// Start to monitor advertisement packets
|
|
232
|
-
const serviceData
|
|
275
|
+
const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as hub2ServiceData;
|
|
233
276
|
// Update HomeKit
|
|
234
|
-
if (serviceData.model
|
|
277
|
+
if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
|
|
235
278
|
await this.BLEparseStatus(serviceData);
|
|
236
279
|
await this.updateHomeKitCharacteristics();
|
|
237
280
|
} else {
|
|
238
|
-
this.errorLog(
|
|
281
|
+
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
|
|
239
282
|
await this.BLERefreshConnection(switchbot);
|
|
240
283
|
}
|
|
241
284
|
})();
|
|
@@ -243,61 +286,39 @@ export class Hub extends deviceBase {
|
|
|
243
286
|
}
|
|
244
287
|
|
|
245
288
|
async openAPIRefreshStatus(): Promise<void> {
|
|
246
|
-
this.debugLog(
|
|
289
|
+
await this.debugLog('openAPIRefreshStatus');
|
|
247
290
|
try {
|
|
248
291
|
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
249
292
|
const deviceStatus: any = await body.json();
|
|
250
|
-
await this.
|
|
251
|
-
if (
|
|
252
|
-
await this.
|
|
253
|
-
await this.openAPIparseStatus(deviceStatus);
|
|
293
|
+
await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
|
|
294
|
+
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
295
|
+
await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
296
|
+
await this.openAPIparseStatus(deviceStatus.body);
|
|
254
297
|
await this.updateHomeKitCharacteristics();
|
|
255
298
|
} else {
|
|
256
|
-
await this.
|
|
299
|
+
await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
300
|
+
await this.debugWarnLog(statusCode, deviceStatus);
|
|
257
301
|
}
|
|
258
302
|
} catch (e: any) {
|
|
259
|
-
this.apiError(e);
|
|
260
|
-
this.errorLog(
|
|
261
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
303
|
+
await this.apiError(e);
|
|
304
|
+
await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
262
305
|
}
|
|
263
306
|
}
|
|
264
307
|
|
|
265
|
-
async registerWebhook(
|
|
266
|
-
if (device.webhook) {
|
|
267
|
-
this.debugLog(
|
|
268
|
-
this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
|
|
308
|
+
async registerWebhook(): Promise<void> {
|
|
309
|
+
if (this.device.webhook) {
|
|
310
|
+
await this.debugLog('is listening webhook.');
|
|
311
|
+
this.platform.webhookEventHandler[this.device.deviceId] = async (context: hub2WebhookContext) => {
|
|
269
312
|
try {
|
|
270
|
-
this.debugLog(
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
const { CurrentAmbientLightLevel } = this.LightSensor || { CurrentAmbientLightLevel: undefined };
|
|
274
|
-
const { CurrentRelativeHumidity } = this.HumiditySensor || { CurrentRelativeHumidity: undefined };
|
|
275
|
-
if (context.scale !== 'CELCIUS' && device.hub?.convertUnitTo === undefined) {
|
|
276
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} received a non-CELCIUS Webhook scale: `
|
|
277
|
-
+ `${context.scale}, Use the *convertUnitsTo* config under Hub settings, if displaying incorrectly in HomeKit.`);
|
|
278
|
-
}
|
|
279
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
|
|
280
|
-
'(scale, temperature, humidity, lightLevel) = ' +
|
|
281
|
-
`Webhook:(${context.scale}, ${convertUnits(temperature, context.scale, device.hub?.convertUnitTo)}, ${humidity}, ${lightLevel}), `
|
|
282
|
-
+ `current:(${CurrentTemperature}, ${CurrentRelativeHumidity}, ${CurrentAmbientLightLevel})`);
|
|
283
|
-
if (!this.device.hub?.hide_humidity) {
|
|
284
|
-
this.HumiditySensor!.CurrentRelativeHumidity = humidity;
|
|
285
|
-
}
|
|
286
|
-
if (!this.device.hub?.hide_temperature) {
|
|
287
|
-
this.TemperatureSensor!.CurrentTemperature = convertUnits(temperature, context.scale, device.hub?.convertUnitTo);
|
|
288
|
-
}
|
|
289
|
-
if (!this.device.hub?.hide_lightsensor) {
|
|
290
|
-
const set_minLux = this.device.curtain?.set_minLux ?? 1;
|
|
291
|
-
const set_maxLux = this.device.curtain?.set_maxLux ?? 6001;
|
|
292
|
-
const spaceBetweenLevels = 19;
|
|
293
|
-
await this.getLightLevel(lightLevel, set_minLux, set_maxLux, spaceBetweenLevels);
|
|
294
|
-
}
|
|
295
|
-
this.updateHomeKitCharacteristics();
|
|
313
|
+
await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
|
|
314
|
+
await this.parseStatusWebhook(context);
|
|
315
|
+
await this.updateHomeKitCharacteristics();
|
|
296
316
|
} catch (e: any) {
|
|
297
|
-
this.errorLog(
|
|
298
|
-
+ `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
317
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
299
318
|
}
|
|
300
319
|
};
|
|
320
|
+
}else {
|
|
321
|
+
await this.debugLog('is not listening webhook.');
|
|
301
322
|
}
|
|
302
323
|
}
|
|
303
324
|
|
|
@@ -306,198 +327,56 @@ export class Hub extends deviceBase {
|
|
|
306
327
|
*/
|
|
307
328
|
|
|
308
329
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
309
|
-
const mqttmessage: string[] = [];
|
|
310
|
-
const entry = { time: Math.round(new Date().valueOf() / 1000) };
|
|
311
|
-
|
|
312
330
|
// CurrentRelativeHumidity
|
|
313
|
-
if (!this.device.hub?.hide_humidity) {
|
|
314
|
-
|
|
315
|
-
this.
|
|
316
|
-
+ ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
|
|
317
|
-
} else {
|
|
318
|
-
if (this.device.mqttURL) {
|
|
319
|
-
mqttmessage.push(`"humidity": ${this.HumiditySensor!.CurrentRelativeHumidity}`);
|
|
320
|
-
}
|
|
321
|
-
if (this.device.history) {
|
|
322
|
-
entry['humidity'] = this.HumiditySensor!.CurrentRelativeHumidity;
|
|
323
|
-
}
|
|
324
|
-
this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity;
|
|
325
|
-
this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
|
|
326
|
-
this.HumiditySensor!.CurrentRelativeHumidity);
|
|
327
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
328
|
-
+ `updateCharacteristic CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
|
|
329
|
-
}
|
|
331
|
+
if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
|
|
332
|
+
await this.updateCharacteristic(this.HumiditySensor.Service, this.hap.Characteristic.CurrentRelativeHumidity,
|
|
333
|
+
this.HumiditySensor.CurrentRelativeHumidity, 'CurrentRelativeHumidity');
|
|
330
334
|
}
|
|
331
|
-
|
|
332
335
|
// CurrentTemperature
|
|
333
|
-
if (!this.device.hub?.hide_temperature) {
|
|
334
|
-
|
|
335
|
-
this.
|
|
336
|
-
} else {
|
|
337
|
-
if (this.device.mqttURL) {
|
|
338
|
-
mqttmessage.push(`"temperature": ${this.TemperatureSensor!.CurrentTemperature}`);
|
|
339
|
-
}
|
|
340
|
-
if (this.device.history) {
|
|
341
|
-
entry['temp'] = this.TemperatureSensor!.CurrentTemperature;
|
|
342
|
-
}
|
|
343
|
-
this.accessory.context.CurrentTemperature = this.TemperatureSensor!.CurrentTemperature;
|
|
344
|
-
this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor!.CurrentTemperature);
|
|
345
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
346
|
-
+ ` CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
|
|
347
|
-
}
|
|
336
|
+
if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
|
|
337
|
+
await this.updateCharacteristic(this.TemperatureSensor.Service, this.hap.Characteristic.CurrentTemperature,
|
|
338
|
+
this.TemperatureSensor.CurrentTemperature, 'CurrentTemperature');
|
|
348
339
|
}
|
|
349
|
-
|
|
350
340
|
// CurrentAmbientLightLevel
|
|
351
|
-
if (!this.device.hub?.hide_lightsensor) {
|
|
352
|
-
|
|
353
|
-
this.
|
|
354
|
-
+ ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
355
|
-
} else {
|
|
356
|
-
if (this.device.mqttURL) {
|
|
357
|
-
mqttmessage.push(`"light": ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
358
|
-
}
|
|
359
|
-
if (this.device.history) {
|
|
360
|
-
entry['lux'] = this.LightSensor!.CurrentAmbientLightLevel;
|
|
361
|
-
}
|
|
362
|
-
this.accessory.context.CurrentAmbientLightLevel = this.LightSensor!.CurrentAmbientLightLevel;
|
|
363
|
-
this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor!.CurrentAmbientLightLevel);
|
|
364
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
365
|
-
+ `updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// MQTT
|
|
370
|
-
if (this.device.mqttURL) {
|
|
371
|
-
this.mqttPublish(`{${mqttmessage.join(',')}}`);
|
|
372
|
-
}
|
|
373
|
-
if (Number(this.HumiditySensor!.CurrentRelativeHumidity) > 0) {
|
|
374
|
-
// reject unreliable data
|
|
375
|
-
if (this.device.history) {
|
|
376
|
-
this.historyService?.addEntry(entry);
|
|
377
|
-
}
|
|
341
|
+
if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
|
|
342
|
+
await this.updateCharacteristic(this.LightSensor.Service, this.hap.Characteristic.CurrentAmbientLightLevel,
|
|
343
|
+
this.LightSensor.CurrentAmbientLightLevel, 'CurrentAmbientLightLevel');
|
|
378
344
|
}
|
|
379
345
|
}
|
|
380
346
|
|
|
381
347
|
async BLERefreshConnection(switchbot: any): Promise<void> {
|
|
382
|
-
this.errorLog(
|
|
383
|
-
+ ` ${JSON.stringify(switchbot)}`);
|
|
348
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
|
|
384
349
|
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
385
|
-
this.warnLog(
|
|
350
|
+
await this.warnLog('Using OpenAPI Connection to Refresh Status');
|
|
386
351
|
await this.openAPIRefreshStatus();
|
|
387
352
|
}
|
|
388
353
|
}
|
|
389
354
|
|
|
390
355
|
async offlineOff(): Promise<void> {
|
|
391
356
|
if (this.device.offline) {
|
|
392
|
-
if (!this.device.hub?.hide_temperature) {
|
|
393
|
-
this.TemperatureSensor
|
|
357
|
+
if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
|
|
358
|
+
this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.accessory.context.CurrentTemperature);
|
|
394
359
|
}
|
|
395
|
-
if (!this.device.hub?.hide_humidity) {
|
|
396
|
-
this.HumiditySensor
|
|
360
|
+
if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
|
|
361
|
+
this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
|
|
397
362
|
this.accessory.context.CurrentRelativeHumidity);
|
|
398
363
|
}
|
|
399
|
-
if (!this.device.hub?.hide_lightsensor) {
|
|
400
|
-
this.LightSensor
|
|
364
|
+
if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
|
|
365
|
+
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel,
|
|
401
366
|
this.accessory.context.CurrentAmbientLightLevel);
|
|
402
367
|
}
|
|
403
368
|
}
|
|
404
369
|
}
|
|
405
370
|
|
|
406
371
|
async apiError(e: any): Promise<void> {
|
|
407
|
-
if (!this.device.hub?.hide_temperature) {
|
|
408
|
-
this.TemperatureSensor
|
|
409
|
-
}
|
|
410
|
-
if (!this.device.hub?.hide_humidity) {
|
|
411
|
-
this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
|
|
372
|
+
if (!this.device.hub?.hide_temperature && this.TemperatureSensor?.Service) {
|
|
373
|
+
this.TemperatureSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
|
|
412
374
|
}
|
|
413
|
-
if (!this.device.hub?.
|
|
414
|
-
this.
|
|
375
|
+
if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
|
|
376
|
+
this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
|
|
415
377
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
async getLightLevel(lightLevel: any, set_minLux: number, set_maxLux: number, spaceBetweenLevels: number) {
|
|
419
|
-
switch (lightLevel) {
|
|
420
|
-
case 1:
|
|
421
|
-
this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
|
|
422
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
423
|
-
break;
|
|
424
|
-
case 2:
|
|
425
|
-
this.LightSensor!.CurrentAmbientLightLevel = (set_maxLux - set_minLux) / spaceBetweenLevels;
|
|
426
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel},`
|
|
427
|
-
+ ` Calculation: ${(set_maxLux - set_minLux) / spaceBetweenLevels}`);
|
|
428
|
-
break;
|
|
429
|
-
case 3:
|
|
430
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 2;
|
|
431
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
432
|
-
break;
|
|
433
|
-
case 4:
|
|
434
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 3;
|
|
435
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
436
|
-
break;
|
|
437
|
-
case 5:
|
|
438
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 4;
|
|
439
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
440
|
-
break;
|
|
441
|
-
case 6:
|
|
442
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 5;
|
|
443
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
444
|
-
break;
|
|
445
|
-
case 7:
|
|
446
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 6;
|
|
447
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
448
|
-
break;
|
|
449
|
-
case 8:
|
|
450
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 7;
|
|
451
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
452
|
-
break;
|
|
453
|
-
case 9:
|
|
454
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 8;
|
|
455
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
456
|
-
break;
|
|
457
|
-
case 10:
|
|
458
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 9;
|
|
459
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
460
|
-
break;
|
|
461
|
-
case 11:
|
|
462
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 10;
|
|
463
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
464
|
-
break;
|
|
465
|
-
case 12:
|
|
466
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 11;
|
|
467
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
468
|
-
break;
|
|
469
|
-
case 13:
|
|
470
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 12;
|
|
471
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
472
|
-
break;
|
|
473
|
-
case 14:
|
|
474
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 13;
|
|
475
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
476
|
-
break;
|
|
477
|
-
case 15:
|
|
478
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 14;
|
|
479
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
480
|
-
break;
|
|
481
|
-
case 16:
|
|
482
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 15;
|
|
483
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
484
|
-
break;
|
|
485
|
-
case 17:
|
|
486
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 16;
|
|
487
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
488
|
-
break;
|
|
489
|
-
case 18:
|
|
490
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 17;
|
|
491
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
492
|
-
break;
|
|
493
|
-
case 19:
|
|
494
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 18;
|
|
495
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
496
|
-
break;
|
|
497
|
-
case 20:
|
|
498
|
-
default:
|
|
499
|
-
this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
|
|
500
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
378
|
+
if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
|
|
379
|
+
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
|
|
501
380
|
}
|
|
502
381
|
}
|
|
503
382
|
}
|