homebridge-bond 3.2.9

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.
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FlameService = exports.WindowCoveringService = exports.ButtonService = exports.SwitchService = exports.LightbulbService = exports.FanService = void 0;
4
+ const Device_1 = require("./interface/Device");
5
+ const Observer_1 = require("./Observer");
6
+ class FanService {
7
+ constructor(platform, accessory) {
8
+ let service = accessory.getService(platform.Service.Fan);
9
+ const device = accessory.context.device;
10
+ if (service === undefined) {
11
+ service = accessory.addService(platform.Service.Fan, accessory.displayName);
12
+ }
13
+ this.on = service.getCharacteristic(platform.Characteristic.On);
14
+ if (Device_1.Device.canSetSpeed(device)) {
15
+ this.rotationSpeed = service.getCharacteristic(platform.Characteristic.RotationSpeed);
16
+ }
17
+ if (Device_1.Device.hasReverseSwitch(device)) {
18
+ this.rotationDirection = service.getCharacteristic(platform.Characteristic.RotationDirection);
19
+ }
20
+ }
21
+ }
22
+ exports.FanService = FanService;
23
+ class LightbulbService {
24
+ constructor(platform, accessory, name, subType) {
25
+ let service = accessory.getService(platform.Service.Lightbulb);
26
+ const device = accessory.context.device;
27
+ if (subType) {
28
+ service = accessory.getServiceById(platform.Service.Lightbulb, subType);
29
+ }
30
+ if (service === undefined) {
31
+ service = accessory.addService(platform.Service.Lightbulb, name, subType);
32
+ }
33
+ this.on = service.getCharacteristic(platform.Characteristic.On);
34
+ const brightness = service.getCharacteristic(platform.Characteristic.Brightness);
35
+ if (Device_1.Device.LThasBrightness(device)) {
36
+ this.brightness = brightness;
37
+ }
38
+ else {
39
+ // Fixing bug from 3.1.0 where brightness was added to lights unintentionally
40
+ service.removeCharacteristic(brightness);
41
+ }
42
+ this.subType = subType;
43
+ }
44
+ updateState(state) {
45
+ if (this.subType === 'UpLight') {
46
+ this.on.updateValue(state.up_light === 1 && state.light === 1);
47
+ }
48
+ else if (this.subType === 'DownLight') {
49
+ this.on.updateValue(state.down_light === 1 && state.light === 1);
50
+ }
51
+ else {
52
+ this.on.updateValue(state.light === 1);
53
+ }
54
+ if (this.brightness && state.brightness) {
55
+ this.brightness.updateValue(state.brightness);
56
+ }
57
+ }
58
+ observe(platform, bond, accessory) {
59
+ const device = accessory.context.device;
60
+ this.observeLight(platform, bond, device, accessory);
61
+ this.observeLightBrightness(platform, bond, device, accessory);
62
+ }
63
+ observeLight(platform, bond, device, accessory) {
64
+ Observer_1.Observer.set(this.on, (value, callback) => {
65
+ let promise;
66
+ const subtype = this.subType;
67
+ if (subtype === 'UpLight') {
68
+ promise = bond.api.toggleUpLight(device, callback);
69
+ }
70
+ else if (subtype === 'DownLight') {
71
+ promise = bond.api.toggleDownLight(device, callback);
72
+ }
73
+ else {
74
+ promise = bond.api.toggleLight(device, callback);
75
+ }
76
+ promise
77
+ .then(() => {
78
+ platform.debug(accessory, `Set light power: ${value}`);
79
+ })
80
+ .catch((error) => {
81
+ platform.error(accessory, `Error setting light power: ${error}`);
82
+ });
83
+ });
84
+ }
85
+ observeLightBrightness(platform, bond, device, accessory) {
86
+ if (!this.brightness) {
87
+ return;
88
+ }
89
+ Observer_1.Observer.set(this.brightness, (value, callback) => {
90
+ if (value === 0) {
91
+ // Value of 0 is the same as turning the light off.
92
+ // Ignore and complete callback.
93
+ callback(null);
94
+ return;
95
+ }
96
+ bond.api.setBrightness(device, value, callback)
97
+ .then(() => {
98
+ platform.debug(accessory, `Set light brightness: ${value}`);
99
+ })
100
+ .catch((error) => {
101
+ platform.error(accessory, `Error setting light brightness: ${error}`);
102
+ });
103
+ });
104
+ }
105
+ }
106
+ exports.LightbulbService = LightbulbService;
107
+ class SwitchService {
108
+ constructor(platform, accessory, name, subType) {
109
+ // Check for service by subtype
110
+ let service = accessory.getServiceById(platform.Service.Switch, subType);
111
+ if (service === undefined) {
112
+ service = accessory.addService(platform.Service.Switch, name, subType);
113
+ }
114
+ // Set the subtype if not defined
115
+ if (service.subtype === undefined) {
116
+ service.subtype = subType;
117
+ }
118
+ this.on = service.getCharacteristic(platform.Characteristic.On);
119
+ this.subType = subType;
120
+ }
121
+ }
122
+ exports.SwitchService = SwitchService;
123
+ // ButtonService is a switch that resets itself after 500ms. This provides a
124
+ // button like experience that isn't available in homebridge.
125
+ class ButtonService {
126
+ constructor(platform, accessory, name, subType) {
127
+ // Check for service by subtype
128
+ let service = accessory.getServiceById(platform.Service.Switch, subType);
129
+ if (service === undefined) {
130
+ service = accessory.addService(platform.Service.Switch, name, subType);
131
+ }
132
+ // Set the subtype if not defined
133
+ if (service.subtype === undefined) {
134
+ service.subtype = subType;
135
+ }
136
+ this.on = service.getCharacteristic(platform.Characteristic.On);
137
+ this.on.setValue(false);
138
+ this.on.on('set', () => {
139
+ const timer = setInterval(() => {
140
+ this.on.updateValue(false);
141
+ clearInterval(timer);
142
+ }, 500);
143
+ });
144
+ this.subType = subType;
145
+ }
146
+ }
147
+ exports.ButtonService = ButtonService;
148
+ class WindowCoveringService {
149
+ constructor(platform, accessory) {
150
+ let service = accessory.getService(platform.Service.WindowCovering);
151
+ if (service === undefined) {
152
+ service = accessory.addService(platform.Service.WindowCovering, accessory.displayName);
153
+ }
154
+ this.currentPosition = service.getCharacteristic(platform.Characteristic.CurrentPosition);
155
+ this.targetPosition = service.getCharacteristic(platform.Characteristic.TargetPosition);
156
+ this.positionState = service.getCharacteristic(platform.Characteristic.PositionState);
157
+ }
158
+ }
159
+ exports.WindowCoveringService = WindowCoveringService;
160
+ class FlameService {
161
+ constructor(platform, accessory, name, subType) {
162
+ let service = accessory.getService(platform.Service.Lightbulb);
163
+ const device = accessory.context.device;
164
+ if (subType) {
165
+ service = accessory.getServiceById(platform.Service.Lightbulb, subType);
166
+ }
167
+ if (service === undefined) {
168
+ service = accessory.addService(platform.Service.Lightbulb, name, subType);
169
+ }
170
+ this.on = service.getCharacteristic(platform.Characteristic.On);
171
+ if (Device_1.Device.FPhasFlame(device)) {
172
+ const flame = service.getCharacteristic(platform.Characteristic.Brightness);
173
+ this.flame = flame;
174
+ }
175
+ }
176
+ updateState(state) {
177
+ if (this.flame && state.flame) {
178
+ this.flame.updateValue(state.flame);
179
+ }
180
+ }
181
+ observe(platform, bond, accessory) {
182
+ const device = accessory.context.device;
183
+ this.observePower(platform, bond, device, accessory);
184
+ this.observeFlame(platform, bond, device, accessory);
185
+ }
186
+ observePower(platform, bond, device, accessory) {
187
+ Observer_1.Observer.set(this.on, (value, callback) => {
188
+ bond.api.togglePower(device, callback)
189
+ .then(() => {
190
+ platform.debug(accessory, `Set flame power: ${value}`);
191
+ })
192
+ .catch((error) => {
193
+ platform.error(accessory, `Error setting flame power: ${error}`);
194
+ });
195
+ });
196
+ }
197
+ observeFlame(platform, bond, device, accessory) {
198
+ if (!this.flame) {
199
+ return;
200
+ }
201
+ Observer_1.Observer.set(this.flame, (value, callback) => {
202
+ if (value === 0) {
203
+ // Value of 0 is the same as turning the flame off.
204
+ // Ignore and complete callback.
205
+ callback(null);
206
+ return;
207
+ }
208
+ bond.api.setFlame(device, value, callback)
209
+ .then(() => {
210
+ platform.debug(accessory, `Set flame brightness: ${value}`);
211
+ })
212
+ .catch((error) => {
213
+ platform.error(accessory, `Error setting flame brightness: ${error}`);
214
+ });
215
+ });
216
+ }
217
+ }
218
+ exports.FlameService = FlameService;
@@ -0,0 +1,344 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CeilingFanAccessory = void 0;
4
+ const Device_1 = require("../interface/Device");
5
+ const Services_1 = require("../Services");
6
+ const Observer_1 = require("../Observer");
7
+ class CeilingFanAccessory {
8
+ constructor(platform, accessory, bond) {
9
+ const config = platform.config;
10
+ this.platform = platform;
11
+ this.accessory = accessory;
12
+ const fanSpeedValues = config.fan_speed_values;
13
+ const includeDimmer = config.include_dimmer;
14
+ const includeToggle = config.include_toggle_state;
15
+ const device = accessory.context.device;
16
+ this.values = Device_1.Device.fanSpeeds(device);
17
+ this.minStep = Math.floor(100 / this.values.length);
18
+ this.maxValue = this.minStep * this.values.length;
19
+ if (fanSpeedValues) {
20
+ this.minStep = 1;
21
+ this.maxValue = this.values.length;
22
+ }
23
+ this.fanService = new Services_1.FanService(platform, accessory);
24
+ if (device.properties.max_speed === undefined) {
25
+ if (Device_1.Device.canIncreaseDecreaseSpeed(device)) {
26
+ this.increaseSpeedService = new Services_1.ButtonService(platform, accessory, `${accessory.displayName} Increase Speed`, 'IncreaseSpeed');
27
+ this.decreaseSpeedService = new Services_1.ButtonService(platform, accessory, `${accessory.displayName} Decrease Speed`, 'DecreaseSpeed');
28
+ }
29
+ else {
30
+ this.removeService(`${accessory.displayName} Increase Speed`);
31
+ this.removeService(`${accessory.displayName} Decrease Speed`);
32
+ this.platform.error(accessory, 'Fan Speed is not supported (missing max_speed property or IncreaseSpeed/DescreaseSpeed actions).');
33
+ }
34
+ }
35
+ if (Device_1.Device.CFhasUpDownLight(device)) {
36
+ this.upLightService = new Services_1.LightbulbService(platform, accessory, `${accessory.displayName} Up Light`, 'UpLight');
37
+ this.downLightService = new Services_1.LightbulbService(platform, accessory, `${accessory.displayName} Down Light`, 'DownLight');
38
+ if (includeToggle) {
39
+ this.toggleUpLightService = new Services_1.ButtonService(platform, accessory, 'Toggle Up Light State', 'ToggleUpLight');
40
+ this.toggleDownLightService = new Services_1.ButtonService(platform, accessory, 'Toggle Down Light State', 'ToggleDownLight');
41
+ }
42
+ else {
43
+ // Remove services if previously added
44
+ this.removeService('Toggle Up Light State');
45
+ this.removeService('Toggle Down Light State');
46
+ }
47
+ if (includeDimmer && Device_1.Device.HasDimmer(device)) {
48
+ this.upLightDimmerService = new Services_1.SwitchService(platform, accessory, `${accessory.displayName} Up Light Dimmer`, 'UpLight');
49
+ this.downLightDimmerService = new Services_1.SwitchService(platform, accessory, `${accessory.displayName} Down Light Dimmer`, 'DownLight');
50
+ }
51
+ else {
52
+ // Remove services if previously added
53
+ this.removeService(`${accessory.displayName} Up Light Dimmer`);
54
+ this.removeService(`${accessory.displayName} Down Light Dimmer`);
55
+ }
56
+ }
57
+ else if (Device_1.Device.CFhasLightbulb(device)) {
58
+ this.lightService = new Services_1.LightbulbService(platform, accessory, `${accessory.displayName} Light`);
59
+ if (includeToggle) {
60
+ this.toggleLightService = new Services_1.ButtonService(platform, accessory, 'Toggle Light State', 'ToggleState');
61
+ }
62
+ else {
63
+ this.removeService('Toggle Light State');
64
+ }
65
+ if (includeDimmer && Device_1.Device.HasDimmer(device)) {
66
+ this.dimmerService = new Services_1.SwitchService(platform, accessory, `${accessory.displayName} Dimmer`, 'Dimmer');
67
+ }
68
+ else {
69
+ // Remove service if previously added
70
+ this.removeService(`${accessory.displayName} Dimmer`);
71
+ }
72
+ }
73
+ if (includeDimmer && Device_1.Device.HasSeparateDimmers(device)) {
74
+ this.increaseBrightnessService = new Services_1.SwitchService(platform, accessory, `${accessory.displayName} Increase Brightness`, 'IncreaseBrightness');
75
+ this.decreaseBrightnessService = new Services_1.SwitchService(platform, accessory, `${accessory.displayName} Decrease Brightness`, 'DecreaseBrightness');
76
+ }
77
+ else {
78
+ // Remove service if previously added
79
+ this.removeService(`${accessory.displayName} Increase Brightness`);
80
+ this.removeService(`${accessory.displayName} Decrease Brightness`);
81
+ }
82
+ this.observe(bond);
83
+ }
84
+ updateState(state) {
85
+ // Power
86
+ this.fanService.on.updateValue(state.power === 1);
87
+ // Speed
88
+ if (this.fanService.rotationSpeed) {
89
+ let value = 0;
90
+ if (state.speed && state.power === 1) {
91
+ const index = this.values.indexOf(state.speed) + 1;
92
+ const step = index * this.minStep;
93
+ value = step;
94
+ }
95
+ this.fanService.rotationSpeed.updateValue(value);
96
+ }
97
+ // Rotation direction
98
+ if (this.fanService.rotationDirection) {
99
+ let direction = 0;
100
+ if (state.direction) {
101
+ // Bond state direction is 1 / -1, Homekit direction is 1 / 0
102
+ direction = state.direction === 1 ? 1 : 0;
103
+ this.fanService.rotationDirection.updateValue(direction);
104
+ }
105
+ }
106
+ // Light
107
+ if (this.lightService) {
108
+ this.lightService.updateState(state);
109
+ }
110
+ // Up Light
111
+ if (this.upLightService) {
112
+ this.upLightService.updateState(state);
113
+ }
114
+ // Down Light
115
+ if (this.downLightService) {
116
+ this.downLightService.updateState(state);
117
+ }
118
+ }
119
+ observe(bond) {
120
+ const device = this.accessory.context.device;
121
+ this.observeFanPower(bond, device);
122
+ this.observeFanSpeed(bond, device);
123
+ this.observeFanDirection(bond, device);
124
+ this.observeFanIncreaseSpeed(bond, device);
125
+ this.observeFanDecreaseSpeed(bond, device);
126
+ if (this.lightService) {
127
+ this.lightService.observe(this.platform, bond, this.accessory);
128
+ }
129
+ this.observeLightToggle(bond, device, this.toggleLightService);
130
+ this.observeLightDimmer(bond, device, this.dimmerService);
131
+ if (this.upLightService) {
132
+ this.upLightService.observe(this.platform, bond, this.accessory);
133
+ }
134
+ this.observeLightToggle(bond, device, this.toggleUpLightService);
135
+ this.observeLightDimmer(bond, device, this.upLightDimmerService);
136
+ if (this.downLightService) {
137
+ this.downLightService.observe(this.platform, bond, this.accessory);
138
+ }
139
+ this.observeLightToggle(bond, device, this.toggleDownLightService);
140
+ this.observeLightDimmer(bond, device, this.downLightDimmerService);
141
+ this.observeLightIncreaseBrightness(bond, device, this.decreaseBrightnessService);
142
+ this.observeLightDecreaseBrightness(bond, device, this.increaseBrightnessService);
143
+ // Set initial state
144
+ bond.api.getState(device.id).then(state => {
145
+ this.updateState(state);
146
+ });
147
+ }
148
+ observeFanPower(bond, device) {
149
+ if (!Device_1.Device.hasOffOn(device)) {
150
+ this.platform.error(this.accessory, 'CeilingFanAccessory does not have required actions for fan service.');
151
+ return;
152
+ }
153
+ Observer_1.Observer.set(this.fanService.on, (value, callback) => {
154
+ bond.api.toggleFan(device, value, callback)
155
+ .then(() => {
156
+ this.platform.debug(this.accessory, `Set fan power: ${value}`);
157
+ })
158
+ .catch((error) => {
159
+ this.platform.error(this.accessory, `Error setting fan power: ${error}`);
160
+ });
161
+ });
162
+ }
163
+ observeFanSpeed(bond, device) {
164
+ if (!this.fanService.rotationSpeed) {
165
+ return;
166
+ }
167
+ const props = {
168
+ maxValue: this.maxValue,
169
+ minStep: this.minStep,
170
+ };
171
+ this.fanService.rotationSpeed.setProps(props);
172
+ Observer_1.Observer.set(this.fanService.rotationSpeed, (step, callback) => {
173
+ if (step === 0) {
174
+ // Step of 0 is the same as turning the fan off. This is handled in the fan power observer
175
+ callback(null);
176
+ return;
177
+ }
178
+ const index = step / this.minStep - 1;
179
+ const speed = this.values[index];
180
+ bond.api.setFanSpeed(device, speed, callback)
181
+ .then(() => {
182
+ this.platform.debug(this.accessory, `Set fan speed: ${speed}`);
183
+ })
184
+ .catch((error) => {
185
+ this.platform.error(this.accessory, `Error setting fan speed: ${error}`);
186
+ });
187
+ });
188
+ }
189
+ observeFanDirection(bond, device) {
190
+ if (!this.fanService.rotationDirection) {
191
+ return;
192
+ }
193
+ Observer_1.Observer.set(this.fanService.rotationDirection, (value, callback) => {
194
+ bond.api.toggleDirection(device, callback)
195
+ .then(() => {
196
+ this.platform.debug(this.accessory, `Set fan direction: ${value}`);
197
+ })
198
+ .catch((error) => {
199
+ this.platform.error(this.accessory, `Error setting fan direction: ${error}`);
200
+ });
201
+ });
202
+ }
203
+ observeFanIncreaseSpeed(bond, device) {
204
+ if (!this.increaseSpeedService) {
205
+ return;
206
+ }
207
+ Observer_1.Observer.set(this.increaseSpeedService.on, (value, callback) => {
208
+ bond.api.increaseSpeed(device, callback)
209
+ .then(() => {
210
+ this.platform.debug(this.accessory, `Increased fan speed: ${value}`);
211
+ })
212
+ .catch((error) => {
213
+ this.platform.error(this.accessory, `Error increasing fan speed: ${error}`);
214
+ });
215
+ });
216
+ }
217
+ observeFanDecreaseSpeed(bond, device) {
218
+ if (!this.decreaseSpeedService) {
219
+ return;
220
+ }
221
+ Observer_1.Observer.set(this.decreaseSpeedService.on, (value, callback) => {
222
+ bond.api.decreaseSpeed(device, callback)
223
+ .then(() => {
224
+ this.platform.debug(this.accessory, `Decreased fan speed: ${value}`);
225
+ })
226
+ .catch((error) => {
227
+ this.platform.error(this.accessory, `Error decreasing fan speed: ${error}`);
228
+ });
229
+ });
230
+ }
231
+ observeLightToggle(bond, device, service) {
232
+ if (!service) {
233
+ return;
234
+ }
235
+ Observer_1.Observer.set(service.on, (_, callback) => {
236
+ let promise;
237
+ const subtype = service.subType;
238
+ if (subtype === 'UpLight') {
239
+ promise = bond.api.toggleState(device, 'up_light', callback);
240
+ }
241
+ else if (subtype === 'DownLight') {
242
+ promise = bond.api.toggleState(device, 'down_light', callback);
243
+ }
244
+ else {
245
+ promise = bond.api.toggleState(device, 'light', callback);
246
+ }
247
+ promise
248
+ .then(() => {
249
+ this.platform.debug(this.accessory, 'Light state toggled');
250
+ })
251
+ .catch((error) => {
252
+ this.platform.error(this.accessory, `Error toggling light state: ${error}`);
253
+ });
254
+ });
255
+ }
256
+ observeLightDimmer(bond, device, service) {
257
+ if (!service) {
258
+ return;
259
+ }
260
+ Observer_1.Observer.set(service.on, (value, callback) => {
261
+ let promise;
262
+ if (value === true) {
263
+ const subtype = service.subType;
264
+ if (subtype === 'UpLight') {
265
+ promise = bond.api.startUpLightDimmer(device, callback);
266
+ }
267
+ else if (subtype === 'DownLight') {
268
+ promise = bond.api.startDownLightDimmer(device, callback);
269
+ }
270
+ else {
271
+ promise = bond.api.startDimmer(device, callback);
272
+ }
273
+ }
274
+ else {
275
+ promise = bond.api.stop(device, callback);
276
+ }
277
+ promise
278
+ .then(() => {
279
+ this.platform.debug(this.accessory, `Toggled dimmer: ${value}`);
280
+ })
281
+ .catch((error) => {
282
+ this.platform.error(this.accessory, `Error toggling dimmer: ${error}`);
283
+ });
284
+ });
285
+ }
286
+ observeLightIncreaseBrightness(bond, device, downService) {
287
+ if (!this.increaseBrightnessService) {
288
+ return;
289
+ }
290
+ Observer_1.Observer.set(this.increaseBrightnessService.on, (value, callback) => {
291
+ let promise;
292
+ if (value === true) {
293
+ if (downService) {
294
+ bond.api.stop(device);
295
+ downService.on.updateValue(false);
296
+ }
297
+ promise = bond.api.startIncreasingBrightness(device, callback);
298
+ }
299
+ else {
300
+ promise = bond.api.stop(device, callback);
301
+ }
302
+ promise
303
+ .then(() => {
304
+ this.platform.debug(this.accessory, `Increased Brightness: ${value}`);
305
+ })
306
+ .catch((error) => {
307
+ this.platform.error(this.accessory, `Error increasing brightness: ${error}`);
308
+ });
309
+ });
310
+ }
311
+ observeLightDecreaseBrightness(bond, device, upService) {
312
+ if (!this.decreaseBrightnessService) {
313
+ return;
314
+ }
315
+ Observer_1.Observer.set(this.decreaseBrightnessService.on, (value, callback) => {
316
+ let promise;
317
+ if (value === true) {
318
+ if (upService) {
319
+ bond.api.stop(device);
320
+ upService.on.updateValue(false);
321
+ }
322
+ promise = bond.api.startDecreasingBrightness(device, callback);
323
+ }
324
+ else {
325
+ promise = bond.api.stop(device, callback);
326
+ }
327
+ promise
328
+ .then(() => {
329
+ this.platform.debug(this.accessory, `Decreased Brightness: ${value}`);
330
+ })
331
+ .catch((error) => {
332
+ this.platform.error(this.accessory, `Error decreasing brightness: ${error}`);
333
+ });
334
+ });
335
+ }
336
+ removeService(serviceName) {
337
+ const service = this.accessory.getService(serviceName);
338
+ if (service) {
339
+ this.accessory.removeService(service);
340
+ this.platform.log(`Removing Service ${serviceName}`);
341
+ }
342
+ }
343
+ }
344
+ exports.CeilingFanAccessory = CeilingFanAccessory;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FireplaceAccessory = void 0;
4
+ const Device_1 = require("../interface/Device");
5
+ const Observer_1 = require("../Observer");
6
+ const Services_1 = require("../Services");
7
+ class FireplaceAccessory {
8
+ constructor(platform, accessory, bond) {
9
+ this.platform = platform;
10
+ this.accessory = accessory;
11
+ this.flameService = new Services_1.FlameService(platform, accessory, accessory.displayName, 'Flame');
12
+ if (platform.config.include_toggle_state) {
13
+ this.toggleStateService = new Services_1.ButtonService(platform, accessory, 'Toggle State', 'ToggleState');
14
+ }
15
+ else {
16
+ this.removeService('Toggle State');
17
+ }
18
+ this.observe(bond);
19
+ }
20
+ updateState(state) {
21
+ this.flameService.updateState(state);
22
+ }
23
+ observe(bond) {
24
+ const device = this.accessory.context.device;
25
+ if (Device_1.Device.FPhasToggle(device)) {
26
+ this.flameService.observe(this.platform, bond, this.accessory);
27
+ }
28
+ else {
29
+ this.platform.error(this.accessory, 'FireplaceAccessory does not have required TogglePower action.');
30
+ }
31
+ this.observeToggle(bond);
32
+ // Set initial state
33
+ bond.api.getState(device.id).then(state => {
34
+ this.updateState(state);
35
+ });
36
+ }
37
+ observeToggle(bond) {
38
+ if (!this.toggleStateService) {
39
+ return;
40
+ }
41
+ const device = this.accessory.context.device;
42
+ Observer_1.Observer.set(this.toggleStateService.on, (_, callback) => {
43
+ bond.api.toggleState(device, 'power', callback)
44
+ .then(() => {
45
+ this.platform.debug(this.accessory, `${device.name} power state toggled`);
46
+ })
47
+ .catch((error) => {
48
+ this.platform.error(this.accessory, `Error toggling power state: ${error}`);
49
+ });
50
+ });
51
+ }
52
+ removeService(serviceName) {
53
+ const service = this.accessory.getService(serviceName);
54
+ if (service) {
55
+ this.accessory.removeService(service);
56
+ }
57
+ }
58
+ }
59
+ exports.FireplaceAccessory = FireplaceAccessory;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GenericAccessory = void 0;
4
+ const Device_1 = require("../interface/Device");
5
+ const Observer_1 = require("../Observer");
6
+ const Services_1 = require("../Services");
7
+ class GenericAccessory {
8
+ constructor(platform, accessory, bond) {
9
+ this.platform = platform;
10
+ this.accessory = accessory;
11
+ this.switchService = new Services_1.SwitchService(platform, accessory, accessory.displayName, 'Power');
12
+ if (platform.config.include_toggle_state) {
13
+ this.toggleStateService = new Services_1.ButtonService(platform, accessory, 'Toggle State', 'ToggleState');
14
+ }
15
+ else {
16
+ this.removeService('Toggle State');
17
+ }
18
+ this.observe(bond);
19
+ this.observeToggle(bond);
20
+ }
21
+ updateState(state) {
22
+ this.switchService.on.updateValue(state.power === 1);
23
+ }
24
+ observe(bond) {
25
+ const device = this.accessory.context.device;
26
+ if (!Device_1.Device.GXhasToggle(device)) {
27
+ this.platform.error(this.accessory, 'GenericAccessory does not have required TogglePower action.');
28
+ return;
29
+ }
30
+ // Set initial state
31
+ bond.api.getState(device.id).then(state => {
32
+ this.updateState(state);
33
+ });
34
+ Observer_1.Observer.set(this.switchService.on, (value, callback) => {
35
+ bond.api.togglePower(device, callback)
36
+ .then(() => {
37
+ this.platform.debug(this.accessory, `Toggled power: ${value}`);
38
+ })
39
+ .catch((error) => {
40
+ this.platform.error(this.accessory, `Error toggling power: ${error}`);
41
+ });
42
+ });
43
+ }
44
+ observeToggle(bond) {
45
+ if (!this.toggleStateService) {
46
+ return;
47
+ }
48
+ const device = this.accessory.context.device;
49
+ Observer_1.Observer.set(this.toggleStateService.on, (_, callback) => {
50
+ bond.api.toggleState(device, 'power', callback)
51
+ .then(() => {
52
+ this.platform.debug(this.accessory, `${device.name} power state toggled`);
53
+ })
54
+ .catch((error) => {
55
+ this.platform.error(this.accessory, `Error toggling power state: ${error}`);
56
+ });
57
+ });
58
+ }
59
+ removeService(serviceName) {
60
+ const service = this.accessory.getService(serviceName);
61
+ if (service) {
62
+ this.accessory.removeService(service);
63
+ }
64
+ }
65
+ }
66
+ exports.GenericAccessory = GenericAccessory;