@switchbot/homebridge-switchbot 3.5.0-beta.6 → 3.5.0-beta.60

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 +408 -779
  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 +696 -822
  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 +237 -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 +279 -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 +242 -544
  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 +402 -805
  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 +684 -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 +535 -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 +246 -613
  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 +232 -593
  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 +185 -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 +277 -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 +241 -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 +181 -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 +170 -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 +178 -531
  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 +92 -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 +197 -521
  75. package/dist/device/robotvacuumcleaner.js.map +1 -1
  76. package/dist/device/waterdetector.d.ts +10 -65
  77. package/dist/device/waterdetector.d.ts.map +1 -1
  78. package/dist/device/waterdetector.js +139 -495
  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 +9 -49
  88. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  89. package/dist/irdevice/airconditioner.js +309 -419
  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 +72 -304
  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 +34 -278
  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 +100 -338
  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 +87 -357
  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 +352 -546
  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 +82 -321
  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 +41 -284
  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 +42 -287
  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 -169
  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 +442 -814
  143. package/src/device/bot.ts +754 -915
  144. package/src/device/ceilinglight.ts +255 -633
  145. package/src/device/colorbulb.ts +294 -685
  146. package/src/device/contact.ts +277 -571
  147. package/src/device/curtain.ts +431 -835
  148. package/src/device/device.ts +709 -0
  149. package/src/device/fan.ts +591 -0
  150. package/src/device/hub.ts +286 -644
  151. package/src/device/humidifier.ts +266 -646
  152. package/src/device/iosensor.ts +220 -625
  153. package/src/device/lightstrip.ts +305 -620
  154. package/src/device/lock.ts +280 -600
  155. package/src/device/meter.ts +216 -617
  156. package/src/device/meterplus.ts +205 -619
  157. package/src/device/motion.ts +206 -556
  158. package/src/device/plug.ts +107 -490
  159. package/src/device/robotvacuumcleaner.ts +223 -550
  160. package/src/device/waterdetector.ts +160 -519
  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 +333 -447
  165. package/src/irdevice/airpurifier.ts +92 -322
  166. package/src/irdevice/camera.ts +45 -294
  167. package/src/irdevice/fan.ts +115 -353
  168. package/src/irdevice/irdevice.ts +346 -0
  169. package/src/irdevice/light.ts +110 -385
  170. package/src/irdevice/other.ts +399 -616
  171. package/src/irdevice/tv.ts +103 -340
  172. package/src/irdevice/vacuumcleaner.ts +52 -306
  173. package/src/irdevice/waterheater.ts +53 -311
  174. package/src/platform.ts +355 -216
  175. package/src/settings.ts +116 -611
  176. package/src/utils.ts +642 -1
  177. package/src/device/waterdetector.json +0 -11
@@ -1,125 +1,107 @@
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
+ * light.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 Light {
12
- public readonly api: API;
13
- public readonly log: Logging;
14
- public readonly config!: SwitchBotPlatformConfig;
15
- protected readonly hap: HAP;
18
+ export class Light extends irdeviceBase {
16
19
  // Services
17
- lightBulbService?: Service;
18
- ProgrammableSwitchServiceOn?: Service;
19
- ProgrammableSwitchServiceOff?: Service;
20
-
21
- // Characteristic Values
22
- On!: CharacteristicValue;
23
- ProgrammableSwitchEventOn?: CharacteristicValue;
24
- ProgrammableSwitchOutputStateOn?: CharacteristicValue;
25
- ProgrammableSwitchEventOff?: CharacteristicValue;
26
- ProgrammableSwitchOutputStateOff?: CharacteristicValue;
27
- FirmwareRevision!: CharacteristicValue;
28
-
29
- // Config
30
- deviceLogging!: string;
31
- disablePushOn?: boolean;
32
- disablePushOff?: boolean;
20
+ private LightBulb?: {
21
+ Service: Service;
22
+ On: CharacteristicValue;
23
+ };
24
+
25
+ private ProgrammableSwitchOn?: {
26
+ Service: Service;
27
+ ProgrammableSwitchEvent: CharacteristicValue;
28
+ ProgrammableSwitchOutputState: CharacteristicValue;
29
+ };
30
+
31
+ private ProgrammableSwitchOff?: {
32
+ Service: Service;
33
+ ProgrammableSwitchEvent: CharacteristicValue;
34
+ ProgrammableSwitchOutputState: CharacteristicValue;
35
+ };
33
36
 
34
37
  constructor(
35
- private readonly platform: SwitchBotPlatform,
36
- private accessory: PlatformAccessory,
37
- public device: irdevice & irDevicesConfig,
38
+ readonly platform: SwitchBotPlatform,
39
+ accessory: PlatformAccessory,
40
+ device: irdevice & irDevicesConfig,
38
41
  ) {
39
- this.api = this.platform.api;
40
- this.log = this.platform.log;
41
- this.config = this.platform.config;
42
- this.hap = this.api.hap;
43
- // default placeholders
44
- this.deviceLogs(device);
45
- this.deviceContext();
46
- this.disablePushOnChanges(device);
47
- this.disablePushOffChanges(device);
48
- this.deviceConfig(device);
49
-
50
- // set accessory information
51
- accessory
52
- .getService(this.hap.Service.AccessoryInformation)!
53
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
54
- .setCharacteristic(this.hap.Characteristic.Model, device.remoteType)
55
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
56
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
42
+ super(platform, accessory, device);
57
43
 
58
44
  if (!device.irlight?.stateless) {
45
+ // Initialize LightBulb property
46
+ this.LightBulb = {
47
+ Service: accessory.getService(this.hap.Service.Lightbulb)! as Service,
48
+ On: accessory.context.On || false,
49
+ };
59
50
  // get the Light service if it exists, otherwise create a new Light service
60
51
  // you can create multiple services for each accessory
61
- const lightBulbService = `${accessory.displayName} ${device.remoteType}`;
62
- (this.lightBulbService = accessory.getService(this.hap.Service.Lightbulb)
63
- || accessory.addService(this.hap.Service.Lightbulb)), lightBulbService;
52
+ const LightBulbService = `${accessory.displayName} ${device.remoteType}`;
53
+ (this.LightBulb.Service = accessory.getService(this.hap.Service.Lightbulb)
54
+ || accessory.addService(this.hap.Service.Lightbulb)), LightBulbService;
64
55
 
65
56
 
66
- this.lightBulbService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
67
- if (!this.lightBulbService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
68
- this.lightBulbService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
69
- }
57
+ this.LightBulb.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
70
58
 
71
59
  // handle on / off events using the On characteristic
72
- this.lightBulbService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
60
+ this.LightBulb.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
73
61
  } else {
74
62
 
75
63
  // create a new Stateful Programmable Switch On service
76
- const ProgrammableSwitchServiceOn = `${accessory.displayName} ${device.remoteType} On`;
77
- (this.ProgrammableSwitchServiceOn = accessory.getService(this.hap.Service.StatefulProgrammableSwitch)
78
- || accessory.addService(this.hap.Service.StatefulProgrammableSwitch)), ProgrammableSwitchServiceOn;
64
+ const ProgrammableSwitchOn = `${accessory.displayName} ${device.remoteType} On`;
65
+ (this.ProgrammableSwitchOn!.Service = accessory.getService(this.hap.Service.StatefulProgrammableSwitch)
66
+ || accessory.addService(this.hap.Service.StatefulProgrammableSwitch)), ProgrammableSwitchOn;
79
67
 
80
68
 
81
- this.ProgrammableSwitchServiceOn.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} On`);
82
- if (!this.ProgrammableSwitchServiceOn.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
83
- this.ProgrammableSwitchServiceOn.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} On`);
84
- }
69
+ this.ProgrammableSwitchOn?.Service.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} On`);
85
70
 
86
- this.ProgrammableSwitchServiceOn.getCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent).setProps({
71
+ this.ProgrammableSwitchOn?.Service.getCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent).setProps({
87
72
  validValueRanges: [0, 0],
88
73
  minValue: 0,
89
74
  maxValue: 0,
90
75
  validValues: [0],
91
76
  })
92
77
  .onGet(() => {
93
- return this.ProgrammableSwitchEventOn!;
78
+ return this.ProgrammableSwitchOn!.ProgrammableSwitchEvent;
94
79
  });
95
80
 
96
- this.ProgrammableSwitchServiceOn.getCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState)
81
+ this.ProgrammableSwitchOn?.Service.getCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState)
97
82
  .onSet(this.ProgrammableSwitchOutputStateSetOn.bind(this));
98
83
 
99
84
 
100
85
 
101
86
  // create a new Stateful Programmable Switch Off service
102
- const ProgrammableSwitchServiceOff = `${accessory.displayName} ${device.remoteType} Off`;
103
- (this.ProgrammableSwitchServiceOff = accessory.getService(this.hap.Service.StatefulProgrammableSwitch)
104
- || accessory.addService(this.hap.Service.StatefulProgrammableSwitch)), ProgrammableSwitchServiceOff;
87
+ const ProgrammableSwitchOff = `${accessory.displayName} ${device.remoteType} Off`;
88
+ (this.ProgrammableSwitchOff!.Service = accessory.getService(this.hap.Service.StatefulProgrammableSwitch)
89
+ || accessory.addService(this.hap.Service.StatefulProgrammableSwitch)), ProgrammableSwitchOff;
105
90
 
106
91
 
107
- this.ProgrammableSwitchServiceOff.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Off`);
108
- if (!this.ProgrammableSwitchServiceOff.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
109
- this.ProgrammableSwitchServiceOff.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Off`);
110
- }
92
+ this.ProgrammableSwitchOff?.Service.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Off`);
111
93
 
112
- this.ProgrammableSwitchServiceOff.getCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent).setProps({
94
+ this.ProgrammableSwitchOff?.Service.getCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent).setProps({
113
95
  validValueRanges: [0, 0],
114
96
  minValue: 0,
115
97
  maxValue: 0,
116
98
  validValues: [0],
117
99
  })
118
100
  .onGet(() => {
119
- return this.ProgrammableSwitchEventOff!;
101
+ return this.ProgrammableSwitchOff!.ProgrammableSwitchEvent;
120
102
  });
121
103
 
122
- this.ProgrammableSwitchServiceOff.getCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState)
104
+ this.ProgrammableSwitchOff?.Service.getCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState)
123
105
  .onSet(this.ProgrammableSwitchOutputStateSetOff.bind(this));
124
106
  }
125
107
 
@@ -128,11 +110,13 @@ export class Light {
128
110
  async OnSet(value: CharacteristicValue): Promise<void> {
129
111
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} On: ${value}`);
130
112
 
131
- this.On = value;
132
- if (this.On) {
133
- await this.pushLightOnChanges();
113
+ this.LightBulb!.On = value;
114
+ if (this.LightBulb?.On) {
115
+ const On = true;
116
+ await this.pushLightOnChanges(On);
134
117
  } else {
135
- await this.pushLightOffChanges();
118
+ const On = false;
119
+ await this.pushLightOffChanges(On);
136
120
  }
137
121
  /**
138
122
  * pushLightOnChanges and pushLightOffChanges above assume they are measuring the state of the accessory BEFORE
@@ -143,10 +127,10 @@ export class Light {
143
127
  async ProgrammableSwitchOutputStateSetOn(value: CharacteristicValue): Promise<void> {
144
128
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} On: ${value}`);
145
129
 
146
- this.ProgrammableSwitchOutputStateOn = value;
147
- if (this.ProgrammableSwitchOutputStateOn === 1) {
148
- this.On = true;
149
- await this.pushLightOnChanges();
130
+ this.ProgrammableSwitchOn!.ProgrammableSwitchOutputState = value;
131
+ if (this.ProgrammableSwitchOn?.ProgrammableSwitchOutputState === 1) {
132
+ const On = true;
133
+ await this.pushLightOnChanges(On);
150
134
  }
151
135
  /**
152
136
  * pushLightOnChanges and pushLightOffChanges above assume they are measuring the state of the accessory BEFORE
@@ -157,10 +141,10 @@ export class Light {
157
141
  async ProgrammableSwitchOutputStateSetOff(value: CharacteristicValue): Promise<void> {
158
142
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} On: ${value}`);
159
143
 
160
- this.ProgrammableSwitchOutputStateOff = value;
161
- if (this.ProgrammableSwitchOutputStateOff === 1) {
162
- this.On = false;
163
- await this.pushLightOffChanges();
144
+ this.ProgrammableSwitchOff!.ProgrammableSwitchOutputState = value;
145
+ if (this.ProgrammableSwitchOff?.ProgrammableSwitchOutputState === 1) {
146
+ const On = false;
147
+ await this.pushLightOffChanges(On);
164
148
  }
165
149
  /**
166
150
  * pushLightOnChanges and pushLightOffChanges above assume they are measuring the state of the accessory BEFORE
@@ -180,11 +164,9 @@ export class Light {
180
164
  * Light - "command" "channelAdd" "default" = next channel
181
165
  * Light - "command" "channelSub" "default" = previous channel
182
166
  */
183
- async pushLightOnChanges(): Promise<void> {
184
- this.debugLog(
185
- `${this.device.remoteType}: ${this.accessory.displayName} pushLightOnChanges On: ${this.On},` + ` disablePushOn: ${this.disablePushOn}`,
186
- );
187
- if (this.On && !this.disablePushOn) {
167
+ async pushLightOnChanges(On: boolean): Promise<void> {
168
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} pushLightOnChanges On: ${On}, disablePushOn: ${this.disablePushOn}`);
169
+ if (On === true && this.disablePushOn === false) {
188
170
  const commandType: string = await this.commandType();
189
171
  const command: string = await this.commandOn();
190
172
  const bodyChange = JSON.stringify({
@@ -192,15 +174,13 @@ export class Light {
192
174
  parameter: 'default',
193
175
  commandType: commandType,
194
176
  });
195
- await this.pushChanges(bodyChange);
177
+ await this.pushChanges(bodyChange, On);
196
178
  }
197
179
  }
198
180
 
199
- async pushLightOffChanges(): Promise<void> {
200
- this.debugLog(
201
- `${this.device.remoteType}: ${this.accessory.displayName} pushLightOffChanges On: ${this.On},` + ` disablePushOff: ${this.disablePushOff}`,
202
- );
203
- if (!this.On && !this.disablePushOff) {
181
+ async pushLightOffChanges(On: boolean): Promise<void> {
182
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} pushLightOffChanges On: ${On}, disablePushOff: ${this.disablePushOff}`);
183
+ if (On === false && this.disablePushOff === false) {
204
184
  const commandType: string = await this.commandType();
205
185
  const command: string = await this.commandOff();
206
186
  const bodyChange = JSON.stringify({
@@ -208,29 +188,11 @@ export class Light {
208
188
  parameter: 'default',
209
189
  commandType: commandType,
210
190
  });
211
- await this.pushChanges(bodyChange);
191
+ await this.pushChanges(bodyChange, On);
212
192
  }
213
193
  }
214
194
 
215
- /*async pushLightBrightnessUpChanges(): Promise<void> {
216
- const bodyChange = JSON.stringify({
217
- command: 'brightnessUp',
218
- parameter: 'default',
219
- commandType: 'command',
220
- });
221
- await this.pushChanges(bodyChange);
222
- }
223
-
224
- async pushLightBrightnessDownChanges(): Promise<void> {
225
- const bodyChange = JSON.stringify({
226
- command: 'brightnessDown',
227
- parameter: 'default',
228
- commandType: 'command',
229
- });
230
- await this.pushChanges(bodyChange);
231
- }*/
232
-
233
- async pushChanges(bodyChange: any): Promise<void> {
195
+ async pushChanges(bodyChange: any, On: boolean): Promise<void> {
234
196
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} pushChanges`);
235
197
  if (this.device.connectionType === 'OpenAPI') {
236
198
  this.infoLog(`${this.device.remoteType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
@@ -245,9 +207,11 @@ export class Light {
245
207
  this.debugWarnLog(`${this.device.remoteType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
246
208
  this.debugWarnLog(`${this.device.remoteType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
247
209
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
248
- this.debugErrorLog(`${this.device.remoteType}: ${this.accessory.displayName} `
210
+ this.debugSuccessLog(`${this.device.remoteType}: ${this.accessory.displayName} `
249
211
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
250
- this.accessory.context.On = this.On;
212
+ this.successLog(`${this.device.remoteType}: ${this.accessory.displayName}`
213
+ + ` request to SwitchBot API, body: ${JSON.stringify(bodyChange)} sent successfully`);
214
+ this.accessory.context.On = On;
251
215
  this.updateHomeKitCharacteristics();
252
216
  } else {
253
217
  this.statusCode(statusCode);
@@ -255,298 +219,59 @@ export class Light {
255
219
  }
256
220
  } catch (e: any) {
257
221
  this.apiError(e);
258
- this.errorLog(
259
- `${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType}` +
260
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
261
- );
222
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType}`
223
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
262
224
  }
263
225
  } else {
264
- this.warnLog(
265
- `${this.device.remoteType}: ${this.accessory.displayName}` +
266
- ` Connection Type: ${this.device.connectionType}, commands will not be sent to OpenAPI`,
267
- );
226
+ this.warnLog(`${this.device.remoteType}: ${this.accessory.displayName} Connection Type: `
227
+ + `${this.device.connectionType}, commands will not be sent to OpenAPI`);
268
228
  }
269
229
  }
270
230
 
271
231
  async updateHomeKitCharacteristics(): Promise<void> {
272
- if (this.device.irlight?.stateless) {
232
+ if (!this.device.irlight?.stateless) {
273
233
  // On
274
- if (this.On === undefined) {
275
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} On: ${this.On}`);
234
+ if (this.LightBulb?.On === undefined) {
235
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} On: ${this.LightBulb?.On}`);
276
236
  } else {
277
- this.accessory.context.On = this.On;
278
- this.lightBulbService?.updateCharacteristic(this.hap.Characteristic.On, this.On);
279
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
237
+ this.accessory.context.On = this.LightBulb.On;
238
+ this.LightBulb?.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
239
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
280
240
  }
281
241
  } else {
282
242
  // On Stateful Programmable Switch
283
- if (this.ProgrammableSwitchOutputStateOn === undefined) {
243
+ if (this.ProgrammableSwitchOn?.ProgrammableSwitchOutputState === undefined) {
284
244
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName}`
285
- + ` ProgrammableSwitchOutputStateOn: ${this.ProgrammableSwitchOutputStateOn}`);
245
+ + ` ProgrammableSwitchOutputStateOn: ${this.ProgrammableSwitchOn?.ProgrammableSwitchOutputState}`);
286
246
  } else {
287
- this.accessory.context.ProgrammableSwitchOutputStateOn = this.ProgrammableSwitchOutputStateOn;
288
- this.ProgrammableSwitchServiceOn?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState,
289
- this.ProgrammableSwitchOutputStateOn);
247
+ this.accessory.context.ProgrammableSwitchOutputStateOn = this.ProgrammableSwitchOn.ProgrammableSwitchOutputState;
248
+ this.ProgrammableSwitchOn?.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState,
249
+ this.ProgrammableSwitchOn.ProgrammableSwitchOutputState);
290
250
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic`
291
- + ` ProgrammableSwitchOutputStateOn: ${this.ProgrammableSwitchOutputStateOn}`);
251
+ + ` ProgrammableSwitchOutputStateOn: ${this.ProgrammableSwitchOn.ProgrammableSwitchOutputState}`);
292
252
  }
293
253
  // Off Stateful Programmable Switch
294
- if (this.ProgrammableSwitchOutputStateOff === undefined) {
254
+ if (this.ProgrammableSwitchOff?.ProgrammableSwitchOutputState === undefined) {
295
255
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName}`
296
- + ` ProgrammableSwitchOutputStateOff: ${this.ProgrammableSwitchOutputStateOff}`);
256
+ + ` ProgrammableSwitchOutputStateOff: ${this.ProgrammableSwitchOff?.ProgrammableSwitchOutputState}`);
297
257
  } else {
298
- this.accessory.context.ProgrammableSwitchOutputStateOff = this.ProgrammableSwitchOutputStateOff;
299
- this.ProgrammableSwitchServiceOff?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState,
300
- this.ProgrammableSwitchOutputStateOff);
258
+ this.accessory.context.ProgrammableSwitchOutputStateOff = this.ProgrammableSwitchOff.ProgrammableSwitchOutputState;
259
+ this.ProgrammableSwitchOff.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState,
260
+ this.ProgrammableSwitchOff.ProgrammableSwitchOutputState);
301
261
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic`
302
- + ` ProgrammableSwitchOutputStateOff: ${this.ProgrammableSwitchOutputStateOff}`);
262
+ + ` ProgrammableSwitchOutputStateOff: ${this.ProgrammableSwitchOff?.ProgrammableSwitchOutputState}`);
303
263
  }
304
264
  }
305
265
  }
306
266
 
307
- async disablePushOnChanges(device: irdevice & irDevicesConfig): Promise<void> {
308
- if (device.disablePushOn === undefined) {
309
- this.disablePushOn = false;
310
- } else {
311
- this.disablePushOn = device.disablePushOn;
312
- }
313
- }
314
-
315
- async disablePushOffChanges(device: irdevice & irDevicesConfig): Promise<void> {
316
- if (device.disablePushOff === undefined) {
317
- this.disablePushOff = false;
318
- } else {
319
- this.disablePushOff = device.disablePushOff;
320
- }
321
- }
322
-
323
- async commandType(): Promise<string> {
324
- let commandType: string;
325
- if (this.device.customize) {
326
- commandType = 'customize';
327
- } else {
328
- commandType = 'command';
329
- }
330
- return commandType;
331
- }
332
-
333
- async commandOn(): Promise<string> {
334
- let command: string;
335
- if (this.device.customize && this.device.customOn) {
336
- command = this.device.customOn;
337
- } else {
338
- command = 'turnOn';
339
- }
340
- return command;
341
- }
342
-
343
- async commandOff(): Promise<string> {
344
- let command: string;
345
- if (this.device.customize && this.device.customOff) {
346
- command = this.device.customOff;
347
- } else {
348
- command = 'turnOff';
349
- }
350
- return command;
351
- }
352
-
353
- async statusCode(statusCode: number): Promise<void> {
354
- switch (statusCode) {
355
- case 151:
356
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
357
- break;
358
- case 152:
359
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
360
- break;
361
- case 160:
362
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
363
- break;
364
- case 161:
365
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
366
- break;
367
- case 171:
368
- this.errorLog(
369
- `${this.device.remoteType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
370
- `Hub: ${this.device.hubDeviceId}`,
371
- );
372
- break;
373
- case 190:
374
- this.errorLog(
375
- `${this.device.remoteType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
376
- ` Or command format is invalid, statusCode: ${statusCode}`,
377
- );
378
- break;
379
- case 100:
380
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
381
- break;
382
- case 200:
383
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
384
- break;
385
- case 400:
386
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
387
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
388
- break;
389
- case 401:
390
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
391
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
392
- break;
393
- case 403:
394
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
395
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
396
- break;
397
- case 404:
398
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
399
- + `statusCode: ${statusCode}`);
400
- break;
401
- case 406:
402
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
403
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
404
- break;
405
- case 415:
406
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
407
- + `header that is not supported by the server, statusCode: ${statusCode}`);
408
- break;
409
- case 422:
410
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, but `
411
- + `the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
412
- break;
413
- case 429:
414
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
415
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
416
- break;
417
- case 500:
418
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
419
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
420
- break;
421
- default:
422
- this.infoLog(
423
- `${this.device.remoteType}: ${this.accessory.displayName} Unknown statusCode: ` +
424
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
425
- );
426
- }
427
- }
428
-
429
267
  async apiError(e: any): Promise<void> {
430
- if (this.device.irlight?.stateless) {
431
- this.lightBulbService?.updateCharacteristic(this.hap.Characteristic.On, e);
432
- } else {
433
- this.ProgrammableSwitchServiceOn?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, e);
434
- this.ProgrammableSwitchServiceOn?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, e);
435
- this.ProgrammableSwitchServiceOff?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, e);
436
- this.ProgrammableSwitchServiceOff?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, e);
437
- }
438
- }
439
-
440
- async deviceContext() {
441
- if (this.On === undefined) {
442
- this.On = false;
443
- } else {
444
- this.On = this.accessory.context.On;
445
- }
446
- if (this.FirmwareRevision === undefined) {
447
- this.FirmwareRevision = this.platform.version;
448
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
449
- }
450
- }
451
-
452
- async deviceConfig(device: irdevice & irDevicesConfig): Promise<void> {
453
- let config = {};
454
- if (device.irlight) {
455
- config = device.irlight;
456
- }
457
- if (device.logging !== undefined) {
458
- config['logging'] = device.logging;
459
- }
460
- if (device.connectionType !== undefined) {
461
- config['connectionType'] = device.connectionType;
462
- }
463
- if (device.external !== undefined) {
464
- config['external'] = device.external;
465
- }
466
- if (device.customOn !== undefined) {
467
- config['customOn'] = device.customOn;
468
- }
469
- if (device.customOff !== undefined) {
470
- config['customOff'] = device.customOff;
471
- }
472
- if (device.customize !== undefined) {
473
- config['customize'] = device.customize;
474
- }
475
- if (device.disablePushOn !== undefined) {
476
- config['disablePushOn'] = device.disablePushOn;
477
- }
478
- if (device.disablePushOff !== undefined) {
479
- config['disablePushOff'] = device.disablePushOff;
480
- }
481
- if (Object.entries(config).length !== 0) {
482
- this.debugWarnLog(`${this.device.remoteType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
483
- }
484
- }
485
-
486
- async deviceLogs(device: irdevice & irDevicesConfig): Promise<void> {
487
- if (this.platform.debugMode) {
488
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
489
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
490
- } else if (device.logging) {
491
- this.deviceLogging = this.accessory.context.logging = device.logging;
492
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
493
- } else if (this.platform.config.options?.logging) {
494
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
495
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
268
+ if (!this.device.irlight?.stateless) {
269
+ this.LightBulb?.Service.updateCharacteristic(this.hap.Characteristic.On, e);
496
270
  } else {
497
- this.deviceLogging = this.accessory.context.logging = 'standard';
498
- this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
499
- }
500
- }
501
-
502
- /**
503
- * Logging for Device
504
- */
505
- infoLog(...log: any[]): void {
506
- if (this.enablingDeviceLogging()) {
507
- this.platform.log.info(String(...log));
271
+ this.ProgrammableSwitchOn?.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, e);
272
+ this.ProgrammableSwitchOn?.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, e);
273
+ this.ProgrammableSwitchOff?.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, e);
274
+ this.ProgrammableSwitchOff?.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, e);
508
275
  }
509
276
  }
510
-
511
- warnLog(...log: any[]): void {
512
- if (this.enablingDeviceLogging()) {
513
- this.platform.log.warn(String(...log));
514
- }
515
- }
516
-
517
- debugWarnLog(...log: any[]): void {
518
- if (this.enablingDeviceLogging()) {
519
- if (this.deviceLogging?.includes('debug')) {
520
- this.platform.log.warn('[DEBUG]', String(...log));
521
- }
522
- }
523
- }
524
-
525
- errorLog(...log: any[]): void {
526
- if (this.enablingDeviceLogging()) {
527
- this.platform.log.error(String(...log));
528
- }
529
- }
530
-
531
- debugErrorLog(...log: any[]): void {
532
- if (this.enablingDeviceLogging()) {
533
- if (this.deviceLogging?.includes('debug')) {
534
- this.platform.log.error('[DEBUG]', String(...log));
535
- }
536
- }
537
- }
538
-
539
- debugLog(...log: any[]): void {
540
- if (this.enablingDeviceLogging()) {
541
- if (this.deviceLogging === 'debug') {
542
- this.platform.log.info('[DEBUG]', String(...log));
543
- } else {
544
- this.platform.log.debug(String(...log));
545
- }
546
- }
547
- }
548
-
549
- enablingDeviceLogging(): boolean {
550
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
551
- }
552
277
  }