UNPKG

homebridge-philips-hue-sync-box

Version:
96 lines 4.15 kB
import { PASSTHROUGH, POWER_SAVE } from '../lib/constants.js'; export class SyncBoxDevice { platform; accessory; state; service; constructor(platform, accessory, state) { this.platform = platform; this.accessory = accessory; this.state = state; const existingService = this.getServiceSubType() !== undefined ? this.accessory.getService(this.getServiceType()) : this.accessory.getServiceById(this.getServiceType(), this.getServiceSubType()); this.service = existingService || this.accessory.addService(this.getServiceType(), this.getServiceName(), this.getServiceSubType()); this.service.setCharacteristic(this.platform.api.hap.Characteristic.Name, accessory.displayName); this.service .getCharacteristic(this.getPowerCharacteristic()) .onSet(this.handlePowerCharacteristicSet.bind(this)); // SET - bind to the `setOn` method below const accessoryInformationService = this.accessory.getService(this.platform.api.hap.Service.AccessoryInformation) || this.accessory.addService(this.platform.api.hap.Service.AccessoryInformation); accessoryInformationService .setCharacteristic(this.platform.api.hap.Characteristic.Manufacturer, 'Philips') .setCharacteristic(this.platform.api.hap.Characteristic.Model, 'Sync Box') .setCharacteristic(this.platform.api.hap.Characteristic.FirmwareRevision, this.state.device.firmwareVersion) .setCharacteristic(this.platform.api.hap.Characteristic.SerialNumber, this.state.device.uniqueId + this.getSuffix()); } getServiceSubType() { return undefined; } getServiceName() { return undefined; } updateMode(currentVal, newValue) { this.platform.log.debug('Switch state to ' + newValue); // Ignores changes if the new value equals the old value if (currentVal === newValue) { return; } let mode; // Saves the changes if (newValue) { this.platform.log.debug('Switch state to ON'); mode = this.platform.config.defaultOnMode; if (mode === 'lastSyncMode') { mode = this?.state?.execution?.lastSyncMode || 'video'; } } else { this.platform.log.debug('Switch state to OFF'); mode = this.platform.config.defaultOffMode; } return this.updateExecution({ mode, }); } updateExecution(execution) { this.platform.client.updateExecution(execution).catch(e => { this.platform.log.error('Failed to update execution', e); }); } setBrightness(value) { this.platform.log.debug('Switch brightness to ' + value); this.updateExecution({ brightness: Math.round((value / 100.0) * 200), }); } update(state) { // Updates the on characteristic if (!state) { this.service.updateCharacteristic(this.getPowerCharacteristic(), new this.platform.api.hap.HapStatusError(-70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */)); return; } this.state = state; this.platform.log.debug('Updated state to ' + this.state.execution.mode); this.service.updateCharacteristic(this.getPowerCharacteristic(), this.shouldBeOn()); } shouldBeOn() { return (this.state.execution.mode !== POWER_SAVE && this.state.execution.mode !== PASSTHROUGH); } getSuffix() { return ''; } getPowerCharacteristic() { return this.platform.api.hap.Characteristic.Active; } handlePowerCharacteristicSet(value) { this.platform.log.debug('Set ' + this.getPowerCharacteristic() + ' ->', value); const currentVal = this.service.getCharacteristic(this.getPowerCharacteristic()).value; this.platform.log.debug('Current value of ' + this.getPowerCharacteristic() + ': ' + currentVal); return this.updateMode(currentVal, value); } } //# sourceMappingURL=base.js.map