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