@switchbot/homebridge-switchbot 3.5.0-beta.7 → 3.5.0-beta.70

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 (177) hide show
  1. package/.vscode/settings.json +4 -1
  2. package/CHANGELOG.md +4 -1
  3. package/config.schema.json +120 -17
  4. package/dist/device/blindtilt.d.ts +23 -96
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +425 -778
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +26 -72
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +803 -837
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +14 -68
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +241 -606
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +14 -75
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +283 -658
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +15 -66
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +254 -545
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +19 -88
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +412 -806
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/device.d.ts +59 -0
  29. package/dist/device/device.d.ts.map +1 -0
  30. package/dist/device/device.js +711 -0
  31. package/dist/device/device.js.map +1 -0
  32. package/dist/device/fan.d.ts +51 -0
  33. package/dist/device/fan.d.ts.map +1 -0
  34. package/dist/device/fan.js +540 -0
  35. package/dist/device/fan.js.map +1 -0
  36. package/dist/device/hub.d.ts +16 -60
  37. package/dist/device/hub.d.ts.map +1 -1
  38. package/dist/device/hub.js +261 -616
  39. package/dist/device/hub.js.map +1 -1
  40. package/dist/device/humidifier.d.ts +11 -66
  41. package/dist/device/humidifier.d.ts.map +1 -1
  42. package/dist/device/humidifier.js +240 -594
  43. package/dist/device/humidifier.js.map +1 -1
  44. package/dist/device/iosensor.d.ts +11 -64
  45. package/dist/device/iosensor.d.ts.map +1 -1
  46. package/dist/device/iosensor.js +196 -600
  47. package/dist/device/iosensor.js.map +1 -1
  48. package/dist/device/lightstrip.d.ts +11 -69
  49. package/dist/device/lightstrip.d.ts.map +1 -1
  50. package/dist/device/lightstrip.js +281 -586
  51. package/dist/device/lightstrip.js.map +1 -1
  52. package/dist/device/lock.d.ts +16 -63
  53. package/dist/device/lock.d.ts.map +1 -1
  54. package/dist/device/lock.js +252 -560
  55. package/dist/device/lock.js.map +1 -1
  56. package/dist/device/meter.d.ts +14 -64
  57. package/dist/device/meter.d.ts.map +1 -1
  58. package/dist/device/meter.js +191 -593
  59. package/dist/device/meter.js.map +1 -1
  60. package/dist/device/meterplus.d.ts +14 -65
  61. package/dist/device/meterplus.d.ts.map +1 -1
  62. package/dist/device/meterplus.js +180 -593
  63. package/dist/device/meterplus.js.map +1 -1
  64. package/dist/device/motion.d.ts +13 -69
  65. package/dist/device/motion.d.ts.map +1 -1
  66. package/dist/device/motion.js +188 -532
  67. package/dist/device/motion.js.map +1 -1
  68. package/dist/device/plug.d.ts +9 -57
  69. package/dist/device/plug.d.ts.map +1 -1
  70. package/dist/device/plug.js +96 -458
  71. package/dist/device/plug.js.map +1 -1
  72. package/dist/device/robotvacuumcleaner.d.ts +12 -63
  73. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  74. package/dist/device/robotvacuumcleaner.js +202 -521
  75. package/dist/device/robotvacuumcleaner.js.map +1 -1
  76. package/dist/device/waterdetector.d.ts +10 -63
  77. package/dist/device/waterdetector.d.ts.map +1 -1
  78. package/dist/device/waterdetector.js +146 -475
  79. package/dist/device/waterdetector.js.map +1 -1
  80. package/dist/homebridge-ui/public/index.html +22 -16
  81. package/dist/homebridge-ui/server.js +1 -1
  82. package/dist/homebridge-ui/server.js.map +1 -1
  83. package/dist/index.d.ts +1 -1
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +2 -2
  86. package/dist/index.js.map +1 -1
  87. package/dist/irdevice/airconditioner.d.ts +10 -50
  88. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  89. package/dist/irdevice/airconditioner.js +324 -452
  90. package/dist/irdevice/airconditioner.js.map +1 -1
  91. package/dist/irdevice/airpurifier.d.ts +8 -39
  92. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  93. package/dist/irdevice/airpurifier.js +93 -307
  94. package/dist/irdevice/airpurifier.js.map +1 -1
  95. package/dist/irdevice/camera.d.ts +7 -36
  96. package/dist/irdevice/camera.d.ts.map +1 -1
  97. package/dist/irdevice/camera.js +39 -281
  98. package/dist/irdevice/camera.js.map +1 -1
  99. package/dist/irdevice/fan.d.ts +10 -43
  100. package/dist/irdevice/fan.d.ts.map +1 -1
  101. package/dist/irdevice/fan.js +113 -341
  102. package/dist/irdevice/fan.js.map +1 -1
  103. package/dist/irdevice/irdevice.d.ts +40 -0
  104. package/dist/irdevice/irdevice.d.ts.map +1 -0
  105. package/dist/irdevice/irdevice.js +330 -0
  106. package/dist/irdevice/irdevice.js.map +1 -0
  107. package/dist/irdevice/light.d.ts +12 -45
  108. package/dist/irdevice/light.d.ts.map +1 -1
  109. package/dist/irdevice/light.js +118 -362
  110. package/dist/irdevice/light.js.map +1 -1
  111. package/dist/irdevice/other.d.ts +19 -48
  112. package/dist/irdevice/other.d.ts.map +1 -1
  113. package/dist/irdevice/other.js +438 -570
  114. package/dist/irdevice/other.js.map +1 -1
  115. package/dist/irdevice/tv.d.ts +8 -40
  116. package/dist/irdevice/tv.d.ts.map +1 -1
  117. package/dist/irdevice/tv.js +87 -335
  118. package/dist/irdevice/tv.js.map +1 -1
  119. package/dist/irdevice/vacuumcleaner.d.ts +7 -36
  120. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  121. package/dist/irdevice/vacuumcleaner.js +45 -285
  122. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  123. package/dist/irdevice/waterheater.d.ts +7 -36
  124. package/dist/irdevice/waterheater.d.ts.map +1 -1
  125. package/dist/irdevice/waterheater.js +47 -291
  126. package/dist/irdevice/waterheater.js.map +1 -1
  127. package/dist/platform.d.ts +19 -6
  128. package/dist/platform.d.ts.map +1 -1
  129. package/dist/platform.js +310 -172
  130. package/dist/platform.js.map +1 -1
  131. package/dist/settings.d.ts +62 -19
  132. package/dist/settings.d.ts.map +1 -1
  133. package/dist/settings.js +16 -552
  134. package/dist/settings.js.map +1 -1
  135. package/dist/utils.d.ts +77 -0
  136. package/dist/utils.d.ts.map +1 -1
  137. package/dist/utils.js +638 -1
  138. package/dist/utils.js.map +1 -1
  139. package/eslint.config.js +96 -0
  140. package/package.json +19 -15
  141. package/src/custom.d.ts +2 -1
  142. package/src/device/blindtilt.ts +462 -817
  143. package/src/device/bot.ts +862 -931
  144. package/src/device/ceilinglight.ts +259 -633
  145. package/src/device/colorbulb.ts +300 -688
  146. package/src/device/contact.ts +288 -571
  147. package/src/device/curtain.ts +441 -838
  148. package/src/device/device.ts +734 -0
  149. package/src/device/fan.ts +596 -0
  150. package/src/device/hub.ts +299 -645
  151. package/src/device/humidifier.ts +274 -647
  152. package/src/device/iosensor.ts +233 -626
  153. package/src/device/lightstrip.ts +309 -620
  154. package/src/device/lock.ts +292 -601
  155. package/src/device/meter.ts +228 -619
  156. package/src/device/meterplus.ts +216 -619
  157. package/src/device/motion.ts +218 -559
  158. package/src/device/plug.ts +111 -490
  159. package/src/device/robotvacuumcleaner.ts +228 -550
  160. package/src/device/waterdetector.ts +169 -501
  161. package/src/homebridge-ui/public/index.html +22 -16
  162. package/src/homebridge-ui/server.ts +1 -1
  163. package/src/index.ts +4 -3
  164. package/src/irdevice/airconditioner.ts +350 -477
  165. package/src/irdevice/airpurifier.ts +120 -330
  166. package/src/irdevice/camera.ts +53 -301
  167. package/src/irdevice/fan.ts +130 -360
  168. package/src/irdevice/irdevice.ts +346 -0
  169. package/src/irdevice/light.ts +157 -412
  170. package/src/irdevice/other.ts +507 -648
  171. package/src/irdevice/tv.ts +117 -367
  172. package/src/irdevice/vacuumcleaner.ts +59 -311
  173. package/src/irdevice/waterheater.ts +61 -320
  174. package/src/platform.ts +355 -219
  175. package/src/settings.ts +116 -611
  176. package/src/utils.ts +642 -1
  177. package/src/device/waterdetector.json +0 -11
@@ -1,106 +1,76 @@
1
- import { CharacteristicValue, PlatformAccessory, Service, API, Logging, HAP } from 'homebridge';
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * fan.ts: @switchbot/homebridge-switchbot.
4
+ */
2
5
  import { request } from 'undici';
3
- import { SwitchBotPlatform } from '../platform.js';
4
- import { Devices, irDevicesConfig, irdevice, SwitchBotPlatformConfig } from '../settings.js';
6
+ import { Devices } from '../settings.js';
7
+ import { irdeviceBase } from './irdevice.js';
8
+
9
+ import type { SwitchBotPlatform } from '../platform.js';
10
+ import type { irDevicesConfig, irdevice } from '../settings.js';
11
+ import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
5
12
 
6
13
  /**
7
14
  * Platform Accessory
8
15
  * An instance of this class is created for each accessory your platform registers
9
16
  * Each accessory may expose multiple services of different service types.
10
17
  */
11
- export class Fan {
12
- public readonly api: API;
13
- public readonly log: Logging;
14
- public readonly config!: SwitchBotPlatformConfig;
15
- protected readonly hap: HAP;
18
+ export class IRFan extends irdeviceBase {
16
19
  // Services
17
- fanService!: Service;
18
-
19
- // Characteristic Values
20
- Active!: CharacteristicValue;
21
- SwingMode!: CharacteristicValue;
22
- RotationSpeed!: CharacteristicValue;
23
- FirmwareRevision!: CharacteristicValue;
24
- ActiveIdentifier!: CharacteristicValue;
25
- RotationDirection!: CharacteristicValue;
26
-
27
- // Config
28
- minStep?: number;
29
- minValue?: number;
30
- maxValue?: number;
31
- deviceLogging!: string;
32
- disablePushOn?: boolean;
33
- disablePushOff?: boolean;
20
+ private Fan: {
21
+ Name: CharacteristicValue;
22
+ Service: Service;
23
+ Active: CharacteristicValue;
24
+ SwingMode: CharacteristicValue;
25
+ RotationSpeed: CharacteristicValue;
26
+ RotationDirection: CharacteristicValue;
27
+ };
34
28
 
35
29
  constructor(
36
- private readonly platform: SwitchBotPlatform,
37
- private accessory: PlatformAccessory,
38
- public device: irdevice & irDevicesConfig,
30
+ readonly platform: SwitchBotPlatform,
31
+ accessory: PlatformAccessory,
32
+ device: irdevice & irDevicesConfig,
39
33
  ) {
40
- this.api = this.platform.api;
41
- this.log = this.platform.log;
42
- this.config = this.platform.config;
43
- this.hap = this.api.hap;
44
- // default placeholders
45
- this.deviceLogs(device);
46
- this.deviceContext();
47
- this.disablePushOnChanges(device);
48
- this.disablePushOffChanges(device);
49
- this.deviceConfig(device);
50
-
51
- // set accessory information
52
- accessory
53
- .getService(this.hap.Service.AccessoryInformation)!
54
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
55
- .setCharacteristic(this.hap.Characteristic.Model, device.remoteType)
56
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
57
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
58
-
59
- // get the Television service if it exists, otherwise create a new Television service
60
- // you can create multiple services for each accessory
61
- const fanService = `${accessory.displayName} Fan`;
62
- (this.fanService = accessory.getService(this.hap.Service.Fanv2)
63
- || accessory.addService(this.hap.Service.Fanv2)), fanService;
64
-
65
- this.fanService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
66
- if (!this.fanService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
67
- this.fanService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
68
- }
69
-
70
- // handle on / off events using the Active characteristic
71
- this.fanService.getCharacteristic(this.hap.Characteristic.Active).onSet(this.ActiveSet.bind(this));
34
+ super(platform, accessory, device);
35
+
36
+ // Initialize Switch Service
37
+ accessory.context.Fan = accessory.context.Fan ?? {};
38
+ this.Fan = {
39
+ Name: accessory.context.Fan.Name ?? `${accessory.displayName} Fan`,
40
+ Service: accessory.getService(this.hap.Service.Fanv2) ?? accessory.addService(this.hap.Service.Fanv2) as Service,
41
+ Active: accessory.context.Active ?? this.hap.Characteristic.Active.INACTIVE,
42
+ SwingMode: accessory.context.SwingMode ?? this.hap.Characteristic.SwingMode.SWING_DISABLED,
43
+ RotationSpeed: accessory.context.RotationSpeed ?? 0,
44
+ RotationDirection: accessory.context.RotationDirection ?? this.hap.Characteristic.RotationDirection.CLOCKWISE,
45
+ };
46
+ accessory.context.Fan = this.Fan as object;
47
+
48
+ this.Fan.Service
49
+ .setCharacteristic(this.hap.Characteristic.Name, this.Fan.Name)
50
+ .getCharacteristic(this.hap.Characteristic.Active)
51
+ .onGet(() => {
52
+ return this.Fan.Active;
53
+ })
54
+ .onSet(this.ActiveSet.bind(this));
72
55
 
73
56
  if (device.irfan?.rotation_speed) {
74
- if (device.irfan?.set_minStep) {
75
- this.minStep = device.irfan?.set_minStep;
76
- } else {
77
- this.minStep = 1;
78
- }
79
- if (device.irfan?.set_min) {
80
- this.minValue = device.irfan?.set_min;
81
- } else {
82
- this.minValue = 1;
83
- }
84
- if (device.irfan?.set_max) {
85
- this.maxValue = device.irfan?.set_max;
86
- } else {
87
- this.maxValue = 100;
88
- }
89
57
  // handle Rotation Speed events using the RotationSpeed characteristic
90
- this.fanService
58
+ this.Fan.Service
91
59
  .getCharacteristic(this.hap.Characteristic.RotationSpeed)
92
60
  .setProps({
93
- minStep: this.minStep,
94
- minValue: this.minValue,
95
- maxValue: this.maxValue,
61
+ minStep: Number(this.minStep(device)),
62
+ minValue: Number(this.minValue(device)),
63
+ maxValue: Number(this.maxValue(device)),
64
+ })
65
+ .onGet(() => {
66
+ return this.Fan.RotationSpeed;
96
67
  })
97
68
  .onSet(this.RotationSpeedSet.bind(this));
98
- } else if (this.fanService.testCharacteristic(this.hap.Characteristic.RotationSpeed) && !device.irfan?.swing_mode) {
99
- const characteristic = this.fanService.getCharacteristic(this.hap.Characteristic.RotationSpeed);
100
- this.fanService.removeCharacteristic(characteristic);
69
+ } else if (this.Fan.Service.testCharacteristic(this.hap.Characteristic.RotationSpeed) && !device.irfan?.swing_mode) {
70
+ const characteristic = this.Fan.Service.getCharacteristic(this.hap.Characteristic.RotationSpeed);
71
+ this.Fan.Service.removeCharacteristic(characteristic);
101
72
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Rotation Speed Characteristic was removed.`);
102
73
  } else {
103
- // eslint-disable-next-line max-len
104
74
  this.debugLog(
105
75
  `${this.device.remoteType}: ${this.accessory.displayName} RotationSpeed Characteristic was not removed/added, ` +
106
76
  `Clear Cache on ${this.accessory.displayName} to remove Chracteristic`,
@@ -109,13 +79,17 @@ export class Fan {
109
79
 
110
80
  if (device.irfan?.swing_mode) {
111
81
  // handle Osolcation events using the SwingMode characteristic
112
- this.fanService.getCharacteristic(this.hap.Characteristic.SwingMode).onSet(this.SwingModeSet.bind(this));
113
- } else if (this.fanService.testCharacteristic(this.hap.Characteristic.SwingMode) && !device.irfan?.swing_mode) {
114
- const characteristic = this.fanService.getCharacteristic(this.hap.Characteristic.SwingMode);
115
- this.fanService.removeCharacteristic(characteristic);
82
+ this.Fan.Service
83
+ .getCharacteristic(this.hap.Characteristic.SwingMode)
84
+ .onGet(() => {
85
+ return this.Fan.SwingMode;
86
+ })
87
+ .onSet(this.SwingModeSet.bind(this));
88
+ } else if (this.Fan.Service.testCharacteristic(this.hap.Characteristic.SwingMode) && !device.irfan?.swing_mode) {
89
+ const characteristic = this.Fan.Service.getCharacteristic(this.hap.Characteristic.SwingMode);
90
+ this.Fan.Service.removeCharacteristic(characteristic);
116
91
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Swing Mode Characteristic was removed.`);
117
92
  } else {
118
- // eslint-disable-next-line max-len
119
93
  this.debugLog(
120
94
  `${this.device.remoteType}: ${this.accessory.displayName} Swing Mode Characteristic was not removed/added, ` +
121
95
  `Clear Cache on ${this.accessory.displayName} To Remove Chracteristic`,
@@ -123,40 +97,70 @@ export class Fan {
123
97
  }
124
98
  }
125
99
 
100
+ async minStep(device: irdevice & irDevicesConfig): Promise<number> {
101
+ let minStep: number;
102
+ if (device.irfan?.set_minStep) {
103
+ minStep = device.irfan?.set_minStep;
104
+ } else {
105
+ minStep = 1;
106
+ }
107
+ return minStep;
108
+ }
109
+
110
+ async minValue(device: irdevice & irDevicesConfig): Promise<number> {
111
+ let minValue: number;
112
+ if (device.irfan?.set_min) {
113
+ minValue = device.irfan?.set_min;
114
+ } else {
115
+ minValue = 1;
116
+ }
117
+ return minValue;
118
+ }
119
+
120
+ async maxValue(device: irdevice & irDevicesConfig): Promise<number> {
121
+ let maxValue: number;
122
+ if (device.irfan?.set_max) {
123
+ maxValue = device.irfan?.set_max;
124
+ } else {
125
+ maxValue = 100;
126
+ }
127
+ return maxValue;
128
+ }
129
+
126
130
  async SwingModeSet(value: CharacteristicValue): Promise<void> {
127
131
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} SwingMode: ${value}`);
128
- if (value > this.SwingMode) {
129
- this.SwingMode = 1;
132
+ if (value > this.Fan.SwingMode) {
133
+ this.Fan.SwingMode = 1;
130
134
  await this.pushFanOnChanges();
131
135
  await this.pushFanSwingChanges();
132
136
  } else {
133
- this.SwingMode = 0;
137
+ this.Fan.SwingMode = 0;
134
138
  await this.pushFanOnChanges();
135
139
  await this.pushFanSwingChanges();
136
140
  }
137
- this.SwingMode = value;
138
- this.accessory.context.SwingMode = this.SwingMode;
141
+ this.Fan.SwingMode = value;
142
+ this.accessory.context.SwingMode = this.Fan.SwingMode;
139
143
  }
140
144
 
141
145
  async RotationSpeedSet(value: CharacteristicValue): Promise<void> {
142
146
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} RotationSpeed: ${value}`);
143
- if (value > this.RotationSpeed) {
144
- this.RotationSpeed = 1;
147
+ if (value > this.Fan.RotationSpeed) {
148
+ this.Fan.RotationSpeed = 1;
145
149
  this.pushFanSpeedUpChanges();
146
150
  this.pushFanOnChanges();
147
151
  } else {
148
- this.RotationSpeed = 0;
152
+ this.Fan.RotationSpeed = 0;
149
153
  this.pushFanSpeedDownChanges();
150
154
  }
151
- this.RotationSpeed = value;
152
- this.accessory.context.RotationSpeed = this.RotationSpeed;
155
+ this.Fan.RotationSpeed = value;
156
+ this.accessory.context.RotationSpeed = this.Fan.RotationSpeed;
153
157
  }
154
158
 
155
159
  async ActiveSet(value: CharacteristicValue): Promise<void> {
156
160
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${value}`);
157
161
 
158
- this.Active = value;
159
- if (this.Active === this.hap.Characteristic.Active.ACTIVE) {
162
+ this.Fan.Active = value;
163
+ if (this.Fan.Active === this.hap.Characteristic.Active.ACTIVE) {
160
164
  this.pushFanOnChanges();
161
165
  } else {
162
166
  this.pushFanOffChanges();
@@ -173,10 +177,9 @@ export class Fan {
173
177
  * Fan - "command" "highSpeed" "default" = fan speed to high
174
178
  */
175
179
  async pushFanOnChanges(): Promise<void> {
176
- this.debugLog(
177
- `${this.device.remoteType}: ${this.accessory.displayName} pushFanOnChanges Active: ${this.Active},` + ` disablePushOn: ${this.disablePushOn}`,
178
- );
179
- if (this.Active === this.hap.Characteristic.Active.ACTIVE && !this.disablePushOn) {
180
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} pushFanOnChanges Active: ${this.Fan.Active},`
181
+ + ` disablePushOn: ${this.disablePushOn}`);
182
+ if (this.Fan.Active === this.hap.Characteristic.Active.ACTIVE && !this.disablePushOn) {
180
183
  const commandType: string = await this.commandType();
181
184
  const command: string = await this.commandOn();
182
185
  const bodyChange = JSON.stringify({
@@ -190,10 +193,10 @@ export class Fan {
190
193
 
191
194
  async pushFanOffChanges(): Promise<void> {
192
195
  this.debugLog(
193
- `${this.device.remoteType}: ${this.accessory.displayName} pushLightOffChanges Active: ${this.Active},` +
196
+ `${this.device.remoteType}: ${this.accessory.displayName} pushLightOffChanges Active: ${this.Fan.Active},` +
194
197
  ` disablePushOff: ${this.disablePushOff}`,
195
198
  );
196
- if (this.Active === this.hap.Characteristic.Active.INACTIVE && !this.disablePushOff) {
199
+ if (this.Fan.Active === this.hap.Characteristic.Active.INACTIVE && !this.disablePushOff) {
197
200
  const commandType: string = await this.commandType();
198
201
  const command: string = await this.commandOff();
199
202
  const bodyChange = JSON.stringify({
@@ -247,8 +250,10 @@ export class Fan {
247
250
  this.debugWarnLog(`${this.device.remoteType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
248
251
  this.debugWarnLog(`${this.device.remoteType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
249
252
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
250
- this.debugErrorLog(`${this.device.remoteType}: ${this.accessory.displayName} `
253
+ this.debugSuccessLog(`${this.device.remoteType}: ${this.accessory.displayName} `
251
254
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
255
+ this.successLog(`${this.device.remoteType}: ${this.accessory.displayName}`
256
+ + ` request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
252
257
  this.updateHomeKitCharacteristics();
253
258
  } else {
254
259
  this.statusCode(statusCode);
@@ -270,267 +275,32 @@ export class Fan {
270
275
  }
271
276
 
272
277
  async updateHomeKitCharacteristics(): Promise<void> {
273
- if (this.Active === undefined) {
274
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${this.Active}`);
278
+ if (this.Fan.Active === undefined) {
279
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${this.Fan.Active}`);
275
280
  } else {
276
- this.accessory.context.Active = this.Active;
277
- this.fanService?.updateCharacteristic(this.hap.Characteristic.Active, this.Active);
278
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
281
+ this.accessory.context.Active = this.Fan.Active;
282
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.Fan.Active);
283
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Fan.Active}`);
279
284
  }
280
- if (this.SwingMode === undefined) {
281
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} SwingMode: ${this.SwingMode}`);
285
+ if (this.Fan.SwingMode === undefined) {
286
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} SwingMode: ${this.Fan.SwingMode}`);
282
287
  } else {
283
- this.accessory.context.SwingMode = this.SwingMode;
284
- this.fanService?.updateCharacteristic(this.hap.Characteristic.SwingMode, this.SwingMode);
285
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic SwingMode: ${this.SwingMode}`);
288
+ this.accessory.context.SwingMode = this.Fan.SwingMode;
289
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.Fan.SwingMode);
290
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic SwingMode: ${this.Fan.SwingMode}`);
286
291
  }
287
- if (this.RotationSpeed === undefined) {
288
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} RotationSpeed: ${this.RotationSpeed}`);
292
+ if (this.Fan.RotationSpeed === undefined) {
293
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} RotationSpeed: ${this.Fan.RotationSpeed}`);
289
294
  } else {
290
- this.accessory.context.RotationSpeed = this.RotationSpeed;
291
- this.fanService?.updateCharacteristic(this.hap.Characteristic.RotationSpeed, this.RotationSpeed);
292
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.RotationSpeed}`);
293
- }
294
- }
295
-
296
- async disablePushOnChanges(device: irdevice & irDevicesConfig): Promise<void> {
297
- if (device.disablePushOn === undefined) {
298
- this.disablePushOn = false;
299
- } else {
300
- this.disablePushOn = device.disablePushOn;
301
- }
302
- }
303
-
304
- async disablePushOffChanges(device: irdevice & irDevicesConfig): Promise<void> {
305
- if (device.disablePushOff === undefined) {
306
- this.disablePushOff = false;
307
- } else {
308
- this.disablePushOff = device.disablePushOff;
309
- }
310
- }
311
-
312
- async commandType(): Promise<string> {
313
- let commandType: string;
314
- if (this.device.customize) {
315
- commandType = 'customize';
316
- } else {
317
- commandType = 'command';
318
- }
319
- return commandType;
320
- }
321
-
322
- async commandOn(): Promise<string> {
323
- let command: string;
324
- if (this.device.customize && this.device.customOn) {
325
- command = this.device.customOn;
326
- } else {
327
- command = 'turnOn';
328
- }
329
- return command;
330
- }
331
-
332
- async commandOff(): Promise<string> {
333
- let command: string;
334
- if (this.device.customize && this.device.customOff) {
335
- command = this.device.customOff;
336
- } else {
337
- command = 'turnOff';
338
- }
339
- return command;
340
- }
341
-
342
- async statusCode(statusCode: number): Promise<void> {
343
- switch (statusCode) {
344
- case 151:
345
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
346
- break;
347
- case 152:
348
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
349
- break;
350
- case 160:
351
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
352
- break;
353
- case 161:
354
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
355
- break;
356
- case 171:
357
- this.errorLog(
358
- `${this.device.remoteType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
359
- `Hub: ${this.device.hubDeviceId}`,
360
- );
361
- break;
362
- case 190:
363
- this.errorLog(
364
- `${this.device.remoteType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
365
- ` Or command format is invalid, statusCode: ${statusCode}`,
366
- );
367
- break;
368
- case 100:
369
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
370
- break;
371
- case 200:
372
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
373
- break;
374
- case 400:
375
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
376
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
377
- break;
378
- case 401:
379
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
380
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
381
- break;
382
- case 403:
383
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
384
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
385
- break;
386
- case 404:
387
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
388
- + `statusCode: ${statusCode}`);
389
- break;
390
- case 406:
391
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
392
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
393
- break;
394
- case 415:
395
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
396
- + `header that is not supported by the server, statusCode: ${statusCode}`);
397
- break;
398
- case 422:
399
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, but `
400
- + `the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
401
- break;
402
- case 429:
403
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
404
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
405
- break;
406
- case 500:
407
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
408
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
409
- break;
410
- default:
411
- this.infoLog(
412
- `${this.device.remoteType}: ${this.accessory.displayName} Unknown statusCode: ` +
413
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
414
- );
295
+ this.accessory.context.RotationSpeed = this.Fan.RotationSpeed;
296
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, this.Fan.RotationSpeed);
297
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.Fan.RotationSpeed}`);
415
298
  }
416
299
  }
417
300
 
418
301
  async apiError(e: any): Promise<void> {
419
- this.fanService.updateCharacteristic(this.hap.Characteristic.Active, e);
420
- this.fanService.updateCharacteristic(this.hap.Characteristic.RotationSpeed, e);
421
- this.fanService.updateCharacteristic(this.hap.Characteristic.SwingMode, e);
422
- }
423
-
424
- async deviceContext() {
425
- if (this.Active === undefined) {
426
- this.Active = this.hap.Characteristic.Active.INACTIVE;
427
- } else {
428
- this.Active = this.accessory.context.Active;
429
- }
430
- if (this.FirmwareRevision === undefined) {
431
- this.FirmwareRevision = this.platform.version;
432
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
433
- }
434
- }
435
-
436
- async deviceConfig(device: irdevice & irDevicesConfig): Promise<void> {
437
- let config = {};
438
- if (device.irfan) {
439
- config = device.irfan;
440
- }
441
- if (device.logging !== undefined) {
442
- config['logging'] = device.logging;
443
- }
444
- if (device.connectionType !== undefined) {
445
- config['connectionType'] = device.connectionType;
446
- }
447
- if (device.external !== undefined) {
448
- config['external'] = device.external;
449
- }
450
- if (device.customOn !== undefined) {
451
- config['customOn'] = device.customOn;
452
- }
453
- if (device.customOff !== undefined) {
454
- config['customOff'] = device.customOff;
455
- }
456
- if (device.customize !== undefined) {
457
- config['customize'] = device.customize;
458
- }
459
- if (device.disablePushOn !== undefined) {
460
- config['disablePushOn'] = device.disablePushOn;
461
- }
462
- if (device.disablePushOff !== undefined) {
463
- config['disablePushOff'] = device.disablePushOff;
464
- }
465
- if (Object.entries(config).length !== 0) {
466
- this.debugWarnLog(`${this.device.remoteType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
467
- }
468
- }
469
-
470
- async deviceLogs(device: irdevice & irDevicesConfig): Promise<void> {
471
- if (this.platform.debugMode) {
472
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
473
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
474
- } else if (device.logging) {
475
- this.deviceLogging = this.accessory.context.logging = device.logging;
476
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
477
- } else if (this.platform.config.options?.logging) {
478
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
479
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
480
- } else {
481
- this.deviceLogging = this.accessory.context.logging = 'standard';
482
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
483
- }
484
- }
485
-
486
- /**
487
- * Logging for Device
488
- */
489
- infoLog(...log: any[]): void {
490
- if (this.enablingDeviceLogging()) {
491
- this.platform.log.info(String(...log));
492
- }
493
- }
494
-
495
- warnLog(...log: any[]): void {
496
- if (this.enablingDeviceLogging()) {
497
- this.platform.log.warn(String(...log));
498
- }
499
- }
500
-
501
- debugWarnLog(...log: any[]): void {
502
- if (this.enablingDeviceLogging()) {
503
- if (this.deviceLogging?.includes('debug')) {
504
- this.platform.log.warn('[DEBUG]', String(...log));
505
- }
506
- }
507
- }
508
-
509
- errorLog(...log: any[]): void {
510
- if (this.enablingDeviceLogging()) {
511
- this.platform.log.error(String(...log));
512
- }
513
- }
514
-
515
- debugErrorLog(...log: any[]): void {
516
- if (this.enablingDeviceLogging()) {
517
- if (this.deviceLogging?.includes('debug')) {
518
- this.platform.log.error('[DEBUG]', String(...log));
519
- }
520
- }
521
- }
522
-
523
- debugLog(...log: any[]): void {
524
- if (this.enablingDeviceLogging()) {
525
- if (this.deviceLogging === 'debug') {
526
- this.platform.log.info('[DEBUG]', String(...log));
527
- } else {
528
- this.platform.log.debug(String(...log));
529
- }
530
- }
531
- }
532
-
533
- enablingDeviceLogging(): boolean {
534
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
302
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, e);
303
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, e);
304
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, e);
535
305
  }
536
306
  }