@switchbot/homebridge-switchbot 2.14.0 → 3.0.0-beta.0

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 (154) hide show
  1. package/.github/npm-version-script.cjs +81 -0
  2. package/.github/workflows/dependabot.yml +10 -11
  3. package/CHANGELOG.md +11 -2
  4. package/LICENSE +1 -1
  5. package/config.schema.json +1 -0
  6. package/dist/device/blindtilt.d.ts +9 -5
  7. package/dist/device/blindtilt.d.ts.map +1 -1
  8. package/dist/device/blindtilt.js +151 -91
  9. package/dist/device/blindtilt.js.map +1 -1
  10. package/dist/device/bot.d.ts +9 -5
  11. package/dist/device/bot.d.ts.map +1 -1
  12. package/dist/device/bot.js +234 -184
  13. package/dist/device/bot.js.map +1 -1
  14. package/dist/device/ceilinglight.d.ts +9 -5
  15. package/dist/device/ceilinglight.d.ts.map +1 -1
  16. package/dist/device/ceilinglight.js +110 -64
  17. package/dist/device/ceilinglight.js.map +1 -1
  18. package/dist/device/colorbulb.d.ts +9 -5
  19. package/dist/device/colorbulb.d.ts.map +1 -1
  20. package/dist/device/colorbulb.js +121 -69
  21. package/dist/device/colorbulb.js.map +1 -1
  22. package/dist/device/contact.d.ts +9 -5
  23. package/dist/device/contact.d.ts.map +1 -1
  24. package/dist/device/contact.js +111 -66
  25. package/dist/device/contact.js.map +1 -1
  26. package/dist/device/curtain.d.ts +9 -5
  27. package/dist/device/curtain.d.ts.map +1 -1
  28. package/dist/device/curtain.js +164 -107
  29. package/dist/device/curtain.js.map +1 -1
  30. package/dist/device/hub.d.ts +9 -5
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +87 -51
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +9 -5
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +133 -90
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +9 -5
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +108 -66
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +9 -5
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +110 -65
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +10 -6
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +125 -84
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +9 -5
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +102 -63
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +9 -5
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +102 -63
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +9 -5
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +95 -54
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -5
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +68 -40
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +9 -5
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +86 -52
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/homebridge-ui/server.d.ts +2 -0
  71. package/dist/homebridge-ui/server.d.ts.map +1 -0
  72. package/dist/homebridge-ui/server.js +44 -0
  73. package/dist/homebridge-ui/server.js.map +1 -0
  74. package/dist/index.d.ts +1 -4
  75. package/dist/index.d.ts.map +1 -1
  76. package/dist/index.js +9 -5
  77. package/dist/index.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +8 -4
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js +105 -71
  81. package/dist/irdevice/airconditioner.js.map +1 -1
  82. package/dist/irdevice/airpurifier.d.ts +8 -4
  83. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  84. package/dist/irdevice/airpurifier.js +78 -46
  85. package/dist/irdevice/airpurifier.js.map +1 -1
  86. package/dist/irdevice/camera.d.ts +8 -4
  87. package/dist/irdevice/camera.d.ts.map +1 -1
  88. package/dist/irdevice/camera.js +41 -25
  89. package/dist/irdevice/camera.js.map +1 -1
  90. package/dist/irdevice/fan.d.ts +8 -4
  91. package/dist/irdevice/fan.d.ts.map +1 -1
  92. package/dist/irdevice/fan.js +62 -39
  93. package/dist/irdevice/fan.js.map +1 -1
  94. package/dist/irdevice/light.d.ts +8 -4
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +67 -45
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +8 -4
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +176 -150
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +8 -4
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +81 -62
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +8 -4
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +41 -25
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +8 -4
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +48 -32
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/platform.d.ts +8 -9
  115. package/dist/platform.d.ts.map +1 -1
  116. package/dist/platform.js +147 -162
  117. package/dist/platform.js.map +1 -1
  118. package/dist/settings.js +7 -14
  119. package/dist/settings.js.map +1 -1
  120. package/dist/utils.js +1 -5
  121. package/dist/utils.js.map +1 -1
  122. package/package.json +28 -16
  123. package/src/device/blindtilt.ts +80 -69
  124. package/src/device/bot.ts +189 -178
  125. package/src/device/ceilinglight.ts +55 -42
  126. package/src/device/colorbulb.ts +51 -38
  127. package/src/device/contact.ts +69 -58
  128. package/src/device/curtain.ts +94 -83
  129. package/src/device/hub.ts +54 -43
  130. package/src/device/humidifier.ts +86 -75
  131. package/src/device/iosensor.ts +63 -52
  132. package/src/device/lightstrip.ts +54 -41
  133. package/src/device/lock.ts +89 -79
  134. package/src/device/meter.ts +59 -48
  135. package/src/device/meterplus.ts +63 -52
  136. package/src/device/motion.ts +56 -45
  137. package/src/device/plug.ts +36 -25
  138. package/src/device/robotvacuumcleaner.ts +47 -36
  139. package/src/homebridge-ui/public/index.html +240 -0
  140. package/src/homebridge-ui/server.ts +48 -0
  141. package/src/index.ts +10 -7
  142. package/src/irdevice/airconditioner.ts +70 -62
  143. package/src/irdevice/airpurifier.ts +47 -39
  144. package/src/irdevice/camera.ts +27 -19
  145. package/src/irdevice/fan.ts +41 -33
  146. package/src/irdevice/light.ts +47 -39
  147. package/src/irdevice/other.ts +163 -155
  148. package/src/irdevice/tv.ts +65 -57
  149. package/src/irdevice/vacuumcleaner.ts +27 -19
  150. package/src/irdevice/waterheater.ts +34 -26
  151. package/src/platform.ts +35 -21
  152. package/tsconfig.json +11 -13
  153. package/homebridge-ui/server.js +0 -50
  154. /package/{homebridge-ui → dist/homebridge-ui}/public/index.html +0 -0
@@ -1,7 +1,7 @@
1
- import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
1
+ import { CharacteristicValue, PlatformAccessory, Service, API, Logging, HAP } from 'homebridge';
2
2
  import { request } from 'undici';
3
3
  import { SwitchBotPlatform } from '../platform';
4
- import { Devices, irDevicesConfig, irdevice } from '../settings';
4
+ import { Devices, irDevicesConfig, irdevice, SwitchBotPlatformConfig } from '../settings';
5
5
 
6
6
  /**
7
7
  * Platform Accessory
@@ -9,6 +9,10 @@ import { Devices, irDevicesConfig, irdevice } from '../settings';
9
9
  * Each accessory may expose multiple services of different service types.
10
10
  */
11
11
  export class AirConditioner {
12
+ public readonly api: API;
13
+ public readonly log: Logging;
14
+ public readonly config!: SwitchBotPlatformConfig;
15
+ protected readonly hap: HAP;
12
16
  // Services
13
17
  coolerService!: Service;
14
18
 
@@ -49,37 +53,41 @@ export class AirConditioner {
49
53
  private accessory: PlatformAccessory,
50
54
  public device: irdevice & irDevicesConfig,
51
55
  ) {
56
+ this.api = this.platform.api;
57
+ this.log = this.platform.log;
58
+ this.config = this.platform.config;
59
+ this.hap = this.api.hap;
52
60
  // default placeholders
53
- this.logs(device);
61
+ this.deviceLogs(device);
54
62
  this.context();
55
63
  this.disablePushOnChanges(device);
56
64
  this.disablePushOffChanges(device);
57
65
  this.disablePushDetailChanges(device);
58
- this.config(device);
66
+ this.deviceConfig(device);
59
67
 
60
68
  // set accessory information
61
69
  accessory
62
- .getService(this.platform.Service.AccessoryInformation)!
63
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
64
- .setCharacteristic(this.platform.Characteristic.Model, device.remoteType)
65
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
66
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
70
+ .getService(this.hap.Service.AccessoryInformation)!
71
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
72
+ .setCharacteristic(this.hap.Characteristic.Model, device.remoteType)
73
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
74
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
67
75
 
68
76
  // get the Television service if it exists, otherwise create a new Television service
69
77
  // you can create multiple services for each accessory
70
78
  const coolerService = `${accessory.displayName} ${device.remoteType}`;
71
- (this.coolerService = accessory.getService(this.platform.Service.HeaterCooler)
72
- || accessory.addService(this.platform.Service.HeaterCooler)), coolerService;
79
+ (this.coolerService = accessory.getService(this.hap.Service.HeaterCooler)
80
+ || accessory.addService(this.hap.Service.HeaterCooler)), coolerService;
73
81
 
74
- this.coolerService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
75
- if (!this.coolerService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
76
- this.coolerService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
82
+ this.coolerService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
83
+ if (!this.coolerService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
84
+ this.coolerService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
77
85
  }
78
86
 
79
87
  // handle on / off events using the Active characteristic
80
- this.coolerService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.ActiveSet.bind(this));
88
+ this.coolerService.getCharacteristic(this.hap.Characteristic.Active).onSet(this.ActiveSet.bind(this));
81
89
 
82
- this.coolerService.getCharacteristic(this.platform.Characteristic.CurrentTemperature).onGet(this.CurrentTemperatureGet.bind(this));
90
+ this.coolerService.getCharacteristic(this.hap.Characteristic.CurrentTemperature).onGet(this.CurrentTemperatureGet.bind(this));
83
91
 
84
92
  this.ValidValues = this.hide_automode ? [1, 2] : [0, 1, 2];
85
93
 
@@ -89,21 +97,21 @@ export class AirConditioner {
89
97
  }
90
98
 
91
99
  if (this.meter) {
92
- this.coolerService.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity).onGet(this.CurrentRelativeHumidityGet.bind(this));
100
+ this.coolerService.getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity).onGet(this.CurrentRelativeHumidityGet.bind(this));
93
101
  }
94
102
 
95
103
  this.coolerService
96
- .getCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState)
104
+ .getCharacteristic(this.hap.Characteristic.TargetHeaterCoolerState)
97
105
  .setProps({
98
106
  validValues: this.ValidValues,
99
107
  })
100
108
  .onGet(this.TargetHeaterCoolerStateGet.bind(this))
101
109
  .onSet(this.TargetHeaterCoolerStateSet.bind(this));
102
110
 
103
- this.coolerService.getCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState).onGet(this.CurrentHeaterCoolerStateGet.bind(this));
111
+ this.coolerService.getCharacteristic(this.hap.Characteristic.CurrentHeaterCoolerState).onGet(this.CurrentHeaterCoolerStateGet.bind(this));
104
112
 
105
113
  this.coolerService
106
- .getCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature)
114
+ .getCharacteristic(this.hap.Characteristic.HeatingThresholdTemperature)
107
115
  .setProps({
108
116
  minValue: 0,
109
117
  maxValue: 35,
@@ -113,7 +121,7 @@ export class AirConditioner {
113
121
  .onSet(this.ThresholdTemperatureSet.bind(this));
114
122
 
115
123
  this.coolerService
116
- .getCharacteristic(this.platform.Characteristic.CoolingThresholdTemperature)
124
+ .getCharacteristic(this.hap.Characteristic.CoolingThresholdTemperature)
117
125
  .setProps({
118
126
  minValue: 0,
119
127
  maxValue: 35,
@@ -123,7 +131,7 @@ export class AirConditioner {
123
131
  .onSet(this.ThresholdTemperatureSet.bind(this));
124
132
 
125
133
  this.coolerService
126
- .getCharacteristic(this.platform.Characteristic.RotationSpeed)
134
+ .getCharacteristic(this.hap.Characteristic.RotationSpeed)
127
135
  .setProps({
128
136
  format: 'int',
129
137
  minStep: 1,
@@ -148,7 +156,7 @@ export class AirConditioner {
148
156
  `${this.device.remoteType}: ${this.accessory.displayName} pushAirConditionerOnChanges Active: ${this.Active},` +
149
157
  ` disablePushOn: ${this.disablePushOn}`,
150
158
  );
151
- if (this.Active === this.platform.Characteristic.Active.ACTIVE && !this.disablePushOn) {
159
+ if (this.Active === this.hap.Characteristic.Active.ACTIVE && !this.disablePushOn) {
152
160
  const commandType: string = await this.commandType();
153
161
  const command: string = await this.commandOn();
154
162
  const bodyChange = JSON.stringify({
@@ -165,7 +173,7 @@ export class AirConditioner {
165
173
  `${this.device.remoteType}: ${this.accessory.displayName} pushAirConditionerOffChanges Active: ${this.Active},` +
166
174
  ` disablePushOff: ${this.disablePushOff}`,
167
175
  );
168
- if (this.Active === this.platform.Characteristic.Active.INACTIVE && !this.disablePushOff) {
176
+ if (this.Active === this.hap.Characteristic.Active.INACTIVE && !this.disablePushOff) {
169
177
  const commandType: string = await this.commandType();
170
178
  const command: string = await this.commandOff();
171
179
  const bodyChange = JSON.stringify({
@@ -184,7 +192,7 @@ export class AirConditioner {
184
192
  );
185
193
  if (!this.Busy) {
186
194
  this.Busy = true;
187
- this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.IDLE;
195
+ this.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.IDLE;
188
196
  }
189
197
  clearTimeout(this.Timeout);
190
198
 
@@ -204,7 +212,7 @@ export class AirConditioner {
204
212
  if (this.CurrentFanSpeed === undefined) {
205
213
  this.CurrentFanSpeed = 1;
206
214
  }
207
- if (this.Active === this.platform.Characteristic.Active.ACTIVE) {
215
+ if (this.Active === this.hap.Characteristic.Active.ACTIVE) {
208
216
  this.state = 'on';
209
217
  } else {
210
218
  this.state = 'off';
@@ -230,19 +238,19 @@ export class AirConditioner {
230
238
  }
231
239
 
232
240
  private async UpdateCurrentHeaterCoolerState() {
233
- if (this.Active === this.platform.Characteristic.Active.ACTIVE) {
241
+ if (this.Active === this.hap.Characteristic.Active.ACTIVE) {
234
242
  await this.context();
235
243
  if (this.ThresholdTemperature < this.CurrentTemperature &&
236
- this.TargetHeaterCoolerState !== this.platform.Characteristic.TargetHeaterCoolerState.HEAT) {
237
- this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.COOLING;
244
+ this.TargetHeaterCoolerState !== this.hap.Characteristic.TargetHeaterCoolerState.HEAT) {
245
+ this.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.COOLING;
238
246
  } else if (this.ThresholdTemperature > this.CurrentTemperature &&
239
- this.TargetHeaterCoolerState !== this.platform.Characteristic.TargetHeaterCoolerState.COOL) {
240
- this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.HEATING;
247
+ this.TargetHeaterCoolerState !== this.hap.Characteristic.TargetHeaterCoolerState.COOL) {
248
+ this.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.HEATING;
241
249
  } else {
242
- this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.IDLE;
250
+ this.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.IDLE;
243
251
  }
244
252
  } else {
245
- this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.INACTIVE;
253
+ this.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.INACTIVE;
246
254
  }
247
255
  }
248
256
 
@@ -345,7 +353,7 @@ export class AirConditioner {
345
353
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Active: ${value}`);
346
354
 
347
355
  this.Active = value;
348
- if (this.Active === this.platform.Characteristic.Active.ACTIVE) {
356
+ if (this.Active === this.hap.Characteristic.Active.ACTIVE) {
349
357
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} pushAirConditionerOnChanges, Active: ${this.Active}`);
350
358
  if (this.disablePushOn) {
351
359
  this.pushAirConditionerStatusChanges();
@@ -369,11 +377,11 @@ export class AirConditioner {
369
377
  }
370
378
 
371
379
  async TargetHeaterCoolerStateSet(value: CharacteristicValue): Promise<void> {
372
- if (!this.hide_automode && value === this.platform.Characteristic.TargetHeaterCoolerState.AUTO) {
380
+ if (!this.hide_automode && value === this.hap.Characteristic.TargetHeaterCoolerState.AUTO) {
373
381
  this.TargetHeaterCoolerStateAUTO();
374
- } else if (value === this.platform.Characteristic.TargetHeaterCoolerState.HEAT) {
382
+ } else if (value === this.hap.Characteristic.TargetHeaterCoolerState.HEAT) {
375
383
  this.TargetHeaterCoolerStateHEAT();
376
- } else if (value === this.platform.Characteristic.TargetHeaterCoolerState.COOL) {
384
+ } else if (value === this.hap.Characteristic.TargetHeaterCoolerState.COOL) {
377
385
  this.TargetHeaterCoolerStateCOOL();
378
386
  } else {
379
387
  this.errorLog(
@@ -385,21 +393,21 @@ export class AirConditioner {
385
393
  }
386
394
 
387
395
  async TargetHeaterCoolerStateAUTO(): Promise<void> {
388
- this.TargetHeaterCoolerState = this.platform.Characteristic.TargetHeaterCoolerState.AUTO;
396
+ this.TargetHeaterCoolerState = this.hap.Characteristic.TargetHeaterCoolerState.AUTO;
389
397
  this.CurrentMode = 1;
390
398
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set (AUTO) TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
391
399
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Switchbot CurrentMode: ${this.CurrentMode}`);
392
400
  }
393
401
 
394
402
  async TargetHeaterCoolerStateCOOL(): Promise<void> {
395
- this.TargetHeaterCoolerState = this.platform.Characteristic.TargetHeaterCoolerState.COOL;
403
+ this.TargetHeaterCoolerState = this.hap.Characteristic.TargetHeaterCoolerState.COOL;
396
404
  this.CurrentMode = 2;
397
405
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set (COOL) TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
398
406
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Switchbot CurrentMode: ${this.CurrentMode}`);
399
407
  }
400
408
 
401
409
  async TargetHeaterCoolerStateHEAT(): Promise<void> {
402
- this.TargetHeaterCoolerState = this.platform.Characteristic.TargetHeaterCoolerState.HEAT;
410
+ this.TargetHeaterCoolerState = this.hap.Characteristic.TargetHeaterCoolerState.HEAT;
403
411
  this.CurrentMode = 5;
404
412
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set (HEAT) TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
405
413
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Switchbot CurrentMode: ${this.CurrentMode}`);
@@ -418,11 +426,11 @@ export class AirConditioner {
418
426
 
419
427
  private getTargetHeaterCoolerStateName(): string {
420
428
  switch (this.TargetHeaterCoolerState) {
421
- case this.platform.Characteristic.TargetHeaterCoolerState.AUTO:
429
+ case this.hap.Characteristic.TargetHeaterCoolerState.AUTO:
422
430
  return 'AUTO';
423
- case this.platform.Characteristic.TargetHeaterCoolerState.HEAT:
431
+ case this.hap.Characteristic.TargetHeaterCoolerState.HEAT:
424
432
  return 'HEAT';
425
- case this.platform.Characteristic.TargetHeaterCoolerState.COOL:
433
+ case this.hap.Characteristic.TargetHeaterCoolerState.COOL:
426
434
  return 'COOL';
427
435
  default:
428
436
  return this.TargetHeaterCoolerState.toString();
@@ -450,7 +458,7 @@ export class AirConditioner {
450
458
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${this.Active}`);
451
459
  } else {
452
460
  this.accessory.context.Active = this.Active;
453
- this.coolerService?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
461
+ this.coolerService?.updateCharacteristic(this.hap.Characteristic.Active, this.Active);
454
462
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
455
463
  }
456
464
  // RotationSpeed
@@ -458,7 +466,7 @@ export class AirConditioner {
458
466
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} RotationSpeed: ${this.RotationSpeed}`);
459
467
  } else {
460
468
  this.accessory.context.RotationSpeed = this.RotationSpeed;
461
- this.coolerService?.updateCharacteristic(this.platform.Characteristic.RotationSpeed, this.RotationSpeed);
469
+ this.coolerService?.updateCharacteristic(this.hap.Characteristic.RotationSpeed, this.RotationSpeed);
462
470
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.RotationSpeed}`);
463
471
  }
464
472
  // CurrentTemperature
@@ -466,7 +474,7 @@ export class AirConditioner {
466
474
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
467
475
  } else {
468
476
  this.accessory.context.CurrentTemperature = this.CurrentTemperature;
469
- this.coolerService?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
477
+ this.coolerService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
470
478
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
471
479
  }
472
480
  // CurrentRelativeHumidity
@@ -475,7 +483,7 @@ export class AirConditioner {
475
483
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
476
484
  } else {
477
485
  this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
478
- this.coolerService?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
486
+ this.coolerService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
479
487
  this.debugLog(
480
488
  `${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
481
489
  );
@@ -486,7 +494,7 @@ export class AirConditioner {
486
494
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
487
495
  } else {
488
496
  this.accessory.context.TargetHeaterCoolerState = this.TargetHeaterCoolerState;
489
- this.coolerService?.updateCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState, this.TargetHeaterCoolerState);
497
+ this.coolerService?.updateCharacteristic(this.hap.Characteristic.TargetHeaterCoolerState, this.TargetHeaterCoolerState);
490
498
  this.debugLog(
491
499
  `${this.device.remoteType}: ${this.accessory.displayName}` + ` updateCharacteristic TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`,
492
500
  );
@@ -496,7 +504,7 @@ export class AirConditioner {
496
504
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
497
505
  } else {
498
506
  this.accessory.context.CurrentHeaterCoolerState = this.CurrentHeaterCoolerState;
499
- this.coolerService?.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, this.CurrentHeaterCoolerState);
507
+ this.coolerService?.updateCharacteristic(this.hap.Characteristic.CurrentHeaterCoolerState, this.CurrentHeaterCoolerState);
500
508
  this.debugLog(
501
509
  `${this.device.remoteType}: ${this.accessory.displayName}` +
502
510
  ` updateCharacteristic CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`,
@@ -507,8 +515,8 @@ export class AirConditioner {
507
515
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} ThresholdTemperature: ${this.ThresholdTemperature}`);
508
516
  } else {
509
517
  this.accessory.context.ThresholdTemperature = this.ThresholdTemperature;
510
- this.coolerService?.updateCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature, this.ThresholdTemperature);
511
- this.coolerService?.updateCharacteristic(this.platform.Characteristic.CoolingThresholdTemperature, this.ThresholdTemperature);
518
+ this.coolerService?.updateCharacteristic(this.hap.Characteristic.HeatingThresholdTemperature, this.ThresholdTemperature);
519
+ this.coolerService?.updateCharacteristic(this.hap.Characteristic.CoolingThresholdTemperature, this.ThresholdTemperature);
512
520
  this.debugLog(
513
521
  `${this.device.remoteType}: ${this.accessory.displayName}` + ` updateCharacteristic ThresholdTemperature: ${this.ThresholdTemperature}`,
514
522
  );
@@ -610,19 +618,19 @@ export class AirConditioner {
610
618
  }
611
619
 
612
620
  async apiError({ e }: { e: any }): Promise<void> {
613
- this.coolerService.updateCharacteristic(this.platform.Characteristic.Active, e);
614
- this.coolerService.updateCharacteristic(this.platform.Characteristic.RotationSpeed, e);
615
- this.coolerService.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
616
- this.coolerService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
617
- this.coolerService.updateCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState, e);
618
- this.coolerService.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, e);
619
- this.coolerService.updateCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature, e);
620
- this.coolerService.updateCharacteristic(this.platform.Characteristic.CoolingThresholdTemperature, e);
621
+ this.coolerService.updateCharacteristic(this.hap.Characteristic.Active, e);
622
+ this.coolerService.updateCharacteristic(this.hap.Characteristic.RotationSpeed, e);
623
+ this.coolerService.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
624
+ this.coolerService.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
625
+ this.coolerService.updateCharacteristic(this.hap.Characteristic.TargetHeaterCoolerState, e);
626
+ this.coolerService.updateCharacteristic(this.hap.Characteristic.CurrentHeaterCoolerState, e);
627
+ this.coolerService.updateCharacteristic(this.hap.Characteristic.HeatingThresholdTemperature, e);
628
+ this.coolerService.updateCharacteristic(this.hap.Characteristic.CoolingThresholdTemperature, e);
621
629
  }
622
630
 
623
631
  async context(): Promise<void> {
624
632
  if (this.Active === undefined) {
625
- this.Active = this.platform.Characteristic.Active.INACTIVE;
633
+ this.Active = this.hap.Characteristic.Active.INACTIVE;
626
634
  } else if (this.Active) {
627
635
  this.Active;
628
636
  } else {
@@ -668,7 +676,7 @@ export class AirConditioner {
668
676
  }
669
677
  }
670
678
 
671
- async config(device: irdevice & irDevicesConfig): Promise<void> {
679
+ async deviceConfig(device: irdevice & irDevicesConfig): Promise<void> {
672
680
  let config = {};
673
681
  if (device.irair) {
674
682
  config = device.irair;
@@ -705,7 +713,7 @@ export class AirConditioner {
705
713
  }
706
714
  }
707
715
 
708
- async logs(device: irdevice & irDevicesConfig): Promise<void> {
716
+ async deviceLogs(device: irdevice & irDevicesConfig): Promise<void> {
709
717
  if (this.platform.debugMode) {
710
718
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
711
719
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
@@ -1,7 +1,7 @@
1
- import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
1
+ import { CharacteristicValue, PlatformAccessory, Service, API, Logging, HAP } from 'homebridge';
2
2
  import { request } from 'undici';
3
3
  import { SwitchBotPlatform } from '../platform';
4
- import { Devices, irDevicesConfig, irdevice } from '../settings';
4
+ import { Devices, irDevicesConfig, irdevice, SwitchBotPlatformConfig } from '../settings';
5
5
 
6
6
  /**
7
7
  * Platform Accessory
@@ -9,6 +9,10 @@ import { Devices, irDevicesConfig, irdevice } from '../settings';
9
9
  * Each accessory may expose multiple services of different service types.
10
10
  */
11
11
  export class AirPurifier {
12
+ public readonly api: API;
13
+ public readonly log: Logging;
14
+ public readonly config!: SwitchBotPlatformConfig;
15
+ protected readonly hap: HAP;
12
16
  // Services
13
17
  airPurifierService!: Service;
14
18
 
@@ -44,46 +48,50 @@ export class AirPurifier {
44
48
  private accessory: PlatformAccessory,
45
49
  public device: irdevice & irDevicesConfig,
46
50
  ) {
51
+ this.api = this.platform.api;
52
+ this.log = this.platform.log;
53
+ this.config = this.platform.config;
54
+ this.hap = this.api.hap;
47
55
  // default placeholders
48
- this.logs(device);
56
+ this.deviceLogs(device);
49
57
  this.context();
50
58
  this.disablePushOnChanges(device);
51
59
  this.disablePushOffChanges(device);
52
- this.config(device);
60
+ this.deviceConfig(device);
53
61
 
54
62
  // set accessory information
55
63
  accessory
56
- .getService(this.platform.Service.AccessoryInformation)!
57
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
58
- .setCharacteristic(this.platform.Characteristic.Model, device.remoteType)
59
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
60
- .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
64
+ .getService(this.hap.Service.AccessoryInformation)!
65
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
66
+ .setCharacteristic(this.hap.Characteristic.Model, device.remoteType)
67
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
68
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
61
69
 
62
70
  // get the Television service if it exists, otherwise create a new Television service
63
71
  // you can create multiple services for each accessory
64
72
  const airPurifierService = `${accessory.displayName} Air Purifier`;
65
- (this.airPurifierService = accessory.getService(this.platform.Service.AirPurifier)
66
- || accessory.addService(this.platform.Service.AirPurifier)), airPurifierService;
73
+ (this.airPurifierService = accessory.getService(this.hap.Service.AirPurifier)
74
+ || accessory.addService(this.hap.Service.AirPurifier)), airPurifierService;
67
75
 
68
- this.airPurifierService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
69
- if (!this.airPurifierService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
70
- this.airPurifierService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
76
+ this.airPurifierService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
77
+ if (!this.airPurifierService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
78
+ this.airPurifierService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
71
79
  }
72
80
  // handle on / off events using the Active characteristic
73
- this.airPurifierService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.ActiveSet.bind(this));
81
+ this.airPurifierService.getCharacteristic(this.hap.Characteristic.Active).onSet(this.ActiveSet.bind(this));
74
82
 
75
- this.airPurifierService.getCharacteristic(this.platform.Characteristic.CurrentAirPurifierState).onGet(() => {
83
+ this.airPurifierService.getCharacteristic(this.hap.Characteristic.CurrentAirPurifierState).onGet(() => {
76
84
  return this.CurrentAirPurifierStateGet();
77
85
  });
78
86
 
79
- this.airPurifierService.getCharacteristic(this.platform.Characteristic.TargetAirPurifierState).onSet(this.TargetAirPurifierStateSet.bind(this));
87
+ this.airPurifierService.getCharacteristic(this.hap.Characteristic.TargetAirPurifierState).onSet(this.TargetAirPurifierStateSet.bind(this));
80
88
  }
81
89
 
82
90
  async ActiveSet(value: CharacteristicValue): Promise<void> {
83
91
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Active: ${value}`);
84
92
 
85
93
  this.Active = value;
86
- if (this.Active === this.platform.Characteristic.Active.ACTIVE) {
94
+ if (this.Active === this.hap.Characteristic.Active.ACTIVE) {
87
95
  this.pushAirPurifierOnChanges();
88
96
  } else {
89
97
  this.pushAirPurifierOffChanges();
@@ -92,13 +100,13 @@ export class AirPurifier {
92
100
 
93
101
  async TargetAirPurifierStateSet(value: CharacteristicValue): Promise<void> {
94
102
  switch (value) {
95
- case this.platform.Characteristic.CurrentAirPurifierState.PURIFYING_AIR:
103
+ case this.hap.Characteristic.CurrentAirPurifierState.PURIFYING_AIR:
96
104
  this.CurrentMode = AirPurifier.PURIFYING_AIR;
97
105
  break;
98
- case this.platform.Characteristic.CurrentAirPurifierState.IDLE:
106
+ case this.hap.Characteristic.CurrentAirPurifierState.IDLE:
99
107
  this.CurrentMode = AirPurifier.IDLE;
100
108
  break;
101
- case this.platform.Characteristic.CurrentAirPurifierState.INACTIVE:
109
+ case this.hap.Characteristic.CurrentAirPurifierState.INACTIVE:
102
110
  this.CurrentMode = AirPurifier.INACTIVE;
103
111
  break;
104
112
  default:
@@ -108,9 +116,9 @@ export class AirPurifier {
108
116
 
109
117
  async CurrentAirPurifierStateGet(): Promise<number> {
110
118
  if (this.Active === 1) {
111
- this.CurrentAirPurifierState = this.platform.Characteristic.CurrentAirPurifierState.PURIFYING_AIR;
119
+ this.CurrentAirPurifierState = this.hap.Characteristic.CurrentAirPurifierState.PURIFYING_AIR;
112
120
  } else {
113
- this.CurrentAirPurifierState = this.platform.Characteristic.CurrentAirPurifierState.INACTIVE;
121
+ this.CurrentAirPurifierState = this.hap.Characteristic.CurrentAirPurifierState.INACTIVE;
114
122
  }
115
123
  return this.CurrentAirPurifierState;
116
124
  }
@@ -131,7 +139,7 @@ export class AirPurifier {
131
139
  `${this.device.remoteType}: ${this.accessory.displayName} pushAirPurifierOnChanges Active: ${this.Active},` +
132
140
  ` disablePushOn: ${this.disablePushOn}`,
133
141
  );
134
- if (this.Active === this.platform.Characteristic.Active.ACTIVE && !this.disablePushOn) {
142
+ if (this.Active === this.hap.Characteristic.Active.ACTIVE && !this.disablePushOn) {
135
143
  const commandType: string = await this.commandType();
136
144
  const command: string = await this.commandOn();
137
145
  const bodyChange = JSON.stringify({
@@ -148,7 +156,7 @@ export class AirPurifier {
148
156
  `${this.device.remoteType}: ${this.accessory.displayName} pushAirPurifierOffChanges Active: ${this.Active},` +
149
157
  ` disablePushOff: ${this.disablePushOff}`,
150
158
  );
151
- if (this.Active === this.platform.Characteristic.Active.INACTIVE && !this.disablePushOn) {
159
+ if (this.Active === this.hap.Characteristic.Active.INACTIVE && !this.disablePushOn) {
152
160
  const commandType: string = await this.commandType();
153
161
  const command: string = await this.commandOff();
154
162
  const bodyChange = JSON.stringify({
@@ -167,7 +175,7 @@ export class AirPurifier {
167
175
  );
168
176
  if (!this.Busy) {
169
177
  this.Busy = true;
170
- this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.IDLE;
178
+ this.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.IDLE;
171
179
  }
172
180
  clearTimeout(this.Timeout);
173
181
 
@@ -192,12 +200,12 @@ export class AirPurifier {
192
200
  });
193
201
  if (this.Active === 1) {
194
202
  if ((Number(this.CurrentTemperature) || 24) < (this.LastTemperature || 30)) {
195
- this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.COOLING;
203
+ this.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.COOLING;
196
204
  } else {
197
- this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.HEATING;
205
+ this.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.HEATING;
198
206
  }
199
207
  } else {
200
- this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.INACTIVE;
208
+ this.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.INACTIVE;
201
209
  }
202
210
  await this.pushChanges(bodyChange);
203
211
  }
@@ -248,7 +256,7 @@ export class AirPurifier {
248
256
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${this.Active}`);
249
257
  } else {
250
258
  this.accessory.context.Active = this.Active;
251
- this.airPurifierService?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
259
+ this.airPurifierService?.updateCharacteristic(this.hap.Characteristic.Active, this.Active);
252
260
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
253
261
  }
254
262
  // CurrentAirPurifierState
@@ -256,7 +264,7 @@ export class AirPurifier {
256
264
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} CurrentAirPurifierState: ${this.CurrentAirPurifierState}`);
257
265
  } else {
258
266
  this.accessory.context.CurrentAirPurifierState = this.CurrentAirPurifierState;
259
- this.airPurifierService?.updateCharacteristic(this.platform.Characteristic.CurrentAirPurifierState, this.CurrentAirPurifierState);
267
+ this.airPurifierService?.updateCharacteristic(this.hap.Characteristic.CurrentAirPurifierState, this.CurrentAirPurifierState);
260
268
  this.debugLog(
261
269
  `${this.device.remoteType}: ${this.accessory.displayName}` + ` updateCharacteristic CurrentAirPurifierState: ${this.CurrentAirPurifierState}`,
262
270
  );
@@ -266,7 +274,7 @@ export class AirPurifier {
266
274
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
267
275
  } else {
268
276
  this.accessory.context.CurrentHeaterCoolerState = this.CurrentHeaterCoolerState;
269
- this.airPurifierService?.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, this.CurrentHeaterCoolerState);
277
+ this.airPurifierService?.updateCharacteristic(this.hap.Characteristic.CurrentHeaterCoolerState, this.CurrentHeaterCoolerState);
270
278
  this.debugLog(
271
279
  `${this.device.remoteType}: ${this.accessory.displayName}` +
272
280
  ` updateCharacteristic CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`,
@@ -361,15 +369,15 @@ export class AirPurifier {
361
369
  }
362
370
 
363
371
  async apiError(e: any): Promise<void> {
364
- this.airPurifierService.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, e);
365
- this.airPurifierService.updateCharacteristic(this.platform.Characteristic.CurrentAirPurifierState, e);
366
- this.airPurifierService.updateCharacteristic(this.platform.Characteristic.TargetAirPurifierState, e);
367
- this.airPurifierService.updateCharacteristic(this.platform.Characteristic.Active, e);
372
+ this.airPurifierService.updateCharacteristic(this.hap.Characteristic.CurrentHeaterCoolerState, e);
373
+ this.airPurifierService.updateCharacteristic(this.hap.Characteristic.CurrentAirPurifierState, e);
374
+ this.airPurifierService.updateCharacteristic(this.hap.Characteristic.TargetAirPurifierState, e);
375
+ this.airPurifierService.updateCharacteristic(this.hap.Characteristic.Active, e);
368
376
  }
369
377
 
370
378
  private context() {
371
379
  if (this.Active === undefined) {
372
- this.Active = this.platform.Characteristic.Active.INACTIVE;
380
+ this.Active = this.hap.Characteristic.Active.INACTIVE;
373
381
  } else {
374
382
  this.Active = this.accessory.context.Active;
375
383
  }
@@ -384,7 +392,7 @@ export class AirPurifier {
384
392
  }
385
393
  }
386
394
 
387
- async config(device: irdevice & irDevicesConfig): Promise<void> {
395
+ async deviceConfig(device: irdevice & irDevicesConfig): Promise<void> {
388
396
  let config = {};
389
397
  if (device.irpur) {
390
398
  config = device.irpur;
@@ -418,7 +426,7 @@ export class AirPurifier {
418
426
  }
419
427
  }
420
428
 
421
- async logs(device: irdevice & irDevicesConfig): Promise<void> {
429
+ async deviceLogs(device: irdevice & irDevicesConfig): Promise<void> {
422
430
  if (this.platform.debugMode) {
423
431
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
424
432
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);