@switchbot/homebridge-switchbot 3.5.1-beta.13 → 3.5.1-beta.15

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.
Files changed (121) hide show
  1. package/dist/device/blindtilt.d.ts.map +1 -1
  2. package/dist/device/blindtilt.js +3 -1
  3. package/dist/device/blindtilt.js.map +1 -1
  4. package/dist/device/bot.d.ts.map +1 -1
  5. package/dist/device/bot.js +21 -1
  6. package/dist/device/bot.js.map +1 -1
  7. package/dist/device/ceilinglight.d.ts.map +1 -1
  8. package/dist/device/ceilinglight.js +3 -1
  9. package/dist/device/ceilinglight.js.map +1 -1
  10. package/dist/device/colorbulb.d.ts.map +1 -1
  11. package/dist/device/colorbulb.js +3 -1
  12. package/dist/device/colorbulb.js.map +1 -1
  13. package/dist/device/contact.d.ts.map +1 -1
  14. package/dist/device/contact.js +3 -1
  15. package/dist/device/contact.js.map +1 -1
  16. package/dist/device/curtain.d.ts.map +1 -1
  17. package/dist/device/curtain.js +3 -1
  18. package/dist/device/curtain.js.map +1 -1
  19. package/dist/device/device.d.ts +0 -8
  20. package/dist/device/device.d.ts.map +1 -1
  21. package/dist/device/device.js +9 -195
  22. package/dist/device/device.js.map +1 -1
  23. package/dist/device/fan.d.ts.map +1 -1
  24. package/dist/device/fan.js +3 -1
  25. package/dist/device/fan.js.map +1 -1
  26. package/dist/device/hub.d.ts.map +1 -1
  27. package/dist/device/hub.js +3 -1
  28. package/dist/device/hub.js.map +1 -1
  29. package/dist/device/humidifier.d.ts.map +1 -1
  30. package/dist/device/humidifier.js +3 -1
  31. package/dist/device/humidifier.js.map +1 -1
  32. package/dist/device/iosensor.d.ts.map +1 -1
  33. package/dist/device/iosensor.js +3 -1
  34. package/dist/device/iosensor.js.map +1 -1
  35. package/dist/device/lightstrip.d.ts.map +1 -1
  36. package/dist/device/lightstrip.js +3 -1
  37. package/dist/device/lightstrip.js.map +1 -1
  38. package/dist/device/lock.d.ts.map +1 -1
  39. package/dist/device/lock.js +3 -1
  40. package/dist/device/lock.js.map +1 -1
  41. package/dist/device/meter.d.ts.map +1 -1
  42. package/dist/device/meter.js +3 -1
  43. package/dist/device/meter.js.map +1 -1
  44. package/dist/device/meterplus.d.ts.map +1 -1
  45. package/dist/device/meterplus.js +3 -1
  46. package/dist/device/meterplus.js.map +1 -1
  47. package/dist/device/motion.d.ts.map +1 -1
  48. package/dist/device/motion.js +3 -1
  49. package/dist/device/motion.js.map +1 -1
  50. package/dist/device/plug.d.ts.map +1 -1
  51. package/dist/device/plug.js +3 -1
  52. package/dist/device/plug.js.map +1 -1
  53. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  54. package/dist/device/robotvacuumcleaner.js +3 -1
  55. package/dist/device/robotvacuumcleaner.js.map +1 -1
  56. package/dist/device/waterdetector.d.ts.map +1 -1
  57. package/dist/device/waterdetector.js +3 -1
  58. package/dist/device/waterdetector.js.map +1 -1
  59. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  60. package/dist/irdevice/airconditioner.js +2 -0
  61. package/dist/irdevice/airconditioner.js.map +1 -1
  62. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  63. package/dist/irdevice/airpurifier.js +2 -0
  64. package/dist/irdevice/airpurifier.js.map +1 -1
  65. package/dist/irdevice/camera.d.ts.map +1 -1
  66. package/dist/irdevice/camera.js +2 -0
  67. package/dist/irdevice/camera.js.map +1 -1
  68. package/dist/irdevice/fan.d.ts.map +1 -1
  69. package/dist/irdevice/fan.js +2 -0
  70. package/dist/irdevice/fan.js.map +1 -1
  71. package/dist/irdevice/irdevice.js +1 -1
  72. package/dist/irdevice/irdevice.js.map +1 -1
  73. package/dist/irdevice/light.d.ts.map +1 -1
  74. package/dist/irdevice/light.js +2 -0
  75. package/dist/irdevice/light.js.map +1 -1
  76. package/dist/irdevice/other.d.ts.map +1 -1
  77. package/dist/irdevice/other.js +2 -0
  78. package/dist/irdevice/other.js.map +1 -1
  79. package/dist/irdevice/tv.js +4 -4
  80. package/dist/irdevice/tv.js.map +1 -1
  81. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  82. package/dist/irdevice/vacuumcleaner.js +2 -0
  83. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  84. package/dist/irdevice/waterheater.d.ts.map +1 -1
  85. package/dist/irdevice/waterheater.js +2 -0
  86. package/dist/irdevice/waterheater.js.map +1 -1
  87. package/dist/platform.d.ts +10 -1
  88. package/dist/platform.d.ts.map +1 -1
  89. package/dist/platform.js +175 -2
  90. package/dist/platform.js.map +1 -1
  91. package/package.json +1 -1
  92. package/src/device/blindtilt.ts +4 -1
  93. package/src/device/bot.ts +32 -1
  94. package/src/device/ceilinglight.ts +4 -1
  95. package/src/device/colorbulb.ts +4 -1
  96. package/src/device/contact.ts +4 -1
  97. package/src/device/curtain.ts +3 -1
  98. package/src/device/device.ts +9 -195
  99. package/src/device/fan.ts +4 -1
  100. package/src/device/hub.ts +4 -1
  101. package/src/device/humidifier.ts +4 -1
  102. package/src/device/iosensor.ts +4 -1
  103. package/src/device/lightstrip.ts +4 -1
  104. package/src/device/lock.ts +4 -1
  105. package/src/device/meter.ts +4 -1
  106. package/src/device/meterplus.ts +4 -1
  107. package/src/device/motion.ts +4 -1
  108. package/src/device/plug.ts +4 -1
  109. package/src/device/robotvacuumcleaner.ts +4 -1
  110. package/src/device/waterdetector.ts +4 -1
  111. package/src/irdevice/airconditioner.ts +2 -1
  112. package/src/irdevice/airpurifier.ts +2 -0
  113. package/src/irdevice/camera.ts +2 -0
  114. package/src/irdevice/fan.ts +2 -0
  115. package/src/irdevice/irdevice.ts +1 -1
  116. package/src/irdevice/light.ts +2 -0
  117. package/src/irdevice/other.ts +2 -0
  118. package/src/irdevice/tv.ts +4 -4
  119. package/src/irdevice/vacuumcleaner.ts +2 -0
  120. package/src/irdevice/waterheater.ts +2 -0
  121. package/src/platform.ts +175 -2
package/src/device/bot.ts CHANGED
@@ -115,6 +115,7 @@ export class Bot extends deviceBase {
115
115
  device: device & devicesConfig,
116
116
  ) {
117
117
  super(platform, accessory, device);
118
+
118
119
  // default placeholders
119
120
  this.getBotConfigSettings(device);
120
121
 
@@ -140,6 +141,9 @@ export class Bot extends deviceBase {
140
141
 
141
142
  // deviceType
142
143
  if (this.botDeviceType === 'switch') {
144
+ // Set category
145
+ accessory.category = this.hap.Categories.SWITCH
146
+ ;
143
147
  // Initialize Switch Service
144
148
  accessory.context.Switch = accessory.context.Switch ?? {};
145
149
  this.Switch = {
@@ -171,6 +175,9 @@ export class Bot extends deviceBase {
171
175
  this.removeWindowCoveringService(accessory);
172
176
  this.removeStatefulProgrammableSwitchService(accessory);
173
177
  } else if (this.botDeviceType === 'garagedoor') {
178
+ // Set category
179
+ accessory.category = this.hap.Categories.GARAGE_DOOR_OPENER;
180
+
174
181
  // Initialize GarageDoor Service
175
182
  accessory.context.GarageDoor = accessory.context.GarageDoor ?? {};
176
183
  this.GarageDoor = {
@@ -207,6 +214,9 @@ export class Bot extends deviceBase {
207
214
  this.removeWindowCoveringService(accessory);
208
215
  this.removeStatefulProgrammableSwitchService(accessory);
209
216
  } else if (this.botDeviceType === 'door') {
217
+ // Set category
218
+ accessory.category = this.hap.Categories.DOOR;
219
+
210
220
  // Initialize Door Service
211
221
  accessory.context.Door = accessory.context.Door ?? {};
212
222
  this.Door = {
@@ -243,6 +253,9 @@ export class Bot extends deviceBase {
243
253
  this.removeWindowCoveringService(accessory);
244
254
  this.removeStatefulProgrammableSwitchService(accessory);
245
255
  } else if (this.botDeviceType === 'window') {
256
+ // Set category
257
+ accessory.category = this.hap.Categories.WINDOW;
258
+
246
259
  // Initialize Window Service
247
260
  accessory.context.Window = accessory.context.Window ?? {};
248
261
  this.Window = {
@@ -279,6 +292,9 @@ export class Bot extends deviceBase {
279
292
  this.removeWindowCoveringService(accessory);
280
293
  this.removeStatefulProgrammableSwitchService(accessory);
281
294
  } else if (this.botDeviceType === 'windowcovering') {
295
+ // Set category
296
+ accessory.category = this.hap.Categories.WINDOW_COVERING;
297
+
282
298
  // Initialize WindowCovering Service
283
299
  accessory.context.WindowCovering = accessory.context.WindowCovering ?? {};
284
300
  this.WindowCovering = {
@@ -315,6 +331,9 @@ export class Bot extends deviceBase {
315
331
  this.removeGarageDoorService(accessory);
316
332
  this.removeStatefulProgrammableSwitchService(accessory);
317
333
  } else if (this.botDeviceType === 'lock') {
334
+ // Set category
335
+ accessory.category = this.hap.Categories.DOOR_LOCK;
336
+
318
337
  // Initialize Lock Service
319
338
  accessory.context.LockMechanism = accessory.context.LockMechanism ?? {};
320
339
  this.LockMechanism = {
@@ -345,6 +364,9 @@ export class Bot extends deviceBase {
345
364
  this.removeWindowCoveringService(accessory);
346
365
  this.removeStatefulProgrammableSwitchService(accessory);
347
366
  } else if (this.botDeviceType === 'faucet') {
367
+ // Set category
368
+ accessory.category = this.hap.Categories.FAUCET;
369
+
348
370
  // Initialize Faucet Service
349
371
  accessory.context.Faucet = accessory.context.Faucet ?? {};
350
372
  this.Faucet = {
@@ -375,6 +397,9 @@ export class Bot extends deviceBase {
375
397
  this.removeWindowCoveringService(accessory);
376
398
  this.removeStatefulProgrammableSwitchService(accessory);
377
399
  } else if (this.botDeviceType === 'fan') {
400
+ // Set category
401
+ accessory.category = this.hap.Categories.FAN;
402
+
378
403
  // Initialize Fan Service
379
404
  accessory.context.Fan = accessory.context.Fan ?? {};
380
405
  this.Fan = {
@@ -405,6 +430,9 @@ export class Bot extends deviceBase {
405
430
  this.removeWindowCoveringService(accessory);
406
431
  this.removeStatefulProgrammableSwitchService(accessory);
407
432
  } else if (this.botDeviceType === 'stateful') {
433
+ // Set category
434
+ accessory.category = this.hap.Categories.PROGRAMMABLE_SWITCH;
435
+
408
436
  // Initialize StatefulProgrammableSwitch Service
409
437
  accessory.context.StatefulProgrammableSwitch = accessory.context.StatefulProgrammableSwitch ?? {};
410
438
  this.StatefulProgrammableSwitch = {
@@ -436,6 +464,9 @@ export class Bot extends deviceBase {
436
464
  this.removeGarageDoorService(accessory);
437
465
  this.removeWindowCoveringService(accessory);
438
466
  } else {
467
+ // Set category
468
+ accessory.category = this.hap.Categories.OUTLET;
469
+
439
470
  // Initialize Switch property
440
471
  accessory.context.Outlet = accessory.context.Outlet ?? {};
441
472
  this.Outlet = {
@@ -676,7 +707,7 @@ export class Bot extends deviceBase {
676
707
  async registerWebhook() {
677
708
  if (this.device.webhook) {
678
709
  await this.debugLog('is listening webhook.');
679
- this.webhookEventHandler[this.device.deviceId] = async (context: botWebhookContext) => {
710
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: botWebhookContext) => {
680
711
  try {
681
712
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
682
713
  this.webhookContext = context;
@@ -55,6 +55,9 @@ export class CeilingLight extends deviceBase {
55
55
  device: device & devicesConfig,
56
56
  ) {
57
57
  super(platform, accessory, device);
58
+ // Set category
59
+ accessory.category = this.hap.Categories.LIGHTBULB;
60
+
58
61
  // default placeholders
59
62
  this.adaptiveLighting(device);
60
63
 
@@ -331,7 +334,7 @@ export class CeilingLight extends deviceBase {
331
334
  async registerWebhook() {
332
335
  if (this.device.webhook) {
333
336
  await this.debugLog('is listening webhook.');
334
- this.webhookEventHandler[this.device.deviceId] = async (context: ceilingLightWebhookContext | ceilingLightProWebhookContext) => {
337
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: ceilingLightWebhookContext | ceilingLightProWebhookContext) => {
335
338
  try {
336
339
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
337
340
  this.webhookContext = context;
@@ -55,6 +55,9 @@ export class ColorBulb extends deviceBase {
55
55
  device: device & devicesConfig,
56
56
  ) {
57
57
  super(platform, accessory, device);
58
+ // Set category
59
+ accessory.category = this.hap.Categories.LIGHTBULB;
60
+
58
61
  // default placeholders
59
62
  this.adaptiveLighting(device);
60
63
 
@@ -352,7 +355,7 @@ export class ColorBulb extends deviceBase {
352
355
  async registerWebhook() {
353
356
  if (this.device.webhook) {
354
357
  await this.debugLog('is listening webhook.');
355
- this.webhookEventHandler[this.device.deviceId] = async (context: colorBulbWebhookContext) => {
358
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: colorBulbWebhookContext) => {
356
359
  try {
357
360
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
358
361
  this.webhookContext = context;
@@ -65,6 +65,9 @@ export class Contact extends deviceBase {
65
65
  device: device & devicesConfig,
66
66
  ) {
67
67
  super(platform, accessory, device);
68
+ // Set category
69
+ accessory.category = this.hap.Categories.SENSOR;
70
+
68
71
  // this is subject we use to track when we need to POST changes to the SwitchBot API
69
72
  this.doContactUpdate = new Subject();
70
73
  this.contactUpdateInProgress = false;
@@ -342,7 +345,7 @@ export class Contact extends deviceBase {
342
345
  async registerWebhook() {
343
346
  if (this.device.webhook) {
344
347
  await this.debugLog('is listening webhook.');
345
- this.webhookEventHandler[this.device.deviceId] = async (context: contactSensorWebhookContext) => {
348
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: contactSensorWebhookContext) => {
346
349
  try {
347
350
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
348
351
  this.webhookContext = context;
@@ -63,6 +63,8 @@ export class Curtain extends deviceBase {
63
63
  device: device & devicesConfig,
64
64
  ) {
65
65
  super(platform, accessory, device);
66
+ // Set category
67
+ accessory.category = this.hap.Categories.WINDOW_COVERING;
66
68
 
67
69
  // this is subject we use to track when we need to POST changes to the SwitchBot API
68
70
  this.doCurtainUpdate = new Subject();
@@ -483,7 +485,7 @@ export class Curtain extends deviceBase {
483
485
  async registerWebhook() {
484
486
  if (this.device.webhook) {
485
487
  await this.debugLog('is listening webhook.');
486
- this.webhookEventHandler[this.device.deviceId] = async (context: curtainWebhookContext | curtain3WebhookContext) => {
488
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: curtainWebhookContext | curtain3WebhookContext) => {
487
489
  try {
488
490
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
489
491
  this.webhookContext = context;
@@ -5,17 +5,14 @@
5
5
 
6
6
  import { hostname } from 'os';
7
7
  import { request } from 'undici';
8
- import asyncmqtt from 'async-mqtt';
9
- import { createServer } from 'http';
8
+ import { Devices } from '../settings.js';
10
9
  import { BlindTiltMappingMode, sleep } from '../utils.js';
11
- import { Devices, deleteWebhook, queryWebhook, setupWebhook, updateWebhook } from '../settings.js';
12
10
  import { SwitchBotModel, SwitchBotBLEModel, SwitchBotBLEModelName, SwitchBotBLEModelFriendlyName } from 'node-switchbot';
13
11
 
14
12
  import type { MqttClient } from 'mqtt';
15
13
  import type { device } from '../types/devicelist.js';
16
14
  import type { ad } from '../types/bledevicestatus.js';
17
15
  import type { SwitchBotPlatform } from '../platform.js';
18
- import type { Server, IncomingMessage, ServerResponse } from 'http';
19
16
  import type { SwitchBotPlatformConfig, devicesConfig } from '../settings.js';
20
17
  import type { API, CharacteristicValue, HAP, Logging, PlatformAccessory, Service } from 'homebridge';
21
18
 
@@ -41,10 +38,6 @@ export abstract class deviceBase {
41
38
  protected deviceModel!: SwitchBotModel;
42
39
  protected deviceBLEModel!: SwitchBotBLEModel;
43
40
 
44
- // Webhook
45
- protected readonly webhookEventHandler: { [x: string]: (context: any) => void } = {};
46
- protected webhookEventListener: Server | null = null;
47
-
48
41
  // MQTT
49
42
  protected deviceMqttURL!: string;
50
43
  protected deviceMqttOptions!: any;
@@ -59,8 +52,6 @@ export abstract class deviceBase {
59
52
  //MQTT stuff
60
53
  protected mqttClient: MqttClient | null = null;
61
54
 
62
-
63
-
64
55
  constructor(
65
56
  protected readonly platform: SwitchBotPlatform,
66
57
  protected accessory: PlatformAccessory,
@@ -82,8 +73,6 @@ export abstract class deviceBase {
82
73
  this.getDeviceContext(accessory, device);
83
74
  this.getDeviceScanDuration(accessory, device);
84
75
  this.getMqttSettings(device);
85
- this.setupMqtt(device);
86
- this.setupwebhook();
87
76
 
88
77
  // Set accessory information
89
78
  accessory
@@ -91,7 +80,7 @@ export abstract class deviceBase {
91
80
  .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
92
81
  .setCharacteristic(this.hap.Characteristic.AppMatchingIdentifier, 'id1087374760')
93
82
  .setCharacteristic(this.hap.Characteristic.Name, device.deviceName ?? accessory.displayName)
94
- //.setCharacteristic(this.hap.Characteristic.ConfiguredName, device.deviceName ?? accessory.displayName)
83
+ .setCharacteristic(this.hap.Characteristic.ConfiguredName, String(device.deviceName ?? accessory.displayName))
95
84
  .setCharacteristic(this.hap.Characteristic.Model, device.model ?? accessory.context.model)
96
85
  .setCharacteristic(this.hap.Characteristic.ProductData, device.deviceId ?? accessory.context.deviceId)
97
86
  .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId);
@@ -203,6 +192,12 @@ export abstract class deviceBase {
203
192
  if (device.mqttURL !== '') {
204
193
  deviceConfig['mqttURL'] = device.mqttURL;
205
194
  }
195
+ if (device.mqttOptions) {
196
+ deviceConfig['mqttOptions'] = device.mqttOptions;
197
+ }
198
+ if (device.mqttPubOptions) {
199
+ deviceConfig['mqttPubOptions'] = device.mqttPubOptions;
200
+ }
206
201
  if (device.maxRetries !== 0) {
207
202
  deviceConfig['maxRetries'] = device.maxRetries;
208
203
  }
@@ -304,7 +299,7 @@ export abstract class deviceBase {
304
299
  */
305
300
  async mqttPublish(message: string, topic?: string) {
306
301
  const mac = this.device.deviceId?.toLowerCase().match(/[\s\S]{1,2}/g)?.join(':');
307
- const options = this.device.mqttPubOptions ?? {};
302
+ const options = this.deviceMqttPubOptions ?? {};
308
303
  const mqttTopic = topic ? `/${topic}` : '';
309
304
  const mqttMessageTopic = topic ? `${topic}/` : '';
310
305
  this.mqttClient?.publish(`homebridge-switchbot/${this.device.deviceType}/${mac}${mqttTopic}`, `${message}`, options);
@@ -328,187 +323,6 @@ export abstract class deviceBase {
328
323
  + ` ${mqttPubOptions} mqttPubOptions: ${JSON.stringify(this.deviceMqttPubOptions)}`);
329
324
  }
330
325
 
331
- /*
332
- * Setup MQTT hadler if URL is specified.
333
- */
334
- async setupMqtt(device: device & devicesConfig): Promise<void> {
335
- if (device.mqttURL) {
336
- try {
337
- const { connectAsync } = asyncmqtt;
338
- this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
339
- this.debugLog('MQTT connection has been established successfully.');
340
- this.mqttClient.on('error', (e: Error) => {
341
- this.errorLog(`Failed to publish MQTT messages. ${e}`);
342
- });
343
- } catch (e) {
344
- this.mqttClient = null;
345
- this.errorLog(`Failed to establish MQTT connection. ${e}`);
346
- }
347
- }
348
- }
349
-
350
- async setupDeviceMqtt(): Promise<void> {
351
- if (this.config.options?.mqttURL) {
352
- try {
353
- const { connectAsync } = asyncmqtt;
354
- this.mqttClient = await connectAsync(this.config.options?.mqttURL, this.config.options.mqttOptions || {});
355
- await this.debugLog('MQTT connection has been established successfully.');
356
- this.mqttClient.on('error', async (e: Error) => {
357
- await this.errorLog(`Failed to publish MQTT messages. ${e}`);
358
- });
359
- if (!this.config.options?.webhookURL) {
360
- // receive webhook events via MQTT
361
- await this.infoLog(`Webhook is configured to be received through ${this.config.options.mqttURL}/homebridge-switchbot/webhook.`);
362
- this.mqttClient.subscribe('homebridge-switchbot/webhook/+');
363
- this.mqttClient.on('message', async (topic: string, message) => {
364
- try {
365
- await this.debugLog(`Received Webhook via MQTT: ${topic}=${message}`);
366
- const context = JSON.parse(message.toString());
367
- this.webhookEventHandler[context.deviceMac]?.(context);
368
- } catch (e: any) {
369
- await this.errorLog(`Failed to handle webhook event. Error:${e}`);
370
- }
371
- });
372
- }
373
- } catch (e) {
374
- this.mqttClient = null;
375
- await this.errorLog(`Failed to establish MQTT connection. ${e}`);
376
- }
377
- }
378
- }
379
-
380
- async setupwebhook() {
381
- //webhook configuration
382
- if (this.config.options?.webhookURL) {
383
- const url = this.config.options?.webhookURL;
384
-
385
- try {
386
- const xurl = new URL(url);
387
- const port = Number(xurl.port);
388
- const path = xurl.pathname;
389
- this.webhookEventListener = createServer((request: IncomingMessage, response: ServerResponse) => {
390
- try {
391
- if (request.url === path && request.method === 'POST') {
392
- request.on('data', async (data) => {
393
- try {
394
- const body = JSON.parse(data);
395
- await this.debugLog(`Received Webhook: ${JSON.stringify(body)}`);
396
- if (this.config.options?.mqttURL) {
397
- const mac = body.context.deviceMac
398
- ?.toLowerCase()
399
- .match(/[\s\S]{1,2}/g)
400
- ?.join(':');
401
- const options = this.config.options?.mqttPubOptions || {};
402
- this.mqttClient?.publish(`homebridge-switchbot/webhook/${mac}`, `${JSON.stringify(body.context)}`, options);
403
- }
404
- this.webhookEventHandler[body.context.deviceMac]?.(body.context);
405
- } catch (e: any) {
406
- this.errorLog(`Failed to handle webhook event. Error:${e}`);
407
- }
408
- });
409
- response.writeHead(200, { 'Content-Type': 'text/plain' });
410
- response.end('OK');
411
- }
412
- // else {
413
- // response.writeHead(403, {'Content-Type': 'text/plain'});
414
- // response.end(`NG`);
415
- // }
416
- } catch (e: any) {
417
- this.errorLog(`Failed to handle webhook event. Error:${e}`);
418
- }
419
- }).listen(port ? port : 80);
420
- } catch (e: any) {
421
- this.errorLog(`Failed to create webhook listener. Error:${e.message}`);
422
- return;
423
- }
424
-
425
- try {
426
- const { body, statusCode } = await request(setupWebhook, {
427
- method: 'POST',
428
- headers: this.platform.generateHeaders(),
429
- body: JSON.stringify({
430
- 'action': 'setupWebhook',
431
- 'url': url,
432
- 'deviceList': 'ALL',
433
- }),
434
- });
435
- const response: any = await body.json();
436
- await this.debugLog(`setupWebhook: url:${url}`);
437
- await this.debugLog(`setupWebhook: body:${JSON.stringify(response)}`);
438
- await this.debugLog(`setupWebhook: statusCode:${statusCode}`);
439
- if (statusCode !== 200 || response?.statusCode !== 100) {
440
- this.errorLog(`Failed to configure webhook. Existing webhook well be overridden. HTTP:${statusCode} API:${response?.statusCode} `
441
- + `message:${response?.message}`);
442
- }
443
- } catch (e: any) {
444
- this.errorLog(`Failed to configure webhook. Error: ${e.message}`);
445
- }
446
-
447
- try {
448
- const { body, statusCode } = await request(updateWebhook, {
449
- method: 'POST', headers: this.platform.generateHeaders(), body: JSON.stringify({
450
- 'action': 'updateWebhook',
451
- 'config': {
452
- 'url': url,
453
- 'enable': true,
454
- },
455
- }),
456
- });
457
- const response: any = await body.json();
458
- await this.debugLog(`updateWebhook: url:${url}`);
459
- await this.debugLog(`updateWebhook: body:${JSON.stringify(response)}`);
460
- await this.debugLog(`updateWebhook: statusCode:${statusCode}`);
461
- if (statusCode !== 200 || response?.statusCode !== 100) {
462
- this.errorLog(`Failed to update webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
463
- }
464
- } catch (e: any) {
465
- this.errorLog(`Failed to update webhook. Error:${e.message}`);
466
- }
467
-
468
- try {
469
- const { body, statusCode } = await request(queryWebhook, {
470
- method: 'POST',
471
- headers: this.platform.generateHeaders(),
472
- body: JSON.stringify({
473
- 'action': 'queryUrl',
474
- }),
475
- });
476
- const response: any = await body.json();
477
- await this.debugLog(`queryWebhook: body:${JSON.stringify(response)}`);
478
- await this.debugLog(`queryWebhook: statusCode:${statusCode}`);
479
- if (statusCode !== 200 || response?.statusCode !== 100) {
480
- this.errorLog(`Failed to query webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
481
- } else {
482
- this.infoLog(`Listening webhook on ${response?.body?.urls[0]}`);
483
- }
484
- } catch (e: any) {
485
- this.errorLog(`Failed to query webhook. Error:${e}`);
486
- }
487
-
488
- this.api.on('shutdown', async () => {
489
- try {
490
- const { body, statusCode } = await request(deleteWebhook, {
491
- method: 'POST',
492
- headers: this.platform.generateHeaders(),
493
- body: JSON.stringify({
494
- 'action': 'deleteWebhook',
495
- 'url': url,
496
- }),
497
- });
498
- const response: any = await body.json();
499
- await this.debugLog(`deleteWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
500
- if (statusCode !== 200 || response?.statusCode !== 100) {
501
- this.errorLog(`Failed to delete webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
502
- } else {
503
- this.infoLog('Unregistered webhook to close listening.');
504
- }
505
- } catch (e: any) {
506
- this.errorLog(`Failed to delete webhook. Error:${e.message}`);
507
- }
508
- });
509
- }
510
- }
511
-
512
326
  /*
513
327
  * Setup EVE history graph feature if enabled.
514
328
  */
package/src/device/fan.ts CHANGED
@@ -58,6 +58,9 @@ export class Fan extends deviceBase {
58
58
  device: device & devicesConfig,
59
59
  ) {
60
60
  super(platform, accessory, device);
61
+ // Set category
62
+ accessory.category = this.hap.Categories.FAN;
63
+
61
64
  // this is subject we use to track when we need to POST changes to the SwitchBot API
62
65
  this.doFanUpdate = new Subject();
63
66
  this.fanUpdateInProgress = false;
@@ -369,7 +372,7 @@ export class Fan extends deviceBase {
369
372
  async registerWebhook() {
370
373
  if (this.device.webhook) {
371
374
  await this.debugLog('is listening webhook.');
372
- this.webhookEventHandler[this.device.deviceId] = async (context: batteryCirculatorFanWebhookContext) => {
375
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: batteryCirculatorFanWebhookContext) => {
373
376
  try {
374
377
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
375
378
  this.webhookContext = context;
package/src/device/hub.ts CHANGED
@@ -55,6 +55,9 @@ export class Hub extends deviceBase {
55
55
  device: device & devicesConfig,
56
56
  ) {
57
57
  super(platform, accessory, device);
58
+ // Set category
59
+ accessory.category = this.hap.Categories.SENSOR;
60
+
58
61
  // this is subject we use to track when we need to POST changes to the SwitchBot API
59
62
  this.doHubUpdate = new Subject();
60
63
  this.hubUpdateInProgress = false;
@@ -333,7 +336,7 @@ export class Hub extends deviceBase {
333
336
  async registerWebhook(): Promise<void> {
334
337
  if (this.device.webhook) {
335
338
  await this.debugLog('is listening webhook.');
336
- this.webhookEventHandler[this.device.deviceId] = async (context: hub2WebhookContext) => {
339
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: hub2WebhookContext) => {
337
340
  try {
338
341
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
339
342
  this.webhookContext = context;
@@ -58,6 +58,9 @@ export class Humidifier extends deviceBase {
58
58
  device: device & devicesConfig,
59
59
  ) {
60
60
  super(platform, accessory, device);
61
+ // Set category
62
+ accessory.category = this.hap.Categories.AIR_HUMIDIFIER;
63
+
61
64
  // this is subject we use to track when we need to POST changes to the SwitchBot API
62
65
  this.doHumidifierUpdate = new Subject();
63
66
  this.humidifierUpdateInProgress = false;
@@ -373,7 +376,7 @@ export class Humidifier extends deviceBase {
373
376
  async registerWebhook() {
374
377
  if (this.device.webhook) {
375
378
  await this.debugLog('is listening webhook.');
376
- this.webhookEventHandler[this.device.deviceId] = async (context: humidifierWebhookContext) => {
379
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: humidifierWebhookContext) => {
377
380
  try {
378
381
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
379
382
  this.webhookContext = context;
@@ -61,6 +61,9 @@ export class IOSensor extends deviceBase {
61
61
  device: device & devicesConfig,
62
62
  ) {
63
63
  super(platform, accessory, device);
64
+ // Set category
65
+ accessory.category = this.hap.Categories.SENSOR;
66
+
64
67
  // this is subject we use to track when we need to POST changes to the SwitchBot API
65
68
  this.doIOSensorUpdate = new Subject();
66
69
  this.ioSensorUpdateInProgress = false;
@@ -326,7 +329,7 @@ export class IOSensor extends deviceBase {
326
329
  async registerWebhook() {
327
330
  if (this.device.webhook) {
328
331
  await this.debugLog('is listening webhook.');
329
- this.webhookEventHandler[this.device.deviceId] = async (context: outdoorMeterWebhookContext) => {
332
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: outdoorMeterWebhookContext) => {
330
333
  try {
331
334
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
332
335
  this.webhookContext = context;
@@ -55,6 +55,9 @@ export class StripLight extends deviceBase {
55
55
  device: device & devicesConfig,
56
56
  ) {
57
57
  super(platform, accessory, device);
58
+ // Set category
59
+ accessory.category = this.hap.Categories.LIGHTBULB;
60
+
58
61
  // Adaptive Lighting
59
62
  this.adaptiveLighting(device);
60
63
  // this is subject we use to track when we need to POST changes to the SwitchBot API
@@ -356,7 +359,7 @@ export class StripLight extends deviceBase {
356
359
  async registerWebhook() {
357
360
  if (this.device.webhook) {
358
361
  await this.debugLog('is listening webhook.');
359
- this.webhookEventHandler[this.device.deviceId] = async (context: stripLightWebhookContext) => {
362
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: stripLightWebhookContext) => {
360
363
  try {
361
364
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
362
365
  this.webhookContext = context;
@@ -61,6 +61,9 @@ export class Lock extends deviceBase {
61
61
  device: device & devicesConfig,
62
62
  ) {
63
63
  super(platform, accessory, device);
64
+ // Set category
65
+ accessory.category = this.hap.Categories.DOOR_LOCK;
66
+
64
67
  // this is subject we use to track when we need to POST changes to the SwitchBot API
65
68
  this.doLockUpdate = new Subject();
66
69
  this.lockUpdateInProgress = false;
@@ -353,7 +356,7 @@ export class Lock extends deviceBase {
353
356
  async registerWebhook() {
354
357
  if (this.device.webhook) {
355
358
  await this.debugLog('is listening webhook.');
356
- this.webhookEventHandler[this.device.deviceId] = async (context: lockWebhookContext | lockProWebhookContext) => {
359
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: lockWebhookContext | lockProWebhookContext) => {
357
360
  try {
358
361
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
359
362
  this.webhookContext = context;
@@ -56,6 +56,9 @@ export class Meter extends deviceBase {
56
56
  device: device & devicesConfig,
57
57
  ) {
58
58
  super(platform, accessory, device);
59
+ // Set category
60
+ accessory.category = this.hap.Categories.SENSOR;
61
+
59
62
  // this is subject we use to track when we need to POST changes to the SwitchBot API
60
63
  this.doMeterUpdate = new Subject();
61
64
  this.meterUpdateInProgress = false;
@@ -320,7 +323,7 @@ export class Meter extends deviceBase {
320
323
  async registerWebhook() {
321
324
  if (this.device.webhook) {
322
325
  await this.debugLog('is listening webhook.');
323
- this.webhookEventHandler[this.device.deviceId] = async (context: meterWebhookContext) => {
326
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: meterWebhookContext) => {
324
327
  try {
325
328
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
326
329
  this.webhookContext = context;
@@ -61,6 +61,9 @@ export class MeterPlus extends deviceBase {
61
61
  device: device & devicesConfig,
62
62
  ) {
63
63
  super(platform, accessory, device);
64
+ // Set category
65
+ accessory.category = this.hap.Categories.SENSOR;
66
+
64
67
  // this is subject we use to track when we need to POST changes to the SwitchBot API
65
68
  this.doMeterUpdate = new Subject();
66
69
  this.meterUpdateInProgress = false;
@@ -321,7 +324,7 @@ export class MeterPlus extends deviceBase {
321
324
  async registerWebhook() {
322
325
  if (this.device.webhook) {
323
326
  await this.debugLog('is listening webhook.');
324
- this.webhookEventHandler[this.device.deviceId] = async (context: meterPlusWebhookContext) => {
327
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: meterPlusWebhookContext) => {
325
328
  try {
326
329
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
327
330
  this.webhookContext = context;
@@ -60,6 +60,9 @@ export class Motion extends deviceBase {
60
60
  device: device & devicesConfig,
61
61
  ) {
62
62
  super(platform, accessory, device);
63
+ // Set category
64
+ accessory.category = this.hap.Categories.SENSOR;
65
+
63
66
  // this is subject we use to track when we need to POST changes to the SwitchBot API
64
67
  this.doMotionUpdate = new Subject();
65
68
  this.motionUbpdateInProgress = false;
@@ -292,7 +295,7 @@ export class Motion extends deviceBase {
292
295
  async registerWebhook() {
293
296
  if (this.device.webhook) {
294
297
  await this.debugLog('is listening webhook.');
295
- this.webhookEventHandler[this.device.deviceId] = async (context: motionSensorWebhookContext) => {
298
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: motionSensorWebhookContext) => {
296
299
  try {
297
300
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
298
301
  this.webhookContext = context;
@@ -40,6 +40,9 @@ export class Plug extends deviceBase {
40
40
  device: device & devicesConfig,
41
41
  ) {
42
42
  super(platform, accessory, device);
43
+ // Set category
44
+ accessory.category = this.hap.Categories.OUTLET;
45
+
43
46
  // this is subject we use to track when we need to POST changes to the SwitchBot API
44
47
  this.doPlugUpdate = new Subject();
45
48
  this.plugUpdateInProgress = false;
@@ -204,7 +207,7 @@ export class Plug extends deviceBase {
204
207
  async registerWebhook() {
205
208
  if (this.device.webhook) {
206
209
  await this.debugLog('is listening webhook.');
207
- this.webhookEventHandler[this.device.deviceId] = async (context: plugWebhookContext | plugMiniUSWebhookContext
210
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: plugWebhookContext | plugMiniUSWebhookContext
208
211
  | plugMiniJPWebhookContext) => {
209
212
  try {
210
213
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
@@ -54,6 +54,9 @@ export class RobotVacuumCleaner extends deviceBase {
54
54
  device: device & devicesConfig,
55
55
  ) {
56
56
  super(platform, accessory, device);
57
+ // Set category
58
+ accessory.category = this.hap.Categories.OTHER;
59
+
57
60
  // this is subject we use to track when we need to POST changes to the SwitchBot API
58
61
  this.doRobotVacuumCleanerUpdate = new Subject();
59
62
  this.robotVacuumCleanerUpdateInProgress = false;
@@ -304,7 +307,7 @@ export class RobotVacuumCleaner extends deviceBase {
304
307
  async registerWebhook() {
305
308
  if (this.device.webhook) {
306
309
  await this.debugLog('is listening webhook.');
307
- this.webhookEventHandler[this.device.deviceId] = async (context: robotVacuumCleanerS1WebhookContext
310
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: robotVacuumCleanerS1WebhookContext
308
311
  | robotVacuumCleanerS1PlusWebhookContext | floorCleaningRobotS10WebhookContext) => {
309
312
  try {
310
313
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);