@switchbot/homebridge-switchbot 3.5.0-beta.58 → 3.5.0-beta.59

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 (80) hide show
  1. package/dist/device/blindtilt.d.ts +1 -1
  2. package/dist/device/blindtilt.d.ts.map +1 -1
  3. package/dist/device/blindtilt.js +101 -67
  4. package/dist/device/blindtilt.js.map +1 -1
  5. package/dist/device/bot.d.ts +1 -1
  6. package/dist/device/bot.d.ts.map +1 -1
  7. package/dist/device/bot.js +200 -153
  8. package/dist/device/bot.js.map +1 -1
  9. package/dist/device/ceilinglight.d.ts.map +1 -1
  10. package/dist/device/ceilinglight.js +39 -34
  11. package/dist/device/ceilinglight.js.map +1 -1
  12. package/dist/device/colorbulb.d.ts.map +1 -1
  13. package/dist/device/colorbulb.js +35 -36
  14. package/dist/device/colorbulb.js.map +1 -1
  15. package/dist/device/contact.d.ts.map +1 -1
  16. package/dist/device/contact.js +71 -56
  17. package/dist/device/contact.js.map +1 -1
  18. package/dist/device/curtain.d.ts +1 -1
  19. package/dist/device/curtain.d.ts.map +1 -1
  20. package/dist/device/curtain.js +98 -54
  21. package/dist/device/curtain.js.map +1 -1
  22. package/dist/device/device.d.ts.map +1 -1
  23. package/dist/device/device.js +7 -9
  24. package/dist/device/device.js.map +1 -1
  25. package/dist/device/fan.d.ts.map +1 -1
  26. package/dist/device/fan.js +53 -23
  27. package/dist/device/fan.js.map +1 -1
  28. package/dist/device/hub.d.ts.map +1 -1
  29. package/dist/device/hub.js +39 -52
  30. package/dist/device/hub.js.map +1 -1
  31. package/dist/device/humidifier.d.ts.map +1 -1
  32. package/dist/device/humidifier.js +38 -38
  33. package/dist/device/humidifier.js.map +1 -1
  34. package/dist/device/iosensor.d.ts.map +1 -1
  35. package/dist/device/iosensor.js +40 -45
  36. package/dist/device/iosensor.js.map +1 -1
  37. package/dist/device/lightstrip.d.ts.map +1 -1
  38. package/dist/device/lightstrip.js +34 -31
  39. package/dist/device/lightstrip.js.map +1 -1
  40. package/dist/device/lock.d.ts.map +1 -1
  41. package/dist/device/lock.js +67 -58
  42. package/dist/device/lock.js.map +1 -1
  43. package/dist/device/meter.d.ts.map +1 -1
  44. package/dist/device/meter.js +41 -46
  45. package/dist/device/meter.js.map +1 -1
  46. package/dist/device/meterplus.d.ts.map +1 -1
  47. package/dist/device/meterplus.js +41 -46
  48. package/dist/device/meterplus.js.map +1 -1
  49. package/dist/device/motion.d.ts.map +1 -1
  50. package/dist/device/motion.js +48 -38
  51. package/dist/device/motion.js.map +1 -1
  52. package/dist/device/plug.d.ts.map +1 -1
  53. package/dist/device/plug.js +12 -12
  54. package/dist/device/plug.js.map +1 -1
  55. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  56. package/dist/device/robotvacuumcleaner.js +78 -95
  57. package/dist/device/robotvacuumcleaner.js.map +1 -1
  58. package/dist/device/waterdetector.d.ts.map +1 -1
  59. package/dist/device/waterdetector.js +38 -30
  60. package/dist/device/waterdetector.js.map +1 -1
  61. package/package.json +1 -1
  62. package/src/device/blindtilt.ts +115 -84
  63. package/src/device/bot.ts +233 -172
  64. package/src/device/ceilinglight.ts +43 -42
  65. package/src/device/colorbulb.ts +35 -40
  66. package/src/device/contact.ts +77 -60
  67. package/src/device/curtain.ts +106 -61
  68. package/src/device/device.ts +7 -9
  69. package/src/device/fan.ts +61 -26
  70. package/src/device/hub.ts +47 -57
  71. package/src/device/humidifier.ts +48 -52
  72. package/src/device/iosensor.ts +51 -53
  73. package/src/device/lightstrip.ts +37 -35
  74. package/src/device/lock.ts +74 -65
  75. package/src/device/meter.ts +47 -50
  76. package/src/device/meterplus.ts +47 -50
  77. package/src/device/motion.ts +53 -42
  78. package/src/device/plug.ts +14 -15
  79. package/src/device/robotvacuumcleaner.ts +83 -100
  80. package/src/device/waterdetector.ts +41 -31
@@ -16,6 +16,7 @@ import type { Service, PlatformAccessory, CharacteristicValue, CharacteristicCha
16
16
  export class Curtain extends deviceBase {
17
17
  // Services
18
18
  private WindowCovering: {
19
+ Name: CharacteristicValue;
19
20
  Service: Service;
20
21
  PositionState: CharacteristicValue;
21
22
  TargetPosition: CharacteristicValue;
@@ -24,12 +25,15 @@ export class Curtain extends deviceBase {
24
25
  };
25
26
 
26
27
  private Battery: {
28
+ Name: CharacteristicValue;
27
29
  Service: Service;
28
30
  BatteryLevel: CharacteristicValue;
29
31
  StatusLowBattery: CharacteristicValue;
32
+ ChargingState: CharacteristicValue;
30
33
  };
31
34
 
32
35
  private LightSensor?: {
36
+ Name: CharacteristicValue;
33
37
  Service: Service;
34
38
  CurrentAmbientLightLevel?: CharacteristicValue;
35
39
  };
@@ -57,49 +61,29 @@ export class Curtain extends deviceBase {
57
61
 
58
62
  // Initialize LightBulb property
59
63
  this.WindowCovering = {
60
- Service: accessory.getService(this.hap.Service.WindowCovering) as Service,
61
- PositionState: accessory.context.PositionState || this.hap.Characteristic.PositionState.STOPPED,
62
- TargetPosition: accessory.context.TargetPosition || 100,
63
- CurrentPosition: accessory.context.CurrentPosition || 100,
64
- HoldPosition: accessory.context.HoldPosition || false,
64
+ Name: accessory.context.WindowCovering.Name ?? accessory.displayName,
65
+ Service: accessory.getService(this.hap.Service.WindowCovering) ?? accessory.addService(this.hap.Service.WindowCovering) as Service,
66
+ PositionState: accessory.context.PositionState ?? this.hap.Characteristic.PositionState.STOPPED,
67
+ TargetPosition: accessory.context.TargetPosition ?? 100,
68
+ CurrentPosition: accessory.context.CurrentPosition ?? 100,
69
+ HoldPosition: accessory.context.HoldPosition ?? false,
65
70
  };
66
71
 
67
- // Initialize Battery property
68
- this.Battery = {
69
- Service: accessory.getService(this.hap.Service.Battery) as Service,
70
- BatteryLevel: accessory.context.BatteryLevel || 100,
71
- StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
72
- };
73
-
74
- // Initialize LightSensor property
75
- if (!this.device.curtain?.hide_lightsensor) {
76
- this.LightSensor = {
77
- Service: accessory.getService(this.hap.Service.LightSensor) as Service,
78
- CurrentAmbientLightLevel: accessory.context.CurrentAmbientLightLevel || 0.0001,
79
- };
80
- }
81
-
82
- // Retrieve initial values and updateHomekit
83
- this.refreshStatus();
84
-
85
- // get the WindowCovering service if it exists, otherwise create a new WindowCovering service
86
- // you can create multiple services for each accessory
87
- const WindowCoveringService = `${accessory.displayName} ${device.deviceType}`;
88
- (this.WindowCovering.Service = accessory.getService(this.hap.Service.WindowCovering)
89
- || accessory.addService(this.hap.Service.WindowCovering)), WindowCoveringService;
90
-
91
- this.WindowCovering.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
92
-
93
- // each service must implement at-minimum the "required characteristics" for the given service type
94
- // see https://developers.homebridge.io/#/service/WindowCovering
95
-
96
- // create handlers for required characteristics
97
- this.WindowCovering.Service.setCharacteristic(this.hap.Characteristic.PositionState, this.WindowCovering.PositionState);
72
+ // Initialize WindowCovering Service
73
+ this.WindowCovering.Service.
74
+ setCharacteristic(this.hap.Characteristic.Name, this.WindowCovering.Name)
75
+ .setCharacteristic(this.hap.Characteristic.ObstructionDetected, false)
76
+ .getCharacteristic(this.hap.Characteristic.PositionState)
77
+ .onGet(() => {
78
+ return this.WindowCovering.PositionState;
79
+ });
80
+ accessory.context.WindowCovering.Name = this.WindowCovering.Name;
98
81
 
82
+ // Initialize WindowCovering CurrentPosition
99
83
  this.WindowCovering.Service
100
84
  .getCharacteristic(this.hap.Characteristic.CurrentPosition)
101
85
  .setProps({
102
- minStep: this.minStep(device),
86
+ minStep: Number(this.minStep(device)),
103
87
  minValue: 0,
104
88
  maxValue: 100,
105
89
  validValueRanges: [0, 100],
@@ -108,42 +92,83 @@ export class Curtain extends deviceBase {
108
92
  return this.WindowCovering.CurrentPosition;
109
93
  });
110
94
 
95
+ // Initialize WindowCovering TargetPosition
111
96
  this.WindowCovering.Service
112
97
  .getCharacteristic(this.hap.Characteristic.TargetPosition)
113
98
  .setProps({
114
- minStep: this.minStep(device),
99
+ minStep: Number(this.minStep(device)),
115
100
  minValue: 0,
116
101
  maxValue: 100,
117
102
  validValueRanges: [0, 100],
118
103
  })
104
+ .onGet(() => {
105
+ return this.WindowCovering.TargetPosition;
106
+ })
119
107
  .onSet(this.TargetPositionSet.bind(this));
120
108
 
109
+ // Initialize WindowCovering TargetPosition
121
110
  this.WindowCovering.Service
122
111
  .getCharacteristic(this.hap.Characteristic.HoldPosition)
112
+ .onGet(() => {
113
+ return this.WindowCovering.HoldPosition;
114
+ })
123
115
  .onSet(this.HoldPositionSet.bind(this));
124
116
 
125
- // Light Sensor Service
117
+ // Initialize Battery property
118
+ this.Battery = {
119
+ Name: accessory.context.Battery.Name ?? `${accessory.displayName} Battery`,
120
+ Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery) as Service,
121
+ BatteryLevel: accessory.context.BatteryLevel ?? 100,
122
+ StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
123
+ ChargingState: accessory.context.ChargingState ?? this.hap.Characteristic.ChargingState.NOT_CHARGING,
124
+ };
125
+
126
+ // Initialize Battery Service
127
+ this.Battery.Service
128
+ .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
129
+ .getCharacteristic(this.hap.Characteristic.BatteryLevel)
130
+ .onGet(() => {
131
+ return this.Battery.BatteryLevel;
132
+ });
133
+ accessory.context.Battery.Name = this.Battery.Name;
134
+
135
+ this.Battery.Service
136
+ .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
137
+ .onGet(() => {
138
+ return this.Battery.StatusLowBattery;
139
+ });
140
+
141
+ this.Battery.Service
142
+ .getCharacteristic(this.hap.Characteristic.ChargingState)
143
+ .onGet(() => {
144
+ return this.Battery.ChargingState;
145
+ });
146
+
147
+ // Initialize LightSensor Service
126
148
  if (device.curtain?.hide_lightsensor) {
127
149
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
128
150
  this.LightSensor!.Service = this.accessory.getService(this.hap.Service.LightSensor) as Service;
129
151
  accessory.removeService(this.LightSensor!.Service);
130
- } else if (!this.LightSensor?.Service) {
131
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Light Sensor Service`);
132
- const LightSensorService = `${accessory.displayName} Light Sensor`;
133
- (this.LightSensor!.Service = this.accessory.getService(this.hap.Service.LightSensor)
134
- || this.accessory.addService(this.hap.Service.LightSensor)), LightSensorService;
135
-
136
- this.LightSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, LightSensorService);
137
152
  } else {
138
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
139
- }
153
+ this.LightSensor = {
154
+ Name: accessory.context.LightSensor.Name ?? `${accessory.displayName} Light Sensor`,
155
+ Service: accessory.getService(this.hap.Service.LightSensor) ?? this.accessory.addService(this.hap.Service.LightSensor) as Service,
156
+ CurrentAmbientLightLevel: accessory.context.CurrentAmbientLightLevel ?? 0.0001,
157
+ };
140
158
 
141
- // Battery Service
142
- const batteryService = `${accessory.displayName} Battery`;
143
- (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
144
- || accessory.addService(this.hap.Service.Battery)), batteryService;
159
+ // Initialize LightSensor Characteristic
160
+ this.LightSensor.Service
161
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightSensor.Name)
162
+ .setCharacteristic(this.hap.Characteristic.StatusActive, true)
163
+ .getCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel)
164
+ .onGet(() => {
165
+ return this.LightSensor!.CurrentAmbientLightLevel!;
166
+ });
167
+ accessory.context.LightSensor.Name = this.LightSensor.Name;
168
+ }
145
169
 
146
- this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
170
+ // Retrieve initial values and updateHomekit
171
+ this.refreshStatus();
147
172
 
148
173
  // Update Homekit
149
174
  this.updateHomeKitCharacteristics();
@@ -345,30 +370,37 @@ export class Curtain extends deviceBase {
345
370
  case 3:
346
371
  this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 2;
347
372
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
373
+ this.Battery.ChargingState = this.hap.Characteristic.ChargingState.CHARGING;
348
374
  break;
349
375
  case 4:
350
376
  this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 3;
351
377
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
378
+ this.Battery.ChargingState = this.hap.Characteristic.ChargingState.CHARGING;
352
379
  break;
353
380
  case 5:
354
381
  this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 4;
355
382
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
383
+ this.Battery.ChargingState = this.hap.Characteristic.ChargingState.CHARGING;
356
384
  break;
357
385
  case 6:
358
386
  this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 5;
359
387
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
388
+ this.Battery.ChargingState = this.hap.Characteristic.ChargingState.CHARGING;
360
389
  break;
361
390
  case 7:
362
391
  this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 6;
363
392
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
393
+ this.Battery.ChargingState = this.hap.Characteristic.ChargingState.CHARGING;
364
394
  break;
365
395
  case 8:
366
396
  this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 7;
367
397
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
398
+ this.Battery.ChargingState = this.hap.Characteristic.ChargingState.CHARGING;
368
399
  break;
369
400
  case 9:
370
401
  this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 8;
371
402
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
403
+ this.Battery.ChargingState = this.hap.Characteristic.ChargingState.CHARGING;
372
404
  break;
373
405
  case 10:
374
406
  default:
@@ -441,12 +473,13 @@ export class Curtain extends deviceBase {
441
473
  const set_minLux = await this.minLux();
442
474
  const set_maxLux = await this.maxLux();
443
475
  switch (deviceStatus.body.brightness) {
444
- case 'dim':
445
- this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
446
- break;
447
476
  case 'bright':
448
- default:
449
477
  this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
478
+ this.Battery.ChargingState = this.hap.Characteristic.ChargingState.CHARGING;
479
+ break;
480
+ case 'dim':
481
+ default:
482
+ this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
450
483
  }
451
484
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
452
485
  + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
@@ -843,6 +876,17 @@ export class Curtain extends deviceBase {
843
876
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
844
877
  + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
845
878
  }
879
+ if (this.Battery.ChargingState === undefined) {
880
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
881
+ } else {
882
+ if (this.device.mqttURL) {
883
+ this.mqttPublish('ChargingState', this.Battery.ChargingState.toString());
884
+ }
885
+ this.accessory.context.ChargingState = this.Battery.ChargingState;
886
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
887
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
888
+ + ` ChargingState: ${this.Battery.ChargingState}`);
889
+ }
846
890
  }
847
891
 
848
892
  async BLEPushConnection() {
@@ -908,7 +952,7 @@ export class Curtain extends deviceBase {
908
952
  }
909
953
  }
910
954
 
911
- minStep(device: device & devicesConfig): number {
955
+ async minStep(device: device & devicesConfig): Promise<number> {
912
956
  let set_minStep: number;
913
957
  if (device.curtain?.set_minStep) {
914
958
  set_minStep = device.curtain?.set_minStep;
@@ -950,11 +994,12 @@ export class Curtain extends deviceBase {
950
994
  this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, e);
951
995
  this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.PositionState, e);
952
996
  this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, e);
997
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
998
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
999
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, e);
953
1000
  if (!this.device.curtain?.hide_lightsensor) {
954
1001
  this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
1002
+ this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
955
1003
  }
956
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
957
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
958
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
959
1004
  }
960
1005
  }
@@ -565,15 +565,13 @@ export abstract class deviceBase {
565
565
  }
566
566
 
567
567
  async statusCode(statusCode: number): Promise<void> {
568
- switch (statusCode) {
569
- case 171:
570
- switch (this.device.deviceId) {
571
- case this.device.hubDeviceId:
572
- case '000000000000':
573
- statusCode = 161;
574
- break;
575
- }
576
- break;
568
+ if (statusCode === 171) {
569
+ if (this.device.deviceId === this.device.hubDeviceId) {
570
+ statusCode = 161;
571
+ }
572
+ if (this.device.deviceId === '000000000000') {
573
+ statusCode = 161;
574
+ }
577
575
  }
578
576
  switch (statusCode) {
579
577
  case 151:
package/src/device/fan.ts CHANGED
@@ -14,6 +14,7 @@ import type { device, devicesConfig, serviceData, deviceStatus} from '../setting
14
14
  export class Fan extends deviceBase {
15
15
  // Services
16
16
  private Fan: {
17
+ Name: CharacteristicValue;
17
18
  Service: Service;
18
19
  Active: CharacteristicValue;
19
20
  SwingMode: CharacteristicValue;
@@ -21,6 +22,7 @@ export class Fan extends deviceBase {
21
22
  };
22
23
 
23
24
  private Battery: {
25
+ Name: CharacteristicValue;
24
26
  Service: Service;
25
27
  BatteryLevel: CharacteristicValue;
26
28
  StatusLowBattery: CharacteristicValue;
@@ -41,42 +43,75 @@ export class Fan extends deviceBase {
41
43
  this.doPlugUpdate = new Subject();
42
44
  this.plugUpdateInProgress = false;
43
45
 
44
- // Initialize Fan property
46
+ // Initialize Fan Service
45
47
  this.Fan = {
46
- Service: accessory.getService(this.hap.Service.Fanv2) as Service,
47
- Active: accessory.context.Active || this.hap.Characteristic.Active.INACTIVE,
48
- SwingMode: accessory.context.SwingMode || this.hap.Characteristic.SwingMode.SWING_DISABLED,
49
- RotationSpeed: accessory.context.RotationSpeed || 0,
48
+ Name: accessory.context.Fan.Name ?? accessory.displayName,
49
+ Service: accessory.getService(this.hap.Service.Fanv2) ?? accessory.addService(this.hap.Service.Fanv2) as Service,
50
+ Active: accessory.context.Active ?? this.hap.Characteristic.Active.INACTIVE,
51
+ SwingMode: accessory.context.SwingMode ?? this.hap.Characteristic.SwingMode.SWING_DISABLED,
52
+ RotationSpeed: accessory.context.RotationSpeed ?? 0,
50
53
  };
51
54
 
55
+ // Initialize Fan Service
56
+ this.Fan.Service
57
+ .setCharacteristic(this.hap.Characteristic.Name, this.Fan.Name)
58
+ .getCharacteristic(this.hap.Characteristic.Active)
59
+ .onGet(() => {
60
+ return this.Fan.Active;
61
+ })
62
+ .onSet(this.ActiveSet.bind(this));
63
+
64
+ // Initialize Fan RotationSpeed Characteristic
65
+ this.Fan.Service
66
+ .getCharacteristic(this.hap.Characteristic.RotationSpeed)
67
+ .onGet(() => {
68
+ return this.Fan.RotationSpeed;
69
+ })
70
+ .onSet(this.RotationSpeedSet.bind(this));
71
+
72
+ // Initialize Fan SwingMode Characteristic
73
+ this.Fan.Service.getCharacteristic(this.hap.Characteristic.SwingMode)
74
+ .onGet(() => {
75
+ return this.Fan.SwingMode;
76
+ })
77
+ .onSet(this.SwingModeSet.bind(this));
78
+ accessory.context.Fan.Name = this.Fan.Name;
79
+
52
80
  // Initialize Battery property
53
81
  this.Battery = {
54
- Service: accessory.getService(this.hap.Service.Battery) as Service,
55
- BatteryLevel: accessory.context.BatteryLevel || 100,
56
- StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
57
- ChargingState: accessory.context.ChargingState || this.hap.Characteristic.ChargingState.NOT_CHARGING,
82
+ Name: accessory.context.Battery.Name ?? accessory.displayName,
83
+ Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery) as Service,
84
+ BatteryLevel: accessory.context.BatteryLevel ?? 100,
85
+ StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
86
+ ChargingState: accessory.context.ChargingState ?? this.hap.Characteristic.ChargingState.NOT_CHARGING,
58
87
  };
59
88
 
89
+ // Initialize Battery Service
90
+ this.Battery.Service
91
+ .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
92
+ .getCharacteristic(this.hap.Characteristic.BatteryLevel)
93
+ .onGet(() => {
94
+ return this.Battery.BatteryLevel;
95
+ });
96
+
97
+ // Initialize Battery ChargingState Characteristic
98
+ this.Battery.Service
99
+ .getCharacteristic(this.hap.Characteristic.ChargingState)
100
+ .onGet(() => {
101
+ return this.Battery.ChargingState;
102
+ });
103
+
104
+ // Initialize Battery StatusLowBattery Characteristic
105
+ this.Battery.Service
106
+ .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
107
+ .onGet(() => {
108
+ return this.Battery.StatusLowBattery;
109
+ });
110
+ accessory.context.Battery.Name = this.Battery.Name;
111
+
60
112
  // Retrieve initial values and updateHomekit
61
113
  this.refreshStatus();
62
114
 
63
- // get the Fan service if it exists, otherwise create a new Fanv2 service
64
- // you can create multiple services for each accessory
65
- const FanService = `${accessory.displayName} ${device.deviceType}`;
66
- (this.Fan.Service = accessory.getService(this.hap.Service.Fanv2)
67
- || accessory.addService(this.hap.Service.Fanv2)), FanService;
68
-
69
- this.Fan.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
70
- // each service must implement at-minimum the "required characteristics" for the given service type
71
- // see https://developers.homebridge.io/#/service/Fanv2
72
-
73
- // create handlers for required characteristics
74
- this.Fan.Service.getCharacteristic(this.hap.Characteristic.Active).onSet(this.ActiveSet.bind(this));
75
- // create handlers for required characteristics
76
- this.Fan.Service.getCharacteristic(this.hap.Characteristic.RotationSpeed).onSet(this.RotationSpeedSet.bind(this));
77
- // create handlers for required characteristics
78
- this.Fan.Service.getCharacteristic(this.hap.Characteristic.SwingMode).onSet(this.SwingModeSet.bind(this));
79
-
80
115
  // Update Homekit
81
116
  this.updateHomeKitCharacteristics();
82
117
 
package/src/device/hub.ts CHANGED
@@ -10,21 +10,24 @@ import { Subject, interval, skipWhile } from 'rxjs';
10
10
 
11
11
  import type { SwitchBotPlatform } from '../platform.js';
12
12
  import type { device, deviceStatus, devicesConfig } from '../settings.js';
13
- import type { CharacteristicValue, PlatformAccessory, Service} from 'homebridge';
13
+ import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
14
14
 
15
15
  export class Hub extends deviceBase {
16
16
  // Services
17
17
  private LightSensor?: {
18
+ Name: CharacteristicValue;
18
19
  Service: Service;
19
20
  CurrentAmbientLightLevel: CharacteristicValue;
20
21
  };
21
22
 
22
23
  private HumiditySensor?: {
24
+ Name: CharacteristicValue;
23
25
  Service: Service;
24
26
  CurrentRelativeHumidity: CharacteristicValue;
25
27
  };
26
28
 
27
29
  private TemperatureSensor?: {
30
+ Name: CharacteristicValue;
28
31
  Service: Service;
29
32
  CurrentTemperature: CharacteristicValue;
30
33
  };
@@ -43,46 +46,21 @@ export class Hub extends deviceBase {
43
46
  this.doHubUpdate = new Subject();
44
47
  this.hubUpdateInProgress = false;
45
48
 
46
- // Initialize Temperature Sensor property
47
- if (!device.hub?.hide_temperature) {
48
- this.TemperatureSensor = {
49
- Service: accessory.getService(this.hap.Service.TemperatureSensor) as Service,
50
- CurrentTemperature: accessory.context.CurrentTemperature || 0,
51
- };
52
- }
53
-
54
- // Initialize Humidity Sensor property
55
- if (!device.hub?.hide_humidity) {
56
- this.HumiditySensor = {
57
- Service: accessory.getService(this.hap.Service.HumiditySensor) as Service,
58
- CurrentRelativeHumidity: accessory.context.CurrentRelativeHumidity || 0,
59
- };
60
- }
61
-
62
- // Initialize Light Sensor property
63
- if (!device.hub?.hide_lightsensor) {
64
- this.LightSensor = {
65
- Service: accessory.getService(this.hap.Service.LightSensor) as Service,
66
- CurrentAmbientLightLevel: accessory.context.CurrentAmbientLightLevel || 0.0001,
67
- };
68
- }
69
-
70
- // Retrieve initial values and updateHomekit
71
- this.refreshStatus();
72
-
73
- // Temperature Sensor Service
49
+ // Initialize Temperature Sensor Service
74
50
  if (device.hub?.hide_temperature) {
75
51
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
76
52
  this.TemperatureSensor!.Service = this.accessory.getService(this.hap.Service.TemperatureSensor) as Service;
77
53
  accessory.removeService(this.TemperatureSensor!.Service);
78
- } else if (!this.TemperatureSensor?.Service) {
79
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
80
- const TemperatureSensorService = `${accessory.displayName} Temperature Sensor`;
81
- (this.TemperatureSensor!.Service = this.accessory.getService(this.hap.Service.TemperatureSensor)
82
- || this.accessory.addService(this.hap.Service.TemperatureSensor)), TemperatureSensorService;
83
-
84
- this.TemperatureSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, TemperatureSensorService);
85
- this.TemperatureSensor!.Service
54
+ } else {
55
+ this.TemperatureSensor = {
56
+ Name: accessory.context.TemperatureSensor.Name ?? `${accessory.displayName} Temperature Sensor`,
57
+ Service: accessory.getService(this.hap.Service.TemperatureSensor) ?? this.accessory.addService(this.hap.Service.TemperatureSensor) as Service,
58
+ CurrentTemperature: accessory.context.CurrentTemperature ?? 0,
59
+ };
60
+
61
+ // Initialize Temperature Sensor Characteristic
62
+ this.TemperatureSensor.Service
63
+ .setCharacteristic(this.hap.Characteristic.Name, this.TemperatureSensor.Name)
86
64
  .getCharacteristic(this.hap.Characteristic.CurrentTemperature)
87
65
  .setProps({
88
66
  unit: Units['CELSIUS'],
@@ -94,23 +72,24 @@ export class Hub extends deviceBase {
94
72
  .onGet(() => {
95
73
  return this.TemperatureSensor!.CurrentTemperature;
96
74
  });
97
- } else {
98
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Temperature Sensor Service Not Added`);
75
+ accessory.context.TemperatureSensor.Name = this.TemperatureSensor.Name;
99
76
  }
100
77
 
101
- // Humidity Sensor Service
78
+ // Initialize Humidity Sensor Service
102
79
  if (device.hub?.hide_humidity) {
103
80
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
104
81
  this.HumiditySensor!.Service = this.accessory.getService(this.hap.Service.HumiditySensor) as Service;
105
82
  accessory.removeService(this.HumiditySensor!.Service);
106
- } else if (!this.HumiditySensor?.Service) {
107
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
108
- const HumiditySensorService = `${accessory.displayName} Humidity Sensor`;
109
- (this.HumiditySensor!.Service = this.accessory.getService(this.hap.Service.HumiditySensor)
110
- || this.accessory.addService(this.hap.Service.HumiditySensor)), HumiditySensorService;
83
+ } else {
84
+ this.HumiditySensor = {
85
+ Name: accessory.context.HumiditySensor.Name ?? `${accessory.displayName} Humidity Sensor`,
86
+ Service: accessory.getService(this.hap.Service.HumiditySensor) ?? this.accessory.addService(this.hap.Service.HumiditySensor) as Service,
87
+ CurrentRelativeHumidity: accessory.context.CurrentRelativeHumidity ?? 0,
88
+ };
111
89
 
112
- this.HumiditySensor!.Service.setCharacteristic(this.hap.Characteristic.Name, HumiditySensorService);
90
+ // Initialize Humidity Sensor Characteristics
113
91
  this.HumiditySensor!.Service
92
+ .setCharacteristic(this.hap.Characteristic.Name, this.HumiditySensor.Name)
114
93
  .getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
115
94
  .setProps({
116
95
  minStep: 0.1,
@@ -118,26 +97,37 @@ export class Hub extends deviceBase {
118
97
  .onGet(() => {
119
98
  return this.HumiditySensor!.CurrentRelativeHumidity;
120
99
  });
121
- } else {
122
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Humidity Sensor Service Not Added`);
100
+ accessory.context.HumiditySensor.Name = this.HumiditySensor.Name;
123
101
  }
124
102
 
125
- // Light Sensor Service
103
+ // Initialize Light Sensor Service
126
104
  if (device.hub?.hide_lightsensor) {
127
105
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
128
106
  this.LightSensor!.Service = this.accessory.getService(this.hap.Service.LightSensor) as Service;
129
107
  accessory.removeService(this.LightSensor!.Service);
130
- } else if (!this.LightSensor?.Service) {
131
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Light Sensor Service`);
132
- const LightSensorService = `${accessory.displayName} Light Sensor`;
133
- (this.LightSensor!.Service = this.accessory.getService(this.hap.Service.LightSensor)
134
- || this.accessory.addService(this.hap.Service.LightSensor)), LightSensorService;
135
-
136
- this.LightSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, LightSensorService);
137
108
  } else {
138
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
109
+ this.LightSensor = {
110
+ Name: accessory.context.LightSensor.Name ?? `${accessory.displayName} Light Sensor`,
111
+ Service: accessory.getService(this.hap.Service.LightSensor) ?? this.accessory.addService(this.hap.Service.LightSensor) as Service,
112
+ CurrentAmbientLightLevel: accessory.context.CurrentAmbientLightLevel ?? 0.0001,
113
+ };
114
+
115
+ // Initialize Light Sensor Characteristics
116
+ this.LightSensor!.Service
117
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightSensor.Name)
118
+ .getCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel)
119
+ .setProps({
120
+ minStep: 1,
121
+ })
122
+ .onGet(() => {
123
+ return this.LightSensor!.CurrentAmbientLightLevel;
124
+ });
125
+ accessory.context.LightSensor.Name = this.LightSensor.Name;
139
126
  }
140
127
 
128
+ // Retrieve initial values and updateHomekit
129
+ this.refreshStatus();
130
+
141
131
  // Retrieve initial values and update Homekit
142
132
  this.updateHomeKitCharacteristics();
143
133