@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
@@ -10,20 +10,23 @@ import { debounceTime, take, tap } from 'rxjs/operators';
10
10
 
11
11
  import type { SwitchBotPlatform } from '../platform.js';
12
12
  import type { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
13
- import type { device, devicesConfig, deviceStatus, serviceData} from '../settings.js';
13
+ import type { device, devicesConfig, deviceStatus, serviceData } from '../settings.js';
14
14
 
15
15
  export class RobotVacuumCleaner extends deviceBase {
16
16
  // Services
17
17
  private LightBulb: {
18
+ Name: CharacteristicValue;
18
19
  Service: Service;
19
20
  On: CharacteristicValue;
20
21
  Brightness: CharacteristicValue;
21
22
  };
22
23
 
23
24
  private Battery: {
25
+ Name: CharacteristicValue;
24
26
  Service: Service;
25
27
  BatteryLevel: CharacteristicValue;
26
28
  StatusLowBattery: CharacteristicValue;
29
+ ChargingState: CharacteristicValue;
27
30
  };
28
31
 
29
32
  // Updates
@@ -40,37 +43,24 @@ export class RobotVacuumCleaner extends deviceBase {
40
43
  this.doRobotVacuumCleanerUpdate = new Subject();
41
44
  this.robotVacuumCleanerUpdateInProgress = false;
42
45
 
43
- // Initialize Lightbulb property
46
+ // Initialize Lightbulb Service
44
47
  this.LightBulb = {
45
- Service: accessory.getService(this.hap.Service.Lightbulb) as Service,
46
- On: accessory.context.On || false,
47
- Brightness: accessory.context.Brightness || 0,
48
+ Name: accessory.context.LightBulb.Name ?? accessory.displayName,
49
+ Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb) as Service,
50
+ On: accessory.context.On ?? false,
51
+ Brightness: accessory.context.Brightness ?? 0,
48
52
  };
49
53
 
50
- // Initialize Battery property
51
- this.Battery = {
52
- Service: accessory.getService(this.hap.Service.Battery) as Service,
53
- BatteryLevel: accessory.context.BatteryLevel || 100,
54
- StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
55
- };
56
-
57
- // Retrieve initial values and updateHomekit
58
- this.refreshStatus();
59
-
60
- // get the Lightbulb service if it exists, otherwise create a new Lightbulb service
61
- // you can create multiple services for each accessory
62
- const LightBulbService = `${accessory.displayName} ${device.deviceType}`;
63
- (this.LightBulb.Service = accessory.getService(this.hap.Service.Lightbulb)
64
- || accessory.addService(this.hap.Service.Lightbulb)), LightBulbService;
65
-
66
- this.LightBulb.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
67
- // each service must implement at-minimum the "required characteristics" for the given service type
68
- // see https://developers.homebridge.io/#/service/Lightbulb
69
-
70
- // create handlers for required characteristics
71
- this.LightBulb.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
54
+ // Initialize LightBulb Characteristics
55
+ this.LightBulb.Service
56
+ .setCharacteristic(this.hap.Characteristic.Name, accessory.displayName)
57
+ .getCharacteristic(this.hap.Characteristic.On)
58
+ .onGet(() => {
59
+ return this.LightBulb.On;
60
+ })
61
+ .onSet(this.OnSet.bind(this));
72
62
 
73
- // handle Brightness events using the Brightness characteristic
63
+ // Initialize LightBulb Brightness Characteristic
74
64
  this.LightBulb.Service
75
65
  .getCharacteristic(this.hap.Characteristic.Brightness)
76
66
  .setProps({
@@ -84,14 +74,40 @@ export class RobotVacuumCleaner extends deviceBase {
84
74
  return this.LightBulb.Brightness;
85
75
  })
86
76
  .onSet(this.BrightnessSet.bind(this));
77
+ accessory.context.LightBulb.Name = this.LightBulb.Name;
87
78
 
88
- // Battery Service
89
- const BatteryService = `${accessory.displayName} Battery`;
90
- (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
91
- || accessory.addService(this.hap.Service.Battery)), BatteryService;
79
+ // Initialize Battery Service
80
+ this.Battery = {
81
+ Name: accessory.context.Battery.Name ?? `${accessory.displayName} Battery`,
82
+ Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery) as Service,
83
+ BatteryLevel: accessory.context.BatteryLevel ?? 100,
84
+ StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
85
+ ChargingState: accessory.context.ChargingState ?? this.hap.Characteristic.ChargingState.NOT_CHARGING,
86
+ };
87
+
88
+ // Initialize Battery Characteristics
89
+ this.Battery.Service
90
+ .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
91
+ .getCharacteristic(this.hap.Characteristic.BatteryLevel)
92
+ .onGet(() => {
93
+ return this.Battery.BatteryLevel;
94
+ });
92
95
 
93
- this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
96
+ this.Battery.Service
97
+ .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
98
+ .onGet(() => {
99
+ return this.Battery.StatusLowBattery;
100
+ });
94
101
 
102
+ this.Battery.Service
103
+ .getCharacteristic(this.hap.Characteristic.ChargingState)
104
+ .onGet(() => {
105
+ return this.Battery.ChargingState;
106
+ });
107
+ accessory.context.Battery.Name = this.Battery.Name;
108
+
109
+ // Retrieve initial values and updateHomekit
110
+ this.refreshStatus();
95
111
  // Update Homekit
96
112
  this.updateHomeKitCharacteristics();
97
113
 
@@ -108,14 +124,16 @@ export class RobotVacuumCleaner extends deviceBase {
108
124
  this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
109
125
  try {
110
126
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
111
- const { onlineStatus, battery } = context;
127
+ const { onlineStatus, battery, workingStatus } = context;
112
128
  const { On } = this.LightBulb;
113
- const { BatteryLevel } = this.Battery;
129
+ const { BatteryLevel, ChargingState } = this.Battery;
114
130
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
115
- '(onlineStatus, battery) = ' +
116
- `Webhook:(${onlineStatus}, ${battery}), ` +
117
- `current:(${On}, ${BatteryLevel})`);
131
+ '(onlineStatus, battery, workingStatus) = ' +
132
+ `Webhook:(${onlineStatus}, ${battery}, ${workingStatus}), ` +
133
+ `current:(${On}, ${BatteryLevel}, ${ChargingState})`);
118
134
  this.LightBulb.On = onlineStatus === 'online' ? true : false;
135
+ this.Battery.ChargingState = workingStatus === 'Charging'
136
+ ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
119
137
  this.Battery.BatteryLevel = battery;
120
138
  this.updateHomeKitCharacteristics();
121
139
  } catch (e: any) {
@@ -189,6 +207,8 @@ export class RobotVacuumCleaner extends deviceBase {
189
207
 
190
208
  // BatteryLevel
191
209
  this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
210
+
211
+ // StatusLowBattery
192
212
  if (this.Battery.BatteryLevel < 10) {
193
213
  this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
194
214
  } else {
@@ -197,8 +217,11 @@ export class RobotVacuumCleaner extends deviceBase {
197
217
  if (Number.isNaN(this.Battery.BatteryLevel)) {
198
218
  this.Battery.BatteryLevel = 100;
199
219
  }
220
+ // ChargingState
221
+ this.Battery.ChargingState = deviceStatus.body.workingStatus === 'Charging'
222
+ ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
200
223
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
201
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
224
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}, ChargingState: ${this.Battery.ChargingState}`);
202
225
 
203
226
  // Firmware Version
204
227
  const version = deviceStatus.body.version?.toString();
@@ -219,8 +242,8 @@ export class RobotVacuumCleaner extends deviceBase {
219
242
  async refreshStatus(): Promise<void> {
220
243
  if (!this.device.enableCloudService && this.OpenAPI) {
221
244
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
222
- /*} else if (this.BLE) {
223
- await this.BLERefreshStatus();*/
245
+ } else if (this.BLE) {
246
+ await this.BLERefreshStatus();
224
247
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
225
248
  await this.openAPIRefreshStatus();
226
249
  } else {
@@ -244,21 +267,19 @@ export class RobotVacuumCleaner extends deviceBase {
244
267
  this.getCustomBLEAddress(switchbot);
245
268
  // Start to monitor advertisement packets
246
269
  (async () => {
247
- await switchbot.startScan({
248
- model: '?',
249
- id: this.device.bleMac,
250
- });
270
+ // Start to monitor advertisement packets
271
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
251
272
  // Set an event handler
252
273
  switchbot.onadvertisement = (ad: any) => {
253
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
254
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
255
- if (this.device.bleMac === ad.address && ad.serviceData.model === '?') {
274
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
275
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
276
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
256
277
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
257
278
  } else {
258
279
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
259
280
  }
260
281
  };
261
- // Wait 1 seconds
282
+ // Wait 10 seconds
262
283
  await switchbot.wait(this.scanDuration * 1000);
263
284
  // Stop to monitor
264
285
  await switchbot.stopScan();
@@ -266,63 +287,16 @@ export class RobotVacuumCleaner extends deviceBase {
266
287
  await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
267
288
  await this.updateHomeKitCharacteristics();
268
289
  })();
269
- /*if (switchbot !== false) {
270
- switchbot
271
- .startScan({
272
- model: this.BLEmodel(),
273
- id: this.device.bleMac,
274
- })
275
- .then(async () => {
276
- // Set an event handler
277
- switchbot.onadvertisement = async (ad: ad) => {
278
- this.debugLog(
279
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
280
- ` BLE Address Found: ${ad.address}`,
281
- );
282
- serviceData.state = ad.serviceData.state;
283
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
284
- this.debugLog(
285
- `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
286
- `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
287
- `wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
288
- );
289
-
290
- if (ad.serviceData) {
291
- this.BLE_IsConnected = true;
292
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
293
- await this.stopScanning(switchbot);
294
- } else {
295
- this.BLE_IsConnected = false;
296
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
297
- }
298
- };
299
- // Wait
300
- return await sleep(this.scanDuration * 1000);
301
- })
302
- .then(async () => {
303
- // Stop to monitor
304
- await this.stopScanning(switchbot);
305
- })
306
- .catch(async (e: any) => {
307
- this.apiError(e);
308
- this.errorLog(
309
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
310
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
311
- );
312
- await this.BLERefreshConnection(switchbot);
313
- });
314
- } else {
290
+ if (switchbot === undefined) {
315
291
  await this.BLERefreshConnection(switchbot);
316
- }*/
292
+ }
317
293
  }
318
294
 
319
- async openAPIRefreshStatus() {
295
+ async openAPIRefreshStatus(): Promise<void> {
320
296
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
321
297
  try {
322
298
  const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
323
- `${Devices}/${this.device.deviceId}/status`, {
324
- headers: this.platform.generateHeaders(),
325
- });
299
+ `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
326
300
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
327
301
  const deviceStatus: any = await body.json();
328
302
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -608,6 +582,15 @@ export class RobotVacuumCleaner extends deviceBase {
608
582
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
609
583
  + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
610
584
  }
585
+ // ChargingState
586
+ if (this.Battery.ChargingState === undefined) {
587
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
588
+ } else {
589
+ this.accessory.context.ChargingState = this.Battery.ChargingState;
590
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
591
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
592
+ + ` ChargingState: ${this.Battery.ChargingState}`);
593
+ }
611
594
  }
612
595
 
613
596
  async BLEPushConnection() {
@@ -19,6 +19,7 @@ import type { device, devicesConfig, serviceData, deviceStatus} from '../setting
19
19
  export class WaterDetector extends deviceBase {
20
20
  // Services
21
21
  private Battery: {
22
+ Name: CharacteristicValue
22
23
  Service: Service;
23
24
  BatteryLevel: CharacteristicValue;
24
25
  StatusLowBattery: CharacteristicValue;
@@ -26,6 +27,7 @@ export class WaterDetector extends deviceBase {
26
27
  };
27
28
 
28
29
  private LeakSensor?: {
30
+ Name: CharacteristicValue;
29
31
  Service: Service;
30
32
  StatusActive: CharacteristicValue;
31
33
  LeakDetected: CharacteristicValue;
@@ -48,49 +50,57 @@ export class WaterDetector extends deviceBase {
48
50
  this.doWaterDetectorUpdate = new Subject();
49
51
  this.WaterDetectorUpdateInProgress = false;
50
52
 
51
- // Initialize Battery property
53
+ // Initialize Battery Service
52
54
  this.Battery = {
53
- Service: accessory.getService(this.hap.Service.Battery) as Service,
54
- BatteryLevel: accessory.context.BatteryLevel || 100,
55
- StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
56
- ChargingState: accessory.context.ChargingState || this.hap.Characteristic.ChargingState.NOT_CHARGEABLE,
55
+ Name: accessory.context.Battery.Name ?? `${accessory.displayName} Battery`,
56
+ Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery) as Service,
57
+ BatteryLevel: accessory.context.BatteryLevel ?? 100,
58
+ StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
59
+ ChargingState: accessory.context.ChargingState ?? this.hap.Characteristic.ChargingState.NOT_CHARGEABLE,
57
60
  };
58
61
 
59
- // Initialize Leak Sensor property
60
- if (!this.device.waterdetector?.hide_leak) {
61
- this.LeakSensor = {
62
- Service: accessory.getService(this.hap.Service.LeakSensor) as Service,
63
- StatusActive: accessory.context.StatusActive || false,
64
- LeakDetected: accessory.context.LeakDetected || this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED,
65
- };
66
- }
62
+ // Initialize Battery Characteristic
63
+ this.Battery.Service
64
+ .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
65
+ .setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE)
66
+ .getCharacteristic(this.hap.Characteristic.BatteryLevel)
67
+ .onGet(() => {
68
+ return this.Battery.StatusLowBattery;
69
+ });
67
70
 
68
- // Retrieve initial values and updateHomekit
69
- this.refreshStatus();
71
+ this.Battery.Service
72
+ .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
73
+ .onGet(() => {
74
+ return this.Battery.StatusLowBattery;
75
+ });
76
+ accessory.context.Battery.Name = this.Battery.Name;
70
77
 
71
- // Leak Sensor Service
78
+ // Initialize Leak Sensor Service
72
79
  if (device.waterdetector?.hide_leak) {
73
80
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leak Sensor Service`);
74
81
  this.LeakSensor!.Service = this.accessory.getService(this.hap.Service.LeakSensor) as Service;
75
82
  accessory.removeService(this.LeakSensor!.Service);
76
- } else if (!this.LeakSensor?.Service) {
77
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Leak Sensor Service`);
78
- const LeakSensorService = `${accessory.displayName} Leak Sensor`;
79
- (this.LeakSensor!.Service = this.accessory.getService(this.hap.Service.LeakSensor)
80
- || this.accessory.addService(this.hap.Service.LeakSensor)), LeakSensorService;
81
-
82
- this.LeakSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, LeakSensorService);
83
83
  } else {
84
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Leak Sensor Service Not Added`);
85
- }
84
+ this.LeakSensor = {
85
+ Name: accessory.context.LeakSensor.Name ?? `${accessory.displayName} Leak Sensor`,
86
+ Service: accessory.getService(this.hap.Service.LeakSensor) ?? this.accessory.addService(this.hap.Service.LeakSensor) as Service,
87
+ StatusActive: accessory.context.StatusActive ?? false,
88
+ LeakDetected: accessory.context.LeakDetected ?? this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED,
89
+ };
86
90
 
87
- // Battery Service
88
- const BatteryService = `${accessory.displayName} Battery`;
89
- (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
90
- || accessory.addService(this.hap.Service.Battery)), BatteryService;
91
+ // Initialize LeakSensor Characteristic
92
+ this.LeakSensor!.Service
93
+ .setCharacteristic(this.hap.Characteristic.Name, this.LeakSensor.Name)
94
+ .setCharacteristic(this.hap.Characteristic.StatusActive, true)
95
+ .getCharacteristic(this.hap.Characteristic.LeakDetected)
96
+ .onGet(() => {
97
+ return this.LeakSensor!.LeakDetected;
98
+ });
99
+ accessory.context.LeakSensor.Name = this.LeakSensor.Name;
100
+ }
91
101
 
92
- this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
93
- this.Battery.Service.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
102
+ // Retrieve initial values and updateHomekit
103
+ this.refreshStatus();
94
104
 
95
105
  // Retrieve initial values and updateHomekit
96
106
  this.updateHomeKitCharacteristics();