@switchbot/homebridge-switchbot 3.5.0-beta.23 → 3.5.0-beta.25

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 (150) hide show
  1. package/config.schema.json +23 -2
  2. package/dist/device/blindtilt.d.ts +21 -98
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +332 -785
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +25 -75
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +566 -778
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +13 -71
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +168 -606
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +13 -78
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +218 -655
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +14 -69
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +196 -553
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +17 -90
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +324 -810
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +58 -0
  27. package/dist/device/device.d.ts.map +1 -0
  28. package/dist/device/device.js +629 -0
  29. package/dist/device/device.js.map +1 -0
  30. package/dist/device/hub.d.ts +15 -63
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +223 -610
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +10 -69
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +192 -595
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +11 -68
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +156 -599
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +10 -72
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +204 -591
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +15 -66
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +199 -566
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +13 -67
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +152 -592
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +13 -68
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +151 -599
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +13 -73
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +129 -540
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -61
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +71 -476
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +11 -66
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +122 -471
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/device/waterdetector.d.ts +9 -66
  71. package/dist/device/waterdetector.d.ts.map +1 -1
  72. package/dist/device/waterdetector.js +105 -478
  73. package/dist/device/waterdetector.js.map +1 -1
  74. package/dist/irdevice/airconditioner.d.ts +5 -36
  75. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  76. package/dist/irdevice/airconditioner.js +228 -337
  77. package/dist/irdevice/airconditioner.js.map +1 -1
  78. package/dist/irdevice/airpurifier.d.ts +5 -34
  79. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  80. package/dist/irdevice/airpurifier.js +19 -267
  81. package/dist/irdevice/airpurifier.js.map +1 -1
  82. package/dist/irdevice/camera.d.ts +5 -34
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +13 -261
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +5 -34
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js +13 -260
  89. package/dist/irdevice/fan.js.map +1 -1
  90. package/dist/irdevice/irdevice.d.ts +39 -0
  91. package/dist/irdevice/irdevice.d.ts.map +1 -0
  92. package/dist/irdevice/irdevice.js +502 -0
  93. package/dist/irdevice/irdevice.js.map +1 -0
  94. package/dist/irdevice/light.d.ts +5 -34
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +13 -267
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +18 -48
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +349 -551
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +7 -40
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +79 -325
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +6 -36
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +38 -288
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +6 -36
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +39 -291
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/settings.d.ts +15 -2
  115. package/dist/settings.d.ts.map +1 -1
  116. package/dist/utils.d.ts +68 -0
  117. package/dist/utils.d.ts.map +1 -1
  118. package/dist/utils.js +71 -0
  119. package/dist/utils.js.map +1 -1
  120. package/package.json +9 -9
  121. package/src/device/blindtilt.ts +356 -809
  122. package/src/device/bot.ts +601 -865
  123. package/src/device/ceilinglight.ts +182 -628
  124. package/src/device/colorbulb.ts +235 -683
  125. package/src/device/contact.ts +220 -575
  126. package/src/device/curtain.ts +344 -836
  127. package/src/device/device.ts +655 -0
  128. package/src/device/hub.ts +246 -636
  129. package/src/device/humidifier.ts +224 -645
  130. package/src/device/iosensor.ts +181 -625
  131. package/src/device/lightstrip.ts +230 -622
  132. package/src/device/lock.ts +236 -607
  133. package/src/device/meter.ts +177 -616
  134. package/src/device/meterplus.ts +175 -624
  135. package/src/device/motion.ts +147 -563
  136. package/src/device/plug.ts +81 -505
  137. package/src/device/robotvacuumcleaner.ts +139 -494
  138. package/src/device/waterdetector.ts +126 -506
  139. package/src/irdevice/airconditioner.ts +228 -353
  140. package/src/irdevice/airpurifier.ts +23 -282
  141. package/src/irdevice/camera.ts +18 -277
  142. package/src/irdevice/fan.ts +18 -275
  143. package/src/irdevice/irdevice.ts +518 -0
  144. package/src/irdevice/light.ts +18 -283
  145. package/src/irdevice/other.ts +394 -621
  146. package/src/irdevice/tv.ts +97 -344
  147. package/src/irdevice/vacuumcleaner.ts +46 -310
  148. package/src/irdevice/waterheater.ts +47 -315
  149. package/src/settings.ts +20 -3
  150. package/src/utils.ts +71 -0
@@ -1,94 +1,46 @@
1
- import { hostname } from 'os';
2
- import { sleep } from '../utils.js';
3
- import { MqttClient } from 'mqtt';
4
- import asyncmqtt from 'async-mqtt';
5
- import { interval, Subject } from 'rxjs';
6
- import { skipWhile } from 'rxjs/operators';
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * iosensor.ts: @switchbot/homebridge-switchbot.
4
+ */
5
+ import { deviceBase } from './device.js';
7
6
  import { SwitchBotPlatform } from '../platform.js';
8
- import { Service, PlatformAccessory, Units, CharacteristicValue, API, Logging, HAP } from 'homebridge';
9
- import { device, devicesConfig, serviceData, temperature, deviceStatus, Devices, SwitchBotPlatformConfig } from '../settings.js';
7
+ import { Subject, interval, skipWhile } from 'rxjs';
8
+ import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
9
+ import { device, devicesConfig, serviceData, deviceStatus, Devices } from '../settings.js';
10
10
 
11
11
  /**
12
12
  * Platform Accessory
13
13
  * An instance of this class is created for each accessory your platform registers
14
14
  * Each accessory may expose multiple services of different service types.
15
15
  */
16
- export class IOSensor {
17
- public readonly api: API;
18
- public readonly log: Logging;
19
- public readonly config!: SwitchBotPlatformConfig;
20
- protected readonly hap: HAP;
16
+ export class IOSensor extends deviceBase {
21
17
  // Services
22
- batteryService: Service;
23
- humidityservice?: Service;
24
- temperatureservice?: Service;
25
-
26
- // Characteristic Values
27
- BatteryLevel!: CharacteristicValue;
28
- StatusLowBattery!: CharacteristicValue;
29
- FirmwareRevision!: CharacteristicValue;
30
- CurrentTemperature?: CharacteristicValue;
31
- CurrentRelativeHumidity!: CharacteristicValue;
32
-
33
- // OpenAPI Status
34
- OpenAPI_BatteryLevel: deviceStatus['battery'];
35
- OpenAPI_FirmwareRevision: deviceStatus['version'];
36
- OpenAPI_CurrentTemperature: deviceStatus['temperature'];
37
- OpenAPI_CurrentRelativeHumidity: deviceStatus['humidity'];
38
-
39
- // BLE Status
40
- BLE_Celsius!: temperature['c'];
41
- BLE_Fahrenheit!: temperature['f'];
42
- BLE_BatteryLevel!: serviceData['battery'];
43
- BLE_CurrentTemperature!: serviceData['temperature'];
44
- BLE_CurrentRelativeHumidity!: serviceData['humidity'];
45
-
46
- // BLE Others
47
- BLE_IsConnected?: boolean;
48
-
49
- //MQTT stuff
50
- mqttClient: MqttClient | null = null;
51
-
52
- // EVE history service handler
53
- historyService?: any;
54
-
55
- // Config
56
- scanDuration!: number;
57
- deviceLogging!: string;
58
- deviceRefreshRate!: number;
59
- maxRetries!: number;
60
- delayBetweenRetries!: number;
18
+ private Battery!: {
19
+ Service: Service;
20
+ BatteryLevel: CharacteristicValue;
21
+ StatusLowBattery: CharacteristicValue;
22
+ };
23
+
24
+ private HumiditySensor?: {
25
+ Service: Service;
26
+ CurrentRelativeHumidity: CharacteristicValue;
27
+ };
28
+
29
+ private TemperatureSensor?: {
30
+ Service: Service;
31
+ CurrentTemperature: CharacteristicValue;
32
+ };
61
33
 
62
34
  // Updates
63
35
  ioSensorUpdateInProgress!: boolean;
64
36
  doIOSensorUpdate: Subject<void>;
65
37
 
66
- // Connection
67
- private readonly OpenAPI: boolean;
68
- private readonly BLE: boolean;
69
-
70
38
  constructor(
71
- private readonly platform: SwitchBotPlatform,
72
- private accessory: PlatformAccessory,
73
- public device: device & devicesConfig,
39
+ readonly platform: SwitchBotPlatform,
40
+ accessory: PlatformAccessory,
41
+ device: device & devicesConfig,
74
42
  ) {
75
- this.api = this.platform.api;
76
- this.log = this.platform.log;
77
- this.config = this.platform.config;
78
- this.hap = this.api.hap;
79
- // Connection
80
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
81
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
82
- // default placeholders
83
- this.deviceLogs(device);
84
- this.scan(device);
85
- this.refreshRate(device);
86
- this.deviceContext();
87
- this.setupHistoryService(device);
88
- this.setupMqtt(device);
89
- this.deviceRetry(device);
90
- this.deviceConfig(device);
91
-
43
+ super(platform, accessory, device);
92
44
  // this is subject we use to track when we need to POST changes to the SwitchBot API
93
45
  this.doIOSensorUpdate = new Subject();
94
46
  this.ioSensorUpdateInProgress = false;
@@ -96,30 +48,42 @@ export class IOSensor {
96
48
  // Retrieve initial values and updateHomekit
97
49
  this.refreshStatus();
98
50
 
99
- // set accessory information
100
- accessory
101
- .getService(this.hap.Service.AccessoryInformation)!
102
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
103
- .setCharacteristic(this.hap.Characteristic.Model, 'WoIOSensor')
104
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
105
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
51
+ // Initialize Temperature Sensor property
52
+ if (!device.iosensor?.hide_temperature) {
53
+ this.TemperatureSensor = {
54
+ Service: this.accessory.addService(this.hap.Service.TemperatureSensor),
55
+ CurrentTemperature: accessory.context.CurrentTemperature || 30,
56
+ };
57
+ }
58
+
59
+ // Initialize Humidity Sensor property
60
+ if (!device.iosensor?.hide_humidity) {
61
+ this.HumiditySensor = {
62
+ Service: this.accessory.addService(this.hap.Service.HumiditySensor),
63
+ CurrentRelativeHumidity: accessory.context.CurrentRelativeHumidity || 50,
64
+ };
65
+ }
66
+
67
+ // Initialize Battery property
68
+ this.Battery = {
69
+ Service: this.accessory.addService(this.hap.Service.Battery),
70
+ BatteryLevel: accessory.context.BatteryLevel || 100,
71
+ StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
72
+ };
106
73
 
107
74
  // Temperature Sensor Service
108
- if (device.meter?.hide_temperature) {
75
+ if (device.iosensor?.hide_temperature) {
109
76
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
110
- this.temperatureservice = this.accessory.getService(this.hap.Service.TemperatureSensor);
111
- accessory.removeService(this.temperatureservice!);
112
- } else if (!this.temperatureservice) {
77
+ this.TemperatureSensor!.Service = this.accessory.getService(this.hap.Service.TemperatureSensor) as Service;
78
+ accessory.removeService(this.TemperatureSensor!.Service);
79
+ } else if (!this.TemperatureSensor!.Service) {
113
80
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
114
- const temperatureservice = `${accessory.displayName} Temperature Sensor`;
115
- (this.temperatureservice = this.accessory.getService(this.hap.Service.TemperatureSensor)
116
- || this.accessory.addService(this.hap.Service.TemperatureSensor)), temperatureservice;
81
+ const TemperatureSensorService = `${accessory.displayName} Temperature Sensor`;
82
+ (this.TemperatureSensor!.Service = this.accessory.getService(this.hap.Service.TemperatureSensor)
83
+ || this.accessory.addService(this.hap.Service.TemperatureSensor)), TemperatureSensorService;
117
84
 
118
- this.temperatureservice.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
119
- if (!this.temperatureservice.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
120
- this.temperatureservice.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
121
- }
122
- this.temperatureservice
85
+ this.TemperatureSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, TemperatureSensorService);
86
+ this.TemperatureSensor!.Service
123
87
  .getCharacteristic(this.hap.Characteristic.CurrentTemperature)
124
88
  .setProps({
125
89
  unit: Units['CELSIUS'],
@@ -129,49 +93,43 @@ export class IOSensor {
129
93
  minStep: 0.1,
130
94
  })
131
95
  .onGet(() => {
132
- return this.CurrentTemperature!;
96
+ return this.TemperatureSensor!.CurrentTemperature!;
133
97
  });
134
98
  } else {
135
99
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Temperature Sensor Service Not Added`);
136
100
  }
137
101
 
138
102
  // Humidity Sensor Service
139
- if (device.meter?.hide_humidity) {
103
+ if (device.iosensor?.hide_humidity) {
140
104
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
141
- this.humidityservice = this.accessory.getService(this.hap.Service.HumiditySensor);
142
- accessory.removeService(this.humidityservice!);
143
- } else if (!this.humidityservice) {
105
+ this.HumiditySensor!.Service = this.accessory.getService(this.hap.Service.HumiditySensor) as Service;
106
+ accessory.removeService(this.HumiditySensor!.Service);
107
+ } else if (!this.HumiditySensor!.Service) {
144
108
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
145
- const humidityservice = `${accessory.displayName} Humidity Sensor`;
146
- (this.humidityservice = this.accessory.getService(this.hap.Service.HumiditySensor)
147
- || this.accessory.addService(this.hap.Service.HumiditySensor)), humidityservice;
109
+ const HumiditySensorService = `${accessory.displayName} Humidity Sensor`;
110
+ (this.HumiditySensor!.Service = this.accessory.getService(this.hap.Service.HumiditySensor)
111
+ || this.accessory.addService(this.hap.Service.HumiditySensor)), HumiditySensorService;
148
112
 
149
- this.humidityservice.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
150
- if (!this.humidityservice.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
151
- this.humidityservice.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
152
- }
153
- this.humidityservice
113
+ this.HumiditySensor!.Service.setCharacteristic(this.hap.Characteristic.Name, HumiditySensorService);
114
+ this.HumiditySensor!.Service
154
115
  .getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
155
116
  .setProps({
156
117
  minStep: 0.1,
157
118
  })
158
119
  .onGet(() => {
159
- return this.CurrentRelativeHumidity;
120
+ return this.HumiditySensor!.CurrentRelativeHumidity;
160
121
  });
161
122
  } else {
162
123
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Humidity Sensor Service Not Added`);
163
124
  }
164
125
 
165
126
  // Battery Service
166
- const batteryService = `${accessory.displayName} Battery`;
167
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
168
- || accessory.addService(this.hap.Service.Battery)), batteryService;
127
+ const BatteryService = `${accessory.displayName} Battery`;
128
+ (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
129
+ || accessory.addService(this.hap.Service.Battery)), BatteryService;
169
130
 
170
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
171
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
172
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
173
- }
174
- this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
131
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
132
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
175
133
 
176
134
  // Retrieve initial values and updateHomekit
177
135
  this.updateHomeKitCharacteristics();
@@ -191,13 +149,18 @@ export class IOSensor {
191
149
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
192
150
  if (context.scale === 'CELSIUS') {
193
151
  const { temperature, humidity } = context;
194
- const { CurrentTemperature, CurrentRelativeHumidity } = this;
152
+ const { CurrentTemperature } = this.TemperatureSensor || { CurrentTemperature: undefined };
153
+ const { CurrentRelativeHumidity } = this.HumiditySensor || { CurrentRelativeHumidity: undefined };
195
154
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
196
155
  '(temperature, humidity) = ' +
197
156
  `Webhook:(${temperature}, ${humidity}), ` +
198
157
  `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
199
- this.CurrentRelativeHumidity = humidity;
200
- this.CurrentTemperature = temperature;
158
+ if (this.device.iosensor?.hide_humidity) {
159
+ this.HumiditySensor!.CurrentRelativeHumidity = humidity;
160
+ }
161
+ if (this.device.iosensor?.hide_temperature) {
162
+ this.TemperatureSensor!.CurrentTemperature = temperature;
163
+ }
201
164
  this.updateHomeKitCharacteristics();
202
165
  }
203
166
  } catch (e: any) {
@@ -208,89 +171,61 @@ export class IOSensor {
208
171
  }
209
172
  }
210
173
 
211
- /**
212
- * Parse the device status from the SwitchBot api
213
- */
214
- async parseStatus(): Promise<void> {
215
- if (!this.device.enableCloudService && this.OpenAPI) {
216
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
217
- } else if (this.BLE) {
218
- await this.BLEparseStatus();
219
- } else if (this.OpenAPI && this.platform.config.credentials?.token) {
220
- await this.openAPIparseStatus();
221
- } else {
222
- await this.offlineOff();
223
- this.debugWarnLog(
224
- `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
225
- );
226
- }
227
- }
228
-
229
- async BLEparseStatus(): Promise<void> {
174
+ async BLEparseStatus(serviceData: serviceData): Promise<void> {
230
175
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
231
176
 
232
177
  // Battery
233
- this.BatteryLevel = Number(this.BLE_BatteryLevel);
234
- if (this.BatteryLevel < 15) {
235
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
178
+ this.Battery.BatteryLevel = Number(serviceData.battery);
179
+ if (this.Battery.BatteryLevel < 10) {
180
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
236
181
  } else {
237
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
182
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
238
183
  }
239
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
184
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
240
185
 
241
186
  // Humidity
242
- if (!this.device.meter?.hide_humidity) {
243
- this.CurrentRelativeHumidity = this.BLE_CurrentRelativeHumidity!;
244
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
187
+ if (!this.device.iosensor?.hide_humidity) {
188
+ this.HumiditySensor!.CurrentRelativeHumidity = serviceData.humidity!;
189
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
245
190
  }
246
191
 
247
192
  // Current Temperature
248
- if (!this.device.meter?.hide_temperature) {
249
- this.BLE_Celsius < 0 ? 0 : this.BLE_Celsius > 100 ? 100 : this.BLE_Celsius;
250
- this.CurrentTemperature = this.BLE_Celsius;
251
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
193
+ if (!this.device.iosensor?.hide_temperature) {
194
+ serviceData.temperature!.c < 0 ? 0 : serviceData.temperature!.c > 100 ? 100 : serviceData.temperature!.c;
195
+ this.TemperatureSensor!.CurrentTemperature = serviceData.temperature!.c;
196
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
252
197
  }
253
198
  }
254
199
 
255
- async openAPIparseStatus(): Promise<void> {
200
+ async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
256
201
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
257
- // Battery
258
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
259
- if (this.BatteryLevel < 15) {
260
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
202
+
203
+ // BatteryLevel
204
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
205
+ if (this.Battery.BatteryLevel < 10) {
206
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
261
207
  } else {
262
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
208
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
263
209
  }
264
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
210
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
211
+ this.Battery.BatteryLevel = 100;
212
+ }
213
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
214
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
265
215
 
266
216
  // Current Relative Humidity
267
- if (!this.device.meter?.hide_humidity) {
268
- this.CurrentRelativeHumidity = this.OpenAPI_CurrentRelativeHumidity!;
269
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
217
+ if (!this.device.iosensor?.hide_humidity) {
218
+ this.HumiditySensor!.CurrentRelativeHumidity = deviceStatus.body.humidity!;
219
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
270
220
  }
271
221
 
272
222
  // Current Temperature
273
- if (!this.device.meter?.hide_temperature) {
274
- this.CurrentTemperature = this.OpenAPI_CurrentTemperature!;
275
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
276
- }
277
-
278
- // BatteryLevel
279
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
280
- if (this.BatteryLevel < 10) {
281
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
282
- } else {
283
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
223
+ if (!this.device.iosensor?.hide_temperature) {
224
+ this.TemperatureSensor!.CurrentTemperature = deviceStatus.body.temperature!;
225
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
284
226
  }
285
- if (Number.isNaN(this.BatteryLevel)) {
286
- this.BatteryLevel = 100;
287
- }
288
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
289
- + ` StatusLowBattery: ${this.StatusLowBattery}`);
290
-
291
227
  // FirmwareRevision
292
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision!;
293
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
228
+ this.accessory.context.FirmwareRevision = deviceStatus.body.version;
294
229
  }
295
230
 
296
231
  /**
@@ -325,96 +260,35 @@ export class IOSensor {
325
260
  // Start to monitor advertisement packets
326
261
  (async () => {
327
262
  // Start to monitor advertisement packets
328
- await switchbot.startScan({
329
- model: 'w',
330
- id: this.device.bleMac,
331
- });
263
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
332
264
  // Set an event handler
333
265
  switchbot.onadvertisement = (ad: any) => {
334
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
335
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
336
- if (this.device.bleMac === ad.address && ad.serviceData.model === 'w') {
266
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
267
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
268
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
337
269
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
338
- this.BLE_CurrentTemperature = ad.serviceData.temperature;
339
- this.BLE_Celsius = ad.serviceData.temperature!.c;
340
- this.BLE_Fahrenheit = ad.serviceData.temperature!.f;
341
- this.BLE_BatteryLevel = ad.serviceData.battery;
342
270
  } else {
343
271
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
344
272
  }
345
273
  };
346
- // Wait 1 seconds
274
+ // Wait 10 seconds
347
275
  await switchbot.wait(this.scanDuration * 1000);
348
276
  // Stop to monitor
349
277
  await switchbot.stopScan();
350
278
  // Update HomeKit
351
- await this.BLEparseStatus();
279
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
352
280
  await this.updateHomeKitCharacteristics();
353
281
  })();
354
- /*if (switchbot !== false) {
355
- switchbot
356
- .startScan({
357
- model: 'w',
358
- id: this.device.bleMac,
359
- })
360
- .then(async () => {
361
- // Set an event handler
362
- switchbot.onadvertisement = async (ad: ad) => {
363
- this.debugLog(
364
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
365
- ` BLE Address Found: ${ad.address}`,
366
- );
367
- if (ad.serviceData.humidity! > 0) {
368
- // reject unreliable data
369
- this.BLE_CurrentRelativeHumidity = ad.serviceData.humidity;
370
- }
371
- this.BLE_CurrentTemperature = ad.serviceData.temperature;
372
- this.BLE_Celsius = ad.serviceData.temperature!.c;
373
- this.BLE_Fahrenheit = ad.serviceData.temperature!.f;
374
- this.BLE_BatteryLevel = ad.serviceData.battery;
375
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
376
- this.debugLog(
377
- `${this.device.deviceType}: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, ` +
378
- `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, ` +
379
- `battery: ${ad.serviceData.battery}`,
380
- );
381
-
382
- if (ad.serviceData) {
383
- this.BLE_IsConnected = true;
384
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
385
- await this.stopScanning(switchbot);
386
- } else {
387
- this.BLE_IsConnected = false;
388
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
389
- }
390
- };
391
- // Wait
392
- return await sleep(this.scanDuration * 1000);
393
- })
394
- .then(async () => {
395
- // Stop to monitor
396
- await this.stopScanning(switchbot);
397
- })
398
- .catch(async (e: any) => {
399
- this.apiError(e);
400
- this.errorLog(
401
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
402
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
403
- );
404
- await this.BLERefreshConnection(switchbot);
405
- });
406
- } else {
282
+ if (switchbot === undefined) {
407
283
  await this.BLERefreshConnection(switchbot);
408
- }*/
284
+ }
409
285
  }
410
286
 
411
287
  async openAPIRefreshStatus(): Promise<void> {
412
288
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
413
289
  try {
414
- const { body, statusCode } = await this.platform.retryRequest(this.maxRetries, this.delayBetweenRetries,
415
- `${Devices}/${this.device.deviceId}/status`, {
416
- headers: this.platform.generateHeaders(),
417
- });
290
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
291
+ `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
418
292
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
419
293
  const deviceStatus: any = await body.json();
420
294
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -422,11 +296,7 @@ export class IOSensor {
422
296
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
423
297
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
424
298
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
425
- this.OpenAPI_CurrentRelativeHumidity = deviceStatus.body.humidity!;
426
- this.OpenAPI_CurrentTemperature = deviceStatus.body.temperature!;
427
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
428
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
429
- this.openAPIparseStatus();
299
+ this.openAPIparseStatus(deviceStatus);
430
300
  this.updateHomeKitCharacteristics();
431
301
  } else {
432
302
  this.statusCode(statusCode);
@@ -447,63 +317,73 @@ export class IOSensor {
447
317
  async updateHomeKitCharacteristics(): Promise<void> {
448
318
  const mqttmessage: string[] = [];
449
319
  const entry = { time: Math.round(new Date().valueOf() / 1000) };
450
- if (!this.device.meter?.hide_humidity) {
451
- if (this.CurrentRelativeHumidity === undefined) {
452
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
320
+
321
+ // CurrentRelativeHumidity
322
+ if (!this.device.iosensor?.hide_humidity) {
323
+ if (this.HumiditySensor!.CurrentRelativeHumidity === undefined) {
324
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
325
+ + ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
453
326
  } else {
454
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
455
- this.humidityservice?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
456
- this.debugLog(
457
- `${this.device.deviceType}: ${this.accessory.displayName}` +
458
- ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
459
- );
327
+ this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity;
328
+ this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
329
+ this.HumiditySensor!.CurrentRelativeHumidity);
330
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
331
+ + ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
460
332
  if (this.device.mqttURL) {
461
- mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
333
+ mqttmessage.push(`"humidity": ${this.HumiditySensor!.CurrentRelativeHumidity}`);
462
334
  }
463
335
  if (this.device.history) {
464
- entry['humidity'] = this.CurrentRelativeHumidity;
336
+ entry['humidity'] = this.HumiditySensor!.CurrentRelativeHumidity;
465
337
  }
466
338
  }
467
339
  }
468
- if (!this.device.meter?.hide_temperature) {
469
- if (this.CurrentTemperature === undefined) {
470
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
340
+
341
+ // CurrentTemperature
342
+ if (!this.device.iosensor?.hide_temperature) {
343
+ if (this.TemperatureSensor!.CurrentTemperature === undefined) {
344
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
471
345
  } else {
346
+ this.accessory.context.CurrentTemperature = this.TemperatureSensor!.CurrentTemperature;
347
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor!.CurrentTemperature);
348
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
349
+ + ` CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
472
350
  if (this.device.mqttURL) {
473
- mqttmessage.push(`"temperature": ${this.CurrentTemperature}`);
351
+ mqttmessage.push(`"temperature": ${this.TemperatureSensor!.CurrentTemperature}`);
474
352
  }
475
353
  if (this.device.history) {
476
- entry['temp'] = this.CurrentTemperature;
354
+ entry['temp'] = this.TemperatureSensor!.CurrentTemperature;
477
355
  }
478
- this.accessory.context.CurrentTemperature = this.CurrentTemperature;
479
- this.temperatureservice?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
480
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
481
356
  }
482
357
  }
483
- if (this.BatteryLevel === undefined) {
484
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
358
+ if (this.Battery.BatteryLevel === undefined) {
359
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
485
360
  } else {
361
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
362
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
363
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
486
364
  if (this.device.mqttURL) {
487
- mqttmessage.push(`"battery": ${this.BatteryLevel}`);
365
+ mqttmessage.push(`"battery": ${this.Battery.BatteryLevel}`);
488
366
  }
489
- this.accessory.context.BatteryLevel = this.BatteryLevel;
490
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
491
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
492
367
  }
493
- if (this.StatusLowBattery === undefined) {
494
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
368
+ if (this.Battery.StatusLowBattery === undefined) {
369
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
495
370
  } else {
371
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
372
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
373
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
374
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
496
375
  if (this.device.mqttURL) {
497
- mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
376
+ mqttmessage.push(`"lowBattery": ${this.Battery.StatusLowBattery}`);
498
377
  }
499
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
500
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
501
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
502
378
  }
379
+
380
+ // MQTT Publish
503
381
  if (this.device.mqttURL) {
504
382
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
505
383
  }
506
- if (Number(this.CurrentRelativeHumidity) > 0) {
384
+
385
+ // History Service
386
+ if (!this.device.iosensor?.hide_humidity && (Number(this.HumiditySensor!.CurrentRelativeHumidity) > 0)) {
507
387
  // reject unreliable data
508
388
  if (this.device.history) {
509
389
  this.historyService?.addEntry(entry);
@@ -511,84 +391,6 @@ export class IOSensor {
511
391
  }
512
392
  }
513
393
 
514
- /*
515
- * Publish MQTT message for topics of
516
- * 'homebridge-switchbot/meter/xx:xx:xx:xx:xx:xx'
517
- */
518
- mqttPublish(message: any) {
519
- const mac = this.device.deviceId
520
- ?.toLowerCase()
521
- .match(/[\s\S]{1,2}/g)
522
- ?.join(':');
523
- const options = this.device.mqttPubOptions || {};
524
- this.mqttClient?.publish(`homebridge-switchbot/meter/${mac}`, `${message}`, options);
525
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${message} options:${JSON.stringify(options)}`);
526
- }
527
-
528
- /*
529
- * Setup MQTT hadler if URL is specified.
530
- */
531
- async setupMqtt(device: device & devicesConfig): Promise<void> {
532
- if (device.mqttURL) {
533
- try {
534
- const { connectAsync } = asyncmqtt;
535
- this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
536
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
537
- this.mqttClient.on('error', (e: Error) => {
538
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
539
- });
540
- } catch (e) {
541
- this.mqttClient = null;
542
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
543
- }
544
- }
545
- }
546
-
547
- /*
548
- * Setup EVE history graph feature if enabled.
549
- */
550
- async setupHistoryService(device: device & devicesConfig): Promise<void> {
551
- const mac = this.device
552
- .deviceId!.match(/.{1,2}/g)!
553
- .join(':')
554
- .toLowerCase();
555
- this.historyService = device.history
556
- ? new this.platform.fakegatoAPI('room', this.accessory, {
557
- log: this.platform.log,
558
- storage: 'fs',
559
- filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
560
- })
561
- : null;
562
- }
563
-
564
- async stopScanning(switchbot: any) {
565
- switchbot.stopScan();
566
- if (this.BLE_IsConnected) {
567
- await this.BLEparseStatus();
568
- await this.updateHomeKitCharacteristics();
569
- } else {
570
- await this.BLERefreshConnection(switchbot);
571
- }
572
- }
573
-
574
- async getCustomBLEAddress(switchbot: any) {
575
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
576
- (async () => {
577
- // Start to monitor advertisement packets
578
- await switchbot.startScan({
579
- model: 'i',
580
- });
581
- // Set an event handler
582
- switchbot.onadvertisement = (ad: any) => {
583
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
584
- };
585
- await sleep(10000);
586
- // Stop to monitor
587
- switchbot.stopScan();
588
- })();
589
- }
590
- }
591
-
592
394
  async BLERefreshConnection(switchbot: any): Promise<void> {
593
395
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
594
396
  + ` ${JSON.stringify(switchbot)}`);
@@ -598,272 +400,26 @@ export class IOSensor {
598
400
  }
599
401
  }
600
402
 
601
- async scan(device: device & devicesConfig): Promise<void> {
602
- if (device.scanDuration) {
603
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
604
- if (this.BLE) {
605
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
606
- }
607
- } else {
608
- this.scanDuration = this.accessory.context.scanDuration = 1;
609
- if (this.BLE) {
610
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
611
- }
612
- }
613
- }
614
-
615
- async statusCode(statusCode: number): Promise<void> {
616
- switch (statusCode) {
617
- case 151:
618
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
619
- break;
620
- case 152:
621
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
622
- break;
623
- case 160:
624
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
625
- break;
626
- case 161:
627
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
628
- this.offlineOff();
629
- break;
630
- case 171:
631
- this.errorLog(
632
- `${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
633
- `Hub: ${this.device.hubDeviceId}`,
634
- );
635
- this.offlineOff();
636
- break;
637
- case 190:
638
- this.errorLog(
639
- `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
640
- ` Or command format is invalid, statusCode: ${statusCode}`,
641
- );
642
- break;
643
- case 100:
644
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
645
- break;
646
- case 200:
647
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
648
- break;
649
- case 400:
650
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
651
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
652
- break;
653
- case 401:
654
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
655
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
656
- break;
657
- case 403:
658
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
659
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
660
- break;
661
- case 404:
662
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
663
- + `statusCode: ${statusCode}`);
664
- break;
665
- case 406:
666
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
667
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
668
- break;
669
- case 415:
670
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
671
- + `header that is not supported by the server, statusCode: ${statusCode}`);
672
- break;
673
- case 422:
674
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
675
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
676
- break;
677
- case 429:
678
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
679
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
680
- break;
681
- case 500:
682
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
683
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
684
- break;
685
- default:
686
- this.infoLog(
687
- `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
688
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
689
- );
690
- }
691
- }
692
-
693
403
  async offlineOff(): Promise<void> {
694
404
  if (this.device.offline) {
695
- await this.deviceContext();
696
- await this.updateHomeKitCharacteristics();
697
- }
698
- }
699
-
700
- async apiError(e: any): Promise<void> {
701
- if (!this.device.meter?.hide_humidity) {
702
- this.humidityservice?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
703
- }
704
- if (!this.device.meter?.hide_temperature) {
705
- this.temperatureservice?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
706
- }
707
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
708
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
709
- }
710
-
711
- async deviceContext() {
712
- if (this.CurrentRelativeHumidity === undefined) {
713
- this.CurrentRelativeHumidity = 0;
714
- } else {
715
- this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
716
- }
717
- if (this.CurrentTemperature === undefined) {
718
- this.CurrentTemperature = 0;
719
- } else {
720
- this.CurrentTemperature = this.accessory.context.CurrentTemperature;
721
- }
722
- if (this.BatteryLevel === undefined) {
723
- this.BatteryLevel = 100;
724
- } else {
725
- this.BatteryLevel = this.accessory.context.BatteryLevel;
726
- }
727
- if (this.StatusLowBattery === undefined) {
728
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
729
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
730
- } else {
731
- this.StatusLowBattery = this.accessory.context.StatusLowBattery;
732
- }
733
- if (this.FirmwareRevision === undefined) {
734
- this.FirmwareRevision = this.platform.version;
735
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
736
- }
737
- }
738
-
739
- async refreshRate(device: device & devicesConfig): Promise<void> {
740
- if (device.refreshRate) {
741
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
742
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
743
- } else if (this.platform.config.options!.refreshRate) {
744
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
745
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
746
- }
747
- }
748
-
749
- async deviceRetry(device: device & devicesConfig): Promise<void> {
750
- if (!device.maxRetries) {
751
- this.maxRetries = 5; // Maximum number of retries
752
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.maxRetries}`);
753
- } else {
754
- this.maxRetries = device.maxRetries;
755
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.maxRetries}`);
756
- }
757
- if (!device.delayBetweenRetries) {
758
- this.delayBetweenRetries = 3000; // Delay between retries in milliseconds
759
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set, Using: ${this.delayBetweenRetries}`);
760
- } else {
761
- this.delayBetweenRetries = device.delayBetweenRetries * 1000;
762
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.delayBetweenRetries}`);
763
- }
764
- }
765
-
766
- async deviceConfig(device: device & devicesConfig): Promise<void> {
767
- let config = {};
768
- if (device.meter) {
769
- config = device.meter;
770
- }
771
- if (device.connectionType !== undefined) {
772
- config['connectionType'] = device.connectionType;
773
- }
774
- if (device.external !== undefined) {
775
- config['external'] = device.external;
776
- }
777
- if (device.mqttURL !== undefined) {
778
- config['mqttURL'] = device.mqttURL;
779
- }
780
- if (device.logging !== undefined) {
781
- config['logging'] = device.logging;
782
- }
783
- if (device.refreshRate !== undefined) {
784
- config['refreshRate'] = device.refreshRate;
785
- }
786
- if (device.scanDuration !== undefined) {
787
- config['scanDuration'] = device.scanDuration;
788
- }
789
- if (device.webhook !== undefined) {
790
- config['webhook'] = device.webhook;
791
- }
792
- if (Object.entries(config).length !== 0) {
793
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
794
- }
795
- }
796
-
797
- async deviceLogs(device: device & devicesConfig): Promise<void> {
798
- if (this.platform.debugMode) {
799
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
800
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
801
- } else if (device.logging) {
802
- this.deviceLogging = this.accessory.context.logging = device.logging;
803
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
804
- } else if (this.platform.config.options?.logging) {
805
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
806
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
807
- } else {
808
- this.deviceLogging = this.accessory.context.logging = 'standard';
809
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
810
- }
811
- }
812
-
813
- /**
814
- * Logging for Device
815
- */
816
- successLog(...log: any[]): void {
817
- if (this.enablingDeviceLogging()) {
818
- this.platform.log.success(String(...log));
819
- }
820
- }
821
-
822
- infoLog(...log: any[]): void {
823
- if (this.enablingDeviceLogging()) {
824
- this.platform.log.info(String(...log));
825
- }
826
- }
827
-
828
- warnLog(...log: any[]): void {
829
- if (this.enablingDeviceLogging()) {
830
- this.platform.log.warn(String(...log));
831
- }
832
- }
833
-
834
- debugWarnLog(...log: any[]): void {
835
- if (this.enablingDeviceLogging()) {
836
- if (this.deviceLogging?.includes('debug')) {
837
- this.platform.log.warn('[DEBUG]', String(...log));
405
+ if (!this.device.iosensor?.hide_humidity) {
406
+ this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, 50);
838
407
  }
839
- }
840
- }
841
-
842
- errorLog(...log: any[]): void {
843
- if (this.enablingDeviceLogging()) {
844
- this.platform.log.error(String(...log));
845
- }
846
- }
847
-
848
- debugErrorLog(...log: any[]): void {
849
- if (this.enablingDeviceLogging()) {
850
- if (this.deviceLogging?.includes('debug')) {
851
- this.platform.log.error('[DEBUG]', String(...log));
408
+ if (!this.device.iosensor?.hide_temperature) {
409
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, 30);
852
410
  }
411
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, 100);
853
412
  }
854
413
  }
855
414
 
856
- debugLog(...log: any[]): void {
857
- if (this.enablingDeviceLogging()) {
858
- if (this.deviceLogging === 'debug') {
859
- this.platform.log.info('[DEBUG]', String(...log));
860
- } else {
861
- this.platform.log.debug(String(...log));
862
- }
415
+ async apiError(e: any): Promise<void> {
416
+ if (!this.device.iosensor?.hide_humidity) {
417
+ this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
863
418
  }
864
- }
865
-
866
- enablingDeviceLogging(): boolean {
867
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
419
+ if (!this.device.iosensor?.hide_temperature) {
420
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
421
+ }
422
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
423
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
868
424
  }
869
425
  }