@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
@@ -21,6 +21,7 @@ import type { Service, PlatformAccessory, CharacteristicValue, ControllerConstru
21
21
  export class CeilingLight extends deviceBase {
22
22
  // Services
23
23
  private LightBulb: {
24
+ Name: CharacteristicValue;
24
25
  Service: Service;
25
26
  On: CharacteristicValue;
26
27
  Hue: CharacteristicValue;
@@ -52,35 +53,44 @@ export class CeilingLight extends deviceBase {
52
53
 
53
54
  // Initialize LightBulb property
54
55
  this.LightBulb = {
55
- Service: accessory.getService(this.hap.Service.Lightbulb) as Service,
56
- On: accessory.context.On || false,
57
- Hue: accessory.context.Hue || 0,
58
- Saturation: accessory.context.Saturation || 0,
59
- Brightness: accessory.context.Brightness || 0,
60
- ColorTemperature: accessory.context.ColorTemperature || 140,
56
+ Name: accessory.context.LightBulb.Name ?? accessory.displayName,
57
+ Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb) as Service,
58
+ On: accessory.context.On ?? false,
59
+ Hue: accessory.context.Hue ?? 0,
60
+ Saturation: accessory.context.Saturation ?? 0,
61
+ Brightness: accessory.context.Brightness ?? 0,
62
+ ColorTemperature: accessory.context.ColorTemperature ?? 140,
61
63
  };
62
64
 
63
- // Retrieve initial values and updateHomekit
64
- this.refreshStatus();
65
-
66
- const LightBulbService = `${accessory.displayName} ${device.deviceType}`;
67
- (this.LightBulb!.Service = accessory.getService(this.hap.Service.Lightbulb)
68
- || accessory.addService(this.hap.Service.Lightbulb)), LightBulbService;
69
-
70
-
71
- if (this.adaptiveLightingShift === -1 && this.accessory.context.adaptiveLighting) {
72
- this.accessory.removeService(this.LightBulb!.Service);
73
- this.LightBulb!.Service = this.accessory.addService(this.hap.Service.Lightbulb);
74
- this.accessory.context.adaptiveLighting = false;
75
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
65
+ // Adaptive Lighting
66
+ if (this.adaptiveLightingShift === -1 && accessory.context.adaptiveLighting) {
67
+ accessory.removeService(this.LightBulb.Service);
68
+ this.LightBulb.Service = accessory.addService(this.hap.Service.Lightbulb);
69
+ accessory.context.adaptiveLighting = false;
70
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} adaptiveLighting: ${accessory.context.adaptiveLighting}`);
76
71
  }
72
+ if (this.adaptiveLightingShift !== -1) {
73
+ this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
74
+ customTemperatureAdjustment: this.adaptiveLightingShift,
75
+ });
76
+ this.accessory.configureController(this.AdaptiveLightingController);
77
+ this.accessory.context.adaptiveLighting = true;
78
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},`
79
+ + ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
80
+ }
81
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
77
82
 
78
- this.LightBulb!.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
79
- // handle on / off events using the On characteristic
80
- this.LightBulb!.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
83
+ // Initialize LightBulb Characteristics
84
+ this.LightBulb.Service
85
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightBulb.Name)
86
+ .getCharacteristic(this.hap.Characteristic.On)
87
+ .onGet(() => {
88
+ return this.LightBulb.On;
89
+ })
90
+ .onSet(this.OnSet.bind(this));
81
91
 
82
- // handle Brightness events using the Brightness characteristic
83
- this.LightBulb!.Service
92
+ // Initialize LightBulb Brightness
93
+ this.LightBulb.Service
84
94
  .getCharacteristic(this.hap.Characteristic.Brightness)
85
95
  .setProps({
86
96
  minStep: this.minStep(device),
@@ -93,8 +103,8 @@ export class CeilingLight extends deviceBase {
93
103
  })
94
104
  .onSet(this.BrightnessSet.bind(this));
95
105
 
96
- // handle ColorTemperature events using the ColorTemperature characteristic
97
- this.LightBulb!.Service
106
+ // Initialize LightBulb ColorTemperature
107
+ this.LightBulb.Service
98
108
  .getCharacteristic(this.hap.Characteristic.ColorTemperature)
99
109
  .setProps({
100
110
  minValue: 140,
@@ -106,8 +116,8 @@ export class CeilingLight extends deviceBase {
106
116
  })
107
117
  .onSet(this.ColorTemperatureSet.bind(this));
108
118
 
109
- // handle Hue events using the Hue characteristic
110
- this.LightBulb!.Service
119
+ // Initialize LightBulb Hue
120
+ this.LightBulb.Service
111
121
  .getCharacteristic(this.hap.Characteristic.Hue)
112
122
  .setProps({
113
123
  minValue: 0,
@@ -119,8 +129,8 @@ export class CeilingLight extends deviceBase {
119
129
  })
120
130
  .onSet(this.HueSet.bind(this));
121
131
 
122
- // handle Hue events using the Hue characteristic
123
- this.LightBulb!.Service
132
+ // Initialize LightBulb Saturation
133
+ this.LightBulb.Service
124
134
  .getCharacteristic(this.hap.Characteristic.Saturation)
125
135
  .setProps({
126
136
  minValue: 0,
@@ -131,19 +141,10 @@ export class CeilingLight extends deviceBase {
131
141
  return this.LightBulb.Saturation;
132
142
  })
133
143
  .onSet(this.SaturationSet.bind(this));
144
+ accessory.context.LightBulb.Name = this.LightBulb.Name;
134
145
 
135
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
136
- if (this.adaptiveLightingShift !== -1) {
137
- this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb!.Service, {
138
- customTemperatureAdjustment: this.adaptiveLightingShift,
139
- });
140
- this.accessory.configureController(this.AdaptiveLightingController);
141
- this.accessory.context.adaptiveLighting = true;
142
- this.debugLog(
143
- `${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
144
- ` adaptiveLightingShift: ${this.adaptiveLightingShift}`,
145
- );
146
- }
146
+ // Retrieve initial values and updateHomekit
147
+ this.refreshStatus();
147
148
 
148
149
  // Update Homekit
149
150
  this.updateHomeKitCharacteristics();
@@ -21,6 +21,7 @@ import type { Service, PlatformAccessory, CharacteristicValue, ControllerConstru
21
21
  export class ColorBulb extends deviceBase {
22
22
  // Services
23
23
  private LightBulb: {
24
+ Name: CharacteristicValue;
24
25
  Service: Service;
25
26
  On: CharacteristicValue;
26
27
  Hue: CharacteristicValue;
@@ -53,36 +54,42 @@ export class ColorBulb extends deviceBase {
53
54
 
54
55
  // Initialize LightBulb property
55
56
  this.LightBulb = {
56
- Service: accessory.getService(this.hap.Service.Lightbulb) as Service,
57
- On: accessory.context.On || false,
58
- Hue: accessory.context.Hue || 0,
59
- Saturation: accessory.context.Saturation || 0,
60
- Brightness: accessory.context.Brightness || 0,
61
- ColorTemperature: accessory.context.ColorTemperature || 140,
57
+ Name: accessory.context.LightBulb.Name ?? accessory.displayName,
58
+ Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb) as Service,
59
+ On: accessory.context.On ?? false,
60
+ Hue: accessory.context.Hue ?? 0,
61
+ Saturation: accessory.context.Saturation ?? 0,
62
+ Brightness: accessory.context.Brightness ?? 0,
63
+ ColorTemperature: accessory.context.ColorTemperature ?? 140,
62
64
  };
63
65
 
64
- // Retrieve initial values and updateHomekit
65
- this.refreshStatus();
66
-
67
- // get the Lightbulb service if it exists, otherwise create a new Lightbulb service
68
- // you can create multiple services for each accessory
69
- const lightBulbService = `${accessory.displayName} ${device.deviceType}`;
70
- (this.LightBulb.Service = accessory.getService(this.hap.Service.Lightbulb)
71
- || accessory.addService(this.hap.Service.Lightbulb)), lightBulbService;
72
-
73
- if (this.adaptiveLightingShift === -1 && this.accessory.context.adaptiveLighting) {
74
- this.accessory.removeService(this.LightBulb.Service);
75
- this.LightBulb.Service = this.accessory.addService(this.hap.Service.Lightbulb);
76
- this.accessory.context.adaptiveLighting = false;
77
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
66
+ // Adaptive Lighting
67
+ if (this.adaptiveLightingShift === -1 && accessory.context.adaptiveLighting) {
68
+ accessory.removeService(this.LightBulb.Service);
69
+ this.LightBulb.Service = accessory.addService(this.hap.Service.Lightbulb);
70
+ accessory.context.adaptiveLighting = false;
71
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} adaptiveLighting: ${accessory.context.adaptiveLighting}`);
78
72
  }
73
+ if (this.adaptiveLightingShift !== -1) {
74
+ this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
75
+ customTemperatureAdjustment: this.adaptiveLightingShift,
76
+ });
77
+ accessory.configureController(this.AdaptiveLightingController);
78
+ accessory.context.adaptiveLighting = true;
79
+ this.debugLog(`${device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${accessory.context.adaptiveLighting},`
80
+ + ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
81
+ }
82
+ this.debugLog(`${device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
79
83
 
80
- this.LightBulb.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
81
-
82
- // handle on / off events using the On characteristic
83
- this.LightBulb.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
84
+ // Initialize LightBulb Characteristics
85
+ this.LightBulb.Service
86
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightBulb.Name)
87
+ .getCharacteristic(this.hap.Characteristic.On)
88
+ .onGet(() => {
89
+ return this.LightBulb.On;
90
+ })
91
+ .onSet(this.OnSet.bind(this));
84
92
 
85
- // handle Brightness events using the Brightness characteristic
86
93
  this.LightBulb.Service
87
94
  .getCharacteristic(this.hap.Characteristic.Brightness)
88
95
  .setProps({
@@ -96,7 +103,6 @@ export class ColorBulb extends deviceBase {
96
103
  })
97
104
  .onSet(this.BrightnessSet.bind(this));
98
105
 
99
- // handle ColorTemperature events using the ColorTemperature characteristic
100
106
  this.LightBulb.Service
101
107
  .getCharacteristic(this.hap.Characteristic.ColorTemperature)
102
108
  .setProps({
@@ -109,7 +115,6 @@ export class ColorBulb extends deviceBase {
109
115
  })
110
116
  .onSet(this.ColorTemperatureSet.bind(this));
111
117
 
112
- // handle Hue events using the Hue characteristic
113
118
  this.LightBulb.Service
114
119
  .getCharacteristic(this.hap.Characteristic.Hue)
115
120
  .setProps({
@@ -122,7 +127,6 @@ export class ColorBulb extends deviceBase {
122
127
  })
123
128
  .onSet(this.HueSet.bind(this));
124
129
 
125
- // handle Hue events using the Hue characteristic
126
130
  this.LightBulb.Service
127
131
  .getCharacteristic(this.hap.Characteristic.Saturation)
128
132
  .setProps({
@@ -134,19 +138,10 @@ export class ColorBulb extends deviceBase {
134
138
  return this.LightBulb.Saturation;
135
139
  })
136
140
  .onSet(this.SaturationSet.bind(this));
141
+ accessory.context.LightBulb.Name = this.LightBulb.Name;
137
142
 
138
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
139
- if (this.adaptiveLightingShift !== -1) {
140
- this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
141
- customTemperatureAdjustment: this.adaptiveLightingShift,
142
- });
143
- this.accessory.configureController(this.AdaptiveLightingController);
144
- this.accessory.context.adaptiveLighting = true;
145
- this.debugLog(
146
- `${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
147
- ` adaptiveLightingShift: ${this.adaptiveLightingShift}`,
148
- );
149
- }
143
+ // Retrieve initial values and updateHomekit
144
+ this.refreshStatus();
150
145
 
151
146
  // Update Homekit
152
147
  this.updateHomeKitCharacteristics();
@@ -9,7 +9,7 @@ import { skipWhile } from 'rxjs/operators';
9
9
 
10
10
  import type { SwitchBotPlatform } from '../platform.js';
11
11
  import type { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
12
- import type { device, devicesConfig, serviceData, deviceStatus} from '../settings.js';
12
+ import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
13
13
 
14
14
  /**
15
15
  * Platform Accessory
@@ -19,22 +19,26 @@ import type { device, devicesConfig, serviceData, deviceStatus} from '../setting
19
19
  export class Contact extends deviceBase {
20
20
  // Services
21
21
  private ContactSensor: {
22
+ Name: CharacteristicValue;
22
23
  Service: Service;
23
24
  ContactSensorState: CharacteristicValue;
24
25
  };
25
26
 
26
27
  private Battery: {
28
+ Name: CharacteristicValue;
27
29
  Service: Service;
28
30
  BatteryLevel: CharacteristicValue;
29
31
  StatusLowBattery: CharacteristicValue;
30
32
  };
31
33
 
32
34
  private MotionSensor?: {
35
+ Name: CharacteristicValue;
33
36
  Service: Service;
34
37
  MotionDetected: CharacteristicValue;
35
38
  };
36
39
 
37
40
  private LightSensor?: {
41
+ Name: CharacteristicValue;
38
42
  Service: Service;
39
43
  CurrentAmbientLightLevel: CharacteristicValue;
40
44
  };
@@ -55,84 +59,94 @@ export class Contact extends deviceBase {
55
59
 
56
60
  // Initialize Contact Sensor property
57
61
  this.ContactSensor = {
58
- Service: accessory.getService(this.hap.Service.ContactSensor) as Service,
59
- ContactSensorState: this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED,
62
+ Name: accessory.context.ContactSensor.Name ?? accessory.displayName,
63
+ Service: accessory.getService(this.hap.Service.ContactSensor) ?? accessory.addService(this.hap.Service.ContactSensor) as Service,
64
+ ContactSensorState: accessory.context.ContactSensorState ?? this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED,
60
65
  };
61
66
 
67
+ // Initialize ContactSensor Characteristics
68
+ this.ContactSensor.Service
69
+ .setCharacteristic(this.hap.Characteristic.Name, this.ContactSensor.Name)
70
+ .setCharacteristic(this.hap.Characteristic.StatusActive, true)
71
+ .getCharacteristic(this.hap.Characteristic.ContactSensorState)
72
+ .onGet(() => {
73
+ return this.ContactSensor.ContactSensorState;
74
+ });
75
+ accessory.context.ContactSensor.Name = this.ContactSensor.Name;
76
+
62
77
  // Initialize Battery property
63
78
  this.Battery = {
64
- Service: accessory.getService(this.hap.Service.Battery) as Service,
65
- BatteryLevel: 100,
66
- StatusLowBattery: this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
79
+ Name: accessory.context.Battery.Name ?? `${accessory.displayName} Battery`,
80
+ Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery) as Service,
81
+ BatteryLevel: accessory.context.BatteryLevel ?? 100,
82
+ StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
67
83
  };
68
84
 
69
- // Initialize Motion Sensor property
70
- if (!this.device.contact?.hide_motionsensor) {
71
- this.MotionSensor = {
72
- Service: accessory.getService(this.hap.Service.MotionSensor) as Service,
73
- MotionDetected: false,
74
- };
75
- }
76
-
77
- // Initialize Light Sensor property
78
- if (!this.device.contact?.hide_lightsensor) {
79
- this.LightSensor = {
80
- Service: accessory.getService(this.hap.Service.LightSensor) as Service,
81
- CurrentAmbientLightLevel: 0,
82
- };
83
- }
84
-
85
- // Retrieve initial values and updateHomekit
86
- this.refreshStatus();
87
-
88
- // get the Contact service if it exists, otherwise create a new Contact service
89
- // you can create multiple services for each accessory
90
- const ContactSensorService = `${accessory.displayName} Contact Sensor`;
91
- (this.ContactSensor.Service = accessory.getService(this.hap.Service.ContactSensor)
92
- || accessory.addService(this.hap.Service.ContactSensor)), ContactSensorService;
85
+ // Initialize Battery Characteristics
86
+ this.Battery.Service
87
+ .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
88
+ .setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE)
89
+ .getCharacteristic(this.hap.Characteristic.BatteryLevel)
90
+ .onGet(() => {
91
+ return this.Battery.BatteryLevel;
92
+ });
93
93
 
94
- this.ContactSensor.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
94
+ this.Battery.Service
95
+ .setCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery)
96
+ .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
97
+ .onGet(() => {
98
+ return this.Battery.StatusLowBattery;
99
+ });
100
+ accessory.context.Battery.Name = this.Battery.Name;
95
101
 
96
- // Motion Sensor Service
97
- if (device.contact?.hide_motionsensor) {
102
+ // Initialize Motion Sensor Service
103
+ if (this.device.contact?.hide_motionsensor) {
98
104
  this.debugLog(`${device.deviceType}: ${accessory.displayName} Removing Motion Sensor Service`);
99
105
  this.MotionSensor!.Service = accessory.getService(this.hap.Service.MotionSensor) as Service;
100
106
  accessory.removeService(this.MotionSensor!.Service);
101
- } else if (!this.MotionSensor?.Service && !device.contact?.hide_motionsensor) {
102
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Add Motion Sensor Service`);
103
- const MotionService = `${accessory.displayName} Motion Sensor`;
104
- (this.MotionSensor!.Service = accessory.getService(this.hap.Service.MotionSensor)
105
- || accessory.addService(this.hap.Service.MotionSensor)), MotionService;
106
-
107
- this.MotionSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, MotionService);
108
107
  } else {
109
- this.debugLog(`${device.deviceType}: ${accessory.displayName} Motion Sensor Service Not Added`);
108
+ this.MotionSensor = {
109
+ Name: accessory.context.MotionSensor.Name ?? `${accessory.displayName} Motion Sensor`,
110
+ Service: accessory.getService(this.hap.Service.MotionSensor) ?? accessory.addService(this.hap.Service.MotionSensor) as Service,
111
+ MotionDetected: accessory.context.MotionDetected ?? false,
112
+ };
113
+
114
+ // Motion Sensor Characteristics
115
+ this.MotionSensor.Service
116
+ .setCharacteristic(this.hap.Characteristic.Name, this.MotionSensor.Name)
117
+ .setCharacteristic(this.hap.Characteristic.StatusActive, true)
118
+ .getCharacteristic(this.hap.Characteristic.MotionDetected)
119
+ .onGet(() => {
120
+ return this.MotionSensor!.MotionDetected;
121
+ });
122
+ accessory.context.MotionSensor.Name = this.MotionSensor.Name;
110
123
  }
111
124
 
112
- // Light Sensor Service
125
+ // Initialize Light Sensor Service
113
126
  if (device.contact?.hide_lightsensor) {
114
127
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
115
128
  this.LightSensor!.Service = accessory.getService(this.hap.Service.LightSensor) as Service;
116
129
  accessory.removeService(this.LightSensor!.Service);
117
- } else if (!this.LightSensor?.Service && !device.contact?.hide_lightsensor) {
118
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Light Sensor Service`);
119
-
120
- const LightSensorService = `${accessory.displayName} Light Sensor`;
121
- (this.LightSensor!.Service = this.accessory.getService(this.hap.Service.LightSensor)
122
- || this.accessory.addService(this.hap.Service.LightSensor)), LightSensorService;
123
-
124
- this.LightSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, LightSensorService);
125
130
  } else {
126
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
127
- }
131
+ this.LightSensor = {
132
+ Name: accessory.context.LightSensor.Name ?? `${accessory.displayName} Light Sensor`,
133
+ Service: accessory.getService(this.hap.Service.LightSensor) ?? accessory.addService(this.hap.Service.LightSensor) as Service,
134
+ CurrentAmbientLightLevel: accessory.context.CurrentAmbientLightLevel ?? 0.0001,
135
+ };
128
136
 
129
- // Battery Service
130
- const BatteryService = `${accessory.displayName} Battery`;
131
- (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
132
- || accessory.addService(this.hap.Service.Battery)), BatteryService;
137
+ // Light Sensor Characteristics
138
+ this.LightSensor.Service
139
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightSensor.Name)
140
+ .setCharacteristic(this.hap.Characteristic.StatusActive, true)
141
+ .getCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel)
142
+ .onGet(() => {
143
+ return this.LightSensor!.CurrentAmbientLightLevel;
144
+ });
145
+ accessory.context.LightSensor.Name = this.LightSensor.Name;
146
+ }
133
147
 
134
- this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
135
- this.Battery.Service.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
148
+ // Retrieve initial values and updateHomekit
149
+ this.refreshStatus();
136
150
 
137
151
  // Retrieve initial values and updateHomekit
138
152
  this.updateHomeKitCharacteristics();
@@ -152,8 +166,8 @@ export class Contact extends deviceBase {
152
166
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
153
167
  const { detectionState, brightness, openState } = context;
154
168
  const { ContactSensorState } = this.ContactSensor;
155
- const { CurrentAmbientLightLevel } = this.LightSensor || {};
156
- const { MotionDetected } = this.MotionSensor || {};
169
+ const { CurrentAmbientLightLevel } = this.LightSensor ?? {};
170
+ const { MotionDetected } = this.MotionSensor ?? {};
157
171
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
158
172
  '(detectionState, brightness, openState) = ' +
159
173
  `Webhook:(${detectionState}, ${brightness}, ${openState}), ` +
@@ -482,11 +496,14 @@ export class Contact extends deviceBase {
482
496
 
483
497
  async apiError(e: any): Promise<void> {
484
498
  this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e);
499
+ this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
485
500
  if (!this.device.contact?.hide_motionsensor) {
486
501
  this.MotionSensor!.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
502
+ this.MotionSensor!.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
487
503
  }
488
504
  if (!this.device.contact?.hide_lightsensor) {
489
505
  this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
506
+ this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
490
507
  }
491
508
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
492
509
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);