@switchbot/homebridge-switchbot 3.5.1-beta.2 → 3.5.1-beta.4
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/dist/device/blindtilt.d.ts +15 -6
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +203 -255
- 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 +196 -218
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +9 -4
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +202 -247
- 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 +329 -378
- 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 +99 -142
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +19 -7
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +203 -264
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +28 -32
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +186 -225
- 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 +198 -227
- 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 +106 -231
- 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 +192 -232
- 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 +91 -157
- 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 +287 -367
- 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 +171 -216
- 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 +93 -158
- 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 +94 -155
- 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 +84 -142
- 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 +99 -124
- 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 +170 -253
- 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 +102 -128
- 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.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.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.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.map +1 -1
- package/dist/irdevice/irdevice.d.ts +2 -1
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +1 -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.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.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.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.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.map +1 -1
- package/dist/platform.d.ts +5 -7
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +51 -34
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +49 -191
- 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 +10 -10
- package/src/device/blindtilt.ts +224 -264
- package/src/device/bot.ts +208 -221
- package/src/device/ceilinglight.ts +210 -257
- package/src/device/colorbulb.ts +334 -397
- package/src/device/contact.ts +111 -146
- package/src/device/curtain.ts +220 -283
- package/src/device/device.ts +194 -245
- package/src/device/fan.ts +214 -230
- package/src/device/hub.ts +126 -246
- package/src/device/humidifier.ts +216 -243
- package/src/device/iosensor.ts +114 -166
- package/src/device/lightstrip.ts +302 -377
- package/src/device/lock.ts +187 -221
- package/src/device/meter.ts +116 -177
- package/src/device/meterplus.ts +117 -174
- package/src/device/motion.ts +99 -144
- package/src/device/plug.ts +110 -128
- package/src/device/robotvacuumcleaner.ts +189 -252
- package/src/device/waterdetector.ts +120 -138
- package/src/irdevice/airconditioner.ts +2 -1
- package/src/irdevice/airpurifier.ts +2 -1
- package/src/irdevice/camera.ts +2 -1
- package/src/irdevice/fan.ts +2 -1
- package/src/irdevice/irdevice.ts +3 -2
- package/src/irdevice/light.ts +2 -1
- package/src/irdevice/other.ts +2 -1
- package/src/irdevice/tv.ts +2 -1
- package/src/irdevice/vacuumcleaner.ts +2 -1
- package/src/irdevice/waterheater.ts +2 -1
- package/src/platform.ts +52 -34
- package/src/settings.ts +30 -239
- 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,60 +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
303
|
await this.apiError(e);
|
|
260
|
-
await this.
|
|
304
|
+
await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
261
305
|
}
|
|
262
306
|
}
|
|
263
307
|
|
|
264
|
-
async registerWebhook(
|
|
265
|
-
if (device.webhook) {
|
|
266
|
-
this.debugLog(
|
|
267
|
-
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) => {
|
|
268
312
|
try {
|
|
269
|
-
this.debugLog(
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const { CurrentAmbientLightLevel } = this.LightSensor || { CurrentAmbientLightLevel: undefined };
|
|
273
|
-
const { CurrentRelativeHumidity } = this.HumiditySensor || { CurrentRelativeHumidity: undefined };
|
|
274
|
-
if (context.scale !== 'CELCIUS' && device.hub?.convertUnitTo === undefined) {
|
|
275
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} received a non-CELCIUS Webhook scale: `
|
|
276
|
-
+ `${context.scale}, Use the *convertUnitsTo* config under Hub settings, if displaying incorrectly in HomeKit.`);
|
|
277
|
-
}
|
|
278
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
|
|
279
|
-
'(scale, temperature, humidity, lightLevel) = ' +
|
|
280
|
-
`Webhook:(${context.scale}, ${convertUnits(temperature, context.scale, device.hub?.convertUnitTo)}, ${humidity}, ${lightLevel}), `
|
|
281
|
-
+ `current:(${CurrentTemperature}, ${CurrentRelativeHumidity}, ${CurrentAmbientLightLevel})`);
|
|
282
|
-
if (!this.device.hub?.hide_humidity) {
|
|
283
|
-
this.HumiditySensor!.CurrentRelativeHumidity = humidity;
|
|
284
|
-
}
|
|
285
|
-
if (!this.device.hub?.hide_temperature) {
|
|
286
|
-
this.TemperatureSensor!.CurrentTemperature = convertUnits(temperature, context.scale, device.hub?.convertUnitTo);
|
|
287
|
-
}
|
|
288
|
-
if (!this.device.hub?.hide_lightsensor) {
|
|
289
|
-
const set_minLux = this.device.curtain?.set_minLux ?? 1;
|
|
290
|
-
const set_maxLux = this.device.curtain?.set_maxLux ?? 6001;
|
|
291
|
-
const spaceBetweenLevels = 19;
|
|
292
|
-
await this.getLightLevel(lightLevel, set_minLux, set_maxLux, spaceBetweenLevels);
|
|
293
|
-
}
|
|
294
|
-
this.updateHomeKitCharacteristics();
|
|
313
|
+
await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
|
|
314
|
+
await this.parseStatusWebhook(context);
|
|
315
|
+
await this.updateHomeKitCharacteristics();
|
|
295
316
|
} catch (e: any) {
|
|
296
|
-
this.errorLog(
|
|
297
|
-
+ `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
317
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
298
318
|
}
|
|
299
319
|
};
|
|
320
|
+
}else {
|
|
321
|
+
await this.debugLog('is not listening webhook.');
|
|
300
322
|
}
|
|
301
323
|
}
|
|
302
324
|
|
|
@@ -305,198 +327,56 @@ export class Hub extends deviceBase {
|
|
|
305
327
|
*/
|
|
306
328
|
|
|
307
329
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
308
|
-
const mqttmessage: string[] = [];
|
|
309
|
-
const entry = { time: Math.round(new Date().valueOf() / 1000) };
|
|
310
|
-
|
|
311
330
|
// CurrentRelativeHumidity
|
|
312
|
-
if (!this.device.hub?.hide_humidity) {
|
|
313
|
-
|
|
314
|
-
this.
|
|
315
|
-
+ ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
|
|
316
|
-
} else {
|
|
317
|
-
if (this.device.mqttURL) {
|
|
318
|
-
mqttmessage.push(`"humidity": ${this.HumiditySensor!.CurrentRelativeHumidity}`);
|
|
319
|
-
}
|
|
320
|
-
if (this.device.history) {
|
|
321
|
-
entry['humidity'] = this.HumiditySensor!.CurrentRelativeHumidity;
|
|
322
|
-
}
|
|
323
|
-
this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity;
|
|
324
|
-
this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
|
|
325
|
-
this.HumiditySensor!.CurrentRelativeHumidity);
|
|
326
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
327
|
-
+ `updateCharacteristic CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
|
|
328
|
-
}
|
|
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');
|
|
329
334
|
}
|
|
330
|
-
|
|
331
335
|
// CurrentTemperature
|
|
332
|
-
if (!this.device.hub?.hide_temperature) {
|
|
333
|
-
|
|
334
|
-
this.
|
|
335
|
-
} else {
|
|
336
|
-
if (this.device.mqttURL) {
|
|
337
|
-
mqttmessage.push(`"temperature": ${this.TemperatureSensor!.CurrentTemperature}`);
|
|
338
|
-
}
|
|
339
|
-
if (this.device.history) {
|
|
340
|
-
entry['temp'] = this.TemperatureSensor!.CurrentTemperature;
|
|
341
|
-
}
|
|
342
|
-
this.accessory.context.CurrentTemperature = this.TemperatureSensor!.CurrentTemperature;
|
|
343
|
-
this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor!.CurrentTemperature);
|
|
344
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
345
|
-
+ ` CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
|
|
346
|
-
}
|
|
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');
|
|
347
339
|
}
|
|
348
|
-
|
|
349
340
|
// CurrentAmbientLightLevel
|
|
350
|
-
if (!this.device.hub?.hide_lightsensor) {
|
|
351
|
-
|
|
352
|
-
this.
|
|
353
|
-
+ ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
354
|
-
} else {
|
|
355
|
-
if (this.device.mqttURL) {
|
|
356
|
-
mqttmessage.push(`"light": ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
357
|
-
}
|
|
358
|
-
if (this.device.history) {
|
|
359
|
-
entry['lux'] = this.LightSensor!.CurrentAmbientLightLevel;
|
|
360
|
-
}
|
|
361
|
-
this.accessory.context.CurrentAmbientLightLevel = this.LightSensor!.CurrentAmbientLightLevel;
|
|
362
|
-
this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor!.CurrentAmbientLightLevel);
|
|
363
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
364
|
-
+ `updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
// MQTT
|
|
369
|
-
if (this.device.mqttURL) {
|
|
370
|
-
this.mqttPublish(`{${mqttmessage.join(',')}}`);
|
|
371
|
-
}
|
|
372
|
-
if (Number(this.HumiditySensor!.CurrentRelativeHumidity) > 0) {
|
|
373
|
-
// reject unreliable data
|
|
374
|
-
if (this.device.history) {
|
|
375
|
-
this.historyService?.addEntry(entry);
|
|
376
|
-
}
|
|
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');
|
|
377
344
|
}
|
|
378
345
|
}
|
|
379
346
|
|
|
380
347
|
async BLERefreshConnection(switchbot: any): Promise<void> {
|
|
381
|
-
this.errorLog(
|
|
382
|
-
+ ` ${JSON.stringify(switchbot)}`);
|
|
348
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
|
|
383
349
|
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
384
|
-
this.warnLog(
|
|
350
|
+
await this.warnLog('Using OpenAPI Connection to Refresh Status');
|
|
385
351
|
await this.openAPIRefreshStatus();
|
|
386
352
|
}
|
|
387
353
|
}
|
|
388
354
|
|
|
389
355
|
async offlineOff(): Promise<void> {
|
|
390
356
|
if (this.device.offline) {
|
|
391
|
-
if (!this.device.hub?.hide_temperature) {
|
|
392
|
-
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);
|
|
393
359
|
}
|
|
394
|
-
if (!this.device.hub?.hide_humidity) {
|
|
395
|
-
this.HumiditySensor
|
|
360
|
+
if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
|
|
361
|
+
this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
|
|
396
362
|
this.accessory.context.CurrentRelativeHumidity);
|
|
397
363
|
}
|
|
398
|
-
if (!this.device.hub?.hide_lightsensor) {
|
|
399
|
-
this.LightSensor
|
|
364
|
+
if (!this.device.hub?.hide_lightsensor && this.LightSensor?.Service) {
|
|
365
|
+
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel,
|
|
400
366
|
this.accessory.context.CurrentAmbientLightLevel);
|
|
401
367
|
}
|
|
402
368
|
}
|
|
403
369
|
}
|
|
404
370
|
|
|
405
371
|
async apiError(e: any): Promise<void> {
|
|
406
|
-
if (!this.device.hub?.hide_temperature) {
|
|
407
|
-
this.TemperatureSensor
|
|
408
|
-
}
|
|
409
|
-
if (!this.device.hub?.hide_humidity) {
|
|
410
|
-
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);
|
|
411
374
|
}
|
|
412
|
-
if (!this.device.hub?.
|
|
413
|
-
this.
|
|
375
|
+
if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
|
|
376
|
+
this.HumiditySensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
|
|
414
377
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
async getLightLevel(lightLevel: any, set_minLux: number, set_maxLux: number, spaceBetweenLevels: number) {
|
|
418
|
-
switch (lightLevel) {
|
|
419
|
-
case 1:
|
|
420
|
-
this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
|
|
421
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
422
|
-
break;
|
|
423
|
-
case 2:
|
|
424
|
-
this.LightSensor!.CurrentAmbientLightLevel = (set_maxLux - set_minLux) / spaceBetweenLevels;
|
|
425
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel},`
|
|
426
|
-
+ ` Calculation: ${(set_maxLux - set_minLux) / spaceBetweenLevels}`);
|
|
427
|
-
break;
|
|
428
|
-
case 3:
|
|
429
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 2;
|
|
430
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
431
|
-
break;
|
|
432
|
-
case 4:
|
|
433
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 3;
|
|
434
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
435
|
-
break;
|
|
436
|
-
case 5:
|
|
437
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 4;
|
|
438
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
439
|
-
break;
|
|
440
|
-
case 6:
|
|
441
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 5;
|
|
442
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
443
|
-
break;
|
|
444
|
-
case 7:
|
|
445
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 6;
|
|
446
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
447
|
-
break;
|
|
448
|
-
case 8:
|
|
449
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 7;
|
|
450
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
451
|
-
break;
|
|
452
|
-
case 9:
|
|
453
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 8;
|
|
454
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
455
|
-
break;
|
|
456
|
-
case 10:
|
|
457
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 9;
|
|
458
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
459
|
-
break;
|
|
460
|
-
case 11:
|
|
461
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 10;
|
|
462
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
463
|
-
break;
|
|
464
|
-
case 12:
|
|
465
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 11;
|
|
466
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
467
|
-
break;
|
|
468
|
-
case 13:
|
|
469
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 12;
|
|
470
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
471
|
-
break;
|
|
472
|
-
case 14:
|
|
473
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 13;
|
|
474
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
475
|
-
break;
|
|
476
|
-
case 15:
|
|
477
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 14;
|
|
478
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
479
|
-
break;
|
|
480
|
-
case 16:
|
|
481
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 15;
|
|
482
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
483
|
-
break;
|
|
484
|
-
case 17:
|
|
485
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 16;
|
|
486
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
487
|
-
break;
|
|
488
|
-
case 18:
|
|
489
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 17;
|
|
490
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
491
|
-
break;
|
|
492
|
-
case 19:
|
|
493
|
-
this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 18;
|
|
494
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
|
|
495
|
-
break;
|
|
496
|
-
case 20:
|
|
497
|
-
default:
|
|
498
|
-
this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
|
|
499
|
-
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);
|
|
500
380
|
}
|
|
501
381
|
}
|
|
502
382
|
}
|