homebridge-soundtouch-platform
Version:
Homebridge SoundTouch platform
519 lines • 27.4 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SoundTouchAccessoryWrapper = void 0;
var soundtouch_api_1 = require("soundtouch-api");
var sound_touch_device_1 = require("./sound-touch-device");
var sound_touch_speaker_volume_1 = require("./sound-touch-speaker-volume");
var accessory_config_1 = require("./accessory-config");
var sound_touch_lightbulb_volume_1 = require("./sound-touch-lightbulb-volume");
var homebridge_base_platform_1 = require("homebridge-base-platform");
var SoundTouchAccessoryWrapper = /** @class */ (function (_super) {
__extends(SoundTouchAccessoryWrapper, _super);
function SoundTouchAccessoryWrapper(context, accessory, device) {
var _this = _super.call(this, context, accessory, device) || this;
_this.informationService = _this.initInformationService();
_this.onService = _this.initOnService();
_this.volume = _this.initVolumeService();
_this.presetServices = _this.initPresetServices();
_this.sourceServices = _this.initSourceServices();
if (_this.device.verbose === true) {
_this.log("[" + _this.getDisplayName() + "] Device ready");
}
if (_this.device.pollingInterval !== undefined) {
_this._refreshDeviceServices().then(function () { return _this.log('Infinite loop'); });
}
return _this;
}
SoundTouchAccessoryWrapper.prototype.getDisplayName = function () {
return this.device.name;
};
SoundTouchAccessoryWrapper.prototype.initVolumeService = function () {
if (this.device.volumeSettings.mode !== accessory_config_1.VolumeMode.none) {
if (this.device.volumeSettings.mode === accessory_config_1.VolumeMode.lightbulb) {
sound_touch_speaker_volume_1.SoundTouchSpeakerVolume.clearServices(this);
return new sound_touch_lightbulb_volume_1.SoundTouchLightbulbVolume(this.device, this);
}
if (this.device.volumeSettings.mode === accessory_config_1.VolumeMode.speaker) {
sound_touch_lightbulb_volume_1.SoundTouchLightbulbVolume.clearServices(this);
return new sound_touch_speaker_volume_1.SoundTouchSpeakerVolume(this.device, this);
}
}
sound_touch_lightbulb_volume_1.SoundTouchLightbulbVolume.clearServices(this);
sound_touch_speaker_volume_1.SoundTouchSpeakerVolume.clearServices(this);
return undefined;
};
SoundTouchAccessoryWrapper.prototype.initOnService = function () {
var _this = this;
var onService = this.getService(this.Service.Switch, this.getDisplayName(), 'onService');
onService
.getCharacteristic(this.Characteristic.On)
.on('get', homebridge_base_platform_1.callbackify(function () { return __awaiter(_this, void 0, void 0, function () {
var isOn;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, sound_touch_device_1.deviceIsOn(this.device)];
case 1:
isOn = _a.sent();
if (this.device.verbose) {
this.log("[" + this.getDisplayName() + "] " + (isOn ? 'Is on' : 'Is off'));
}
return [2 /*return*/, isOn];
}
});
}); }))
.on('set', homebridge_base_platform_1.callbackify(this.setOn.bind(this)));
return onService;
};
SoundTouchAccessoryWrapper.prototype.initPresetServices = function () {
var _this = this;
var presetServices = [];
var _loop_1 = function (i) {
var presetType = _presetIndexToServiceType(i);
var preset = this_1.device.presets.find(function (p) { return p.index === i; });
if (preset === undefined) {
this_1.removeService(this_1.Service.Switch, presetType);
return "continue";
}
var service = this_1.getService(this_1.Service.Switch, preset.name, presetType);
var characteristic = service.getCharacteristic(this_1.Characteristic.On);
characteristic.on('get', homebridge_base_platform_1.callbackify(function () { return _this.isSelectedPreset(preset.index); }));
characteristic.on('set', homebridge_base_platform_1.callbackify(function (on) { return _this.setPreset(on, preset.index); }));
presetServices.push(service);
};
var this_1 = this;
for (var i = 1; i <= 6; i++) {
_loop_1(i);
}
return presetServices;
};
SoundTouchAccessoryWrapper.prototype.initSourceServices = function () {
var _this = this;
var sourceServices = [];
var _loop_2 = function (src) {
var sourceType = _sourceToServiceType(src.source, src.account);
if (src.enabled === false) {
this_2.removeService(this_2.Service.Switch, sourceType);
return "continue";
}
var service = this_2.getService(this_2.Service.Switch, src.name, sourceType);
var characteristic = service.getCharacteristic(this_2.Characteristic.On);
characteristic.on('get', homebridge_base_platform_1.callbackify(function () { return _this.isSelectedSource(src.source, src.account); }));
characteristic.on('set', homebridge_base_platform_1.callbackify(function (on) { return _this.setSource(on, src.source, src.account); }));
sourceServices.push(service);
};
var this_2 = this;
for (var _i = 0, _a = this.device.sources; _i < _a.length; _i++) {
var src = _a[_i];
_loop_2(src);
}
return sourceServices;
};
SoundTouchAccessoryWrapper.prototype.initInformationService = function () {
var informationService = this.accessory.getService(this.Service.AccessoryInformation);
informationService
.setCharacteristic(this.Characteristic.Name, this.getDisplayName())
.setCharacteristic(this.Characteristic.Manufacturer, 'Bose')
.setCharacteristic(this.Characteristic.Model, this.device.model)
.setCharacteristic(this.Characteristic.SerialNumber, this.device.id);
if (this.device.version) {
informationService.setCharacteristic(this.Characteristic.FirmwareRevision, this.device.version);
}
return informationService;
};
;
SoundTouchAccessoryWrapper.prototype.setOn = function (on, updateOn) {
return __awaiter(this, void 0, void 0, function () {
var nowPlaying, success, selectedSource, selectedSource;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.device.api.getNowPlaying()];
case 1:
nowPlaying = _a.sent();
success = false;
if (!(nowPlaying.source === soundtouch_api_1.SourceStatus.standBy)) return [3 /*break*/, 7];
if (!on) return [3 /*break*/, 6];
return [4 /*yield*/, this.device.api.pressKey(soundtouch_api_1.KeyValue.power)];
case 2:
success = _a.sent();
if (!success) {
return [2 /*return*/, false];
}
return [4 /*yield*/, this.deviceDidTurnOn(updateOn, true)];
case 3:
success = _a.sent();
if (!success) {
return [2 /*return*/, false];
}
return [4 /*yield*/, new Promise((function (resolve) { return setTimeout(resolve, 500); }))];
case 4:
_a.sent();
return [4 /*yield*/, this.getSelectedSource()];
case 5:
selectedSource = _a.sent();
this.switchSelectedSource(on, selectedSource);
_a.label = 6;
case 6: return [3 /*break*/, 11];
case 7:
if (!!on) return [3 /*break*/, 11];
return [4 /*yield*/, this.getSelectedSource()];
case 8:
selectedSource = _a.sent();
return [4 /*yield*/, this.device.api.pressKey(soundtouch_api_1.KeyValue.power)];
case 9:
success = _a.sent();
if (!success) {
return [2 /*return*/, false];
}
return [4 /*yield*/, this.deviceDidTurnOff(updateOn, true)];
case 10:
success = _a.sent();
this.switchSelectedSource(on, selectedSource);
_a.label = 11;
case 11: return [2 /*return*/, success];
}
});
});
};
SoundTouchAccessoryWrapper.prototype.getSelectedSource = function () {
return __awaiter(this, void 0, void 0, function () {
var nowPlaying, presets, _i, presets_1, preset;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.device.api.getNowPlaying()];
case 1:
nowPlaying = _a.sent();
if (nowPlaying.source === soundtouch_api_1.SourceStatus.standBy) {
return [2 /*return*/, undefined];
}
return [4 /*yield*/, this.device.api.getPresets()];
case 2:
presets = _a.sent();
for (_i = 0, presets_1 = presets; _i < presets_1.length; _i++) {
preset = presets_1[_i];
if (JSON.stringify(preset.contentItem) === JSON.stringify(nowPlaying.contentItem)) {
return [2 /*return*/, {
presetIndex: preset.id
}];
}
}
return [2 /*return*/, {
sourceItem: {
source: nowPlaying.source,
sourceAccount: nowPlaying.sourceAccount
}
}];
}
});
});
};
SoundTouchAccessoryWrapper.prototype.isSelectedPreset = function (index) {
return __awaiter(this, void 0, void 0, function () {
var selectedPreset;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getSelectedSource()];
case 1:
selectedPreset = _a.sent();
if (selectedPreset !== undefined) {
if (selectedPreset.presetIndex === index) {
if (this.device.verbose) {
this.log("[" + this.getDisplayName() + "] Current preset n\u00B0" + index);
}
return [2 /*return*/, true];
}
}
return [2 /*return*/, false];
}
});
});
};
SoundTouchAccessoryWrapper.prototype.setPreset = function (on, index) {
return __awaiter(this, void 0, void 0, function () {
var success, isOn, selectedSource;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
success = false;
if (!on) return [3 /*break*/, 6];
return [4 /*yield*/, sound_touch_device_1.deviceIsOn(this.device)];
case 1:
isOn = _a.sent();
if (!!isOn) return [3 /*break*/, 3];
return [4 /*yield*/, this.setOn(on, true)];
case 2:
success = _a.sent();
if (!success) {
return [2 /*return*/, false];
}
_a.label = 3;
case 3: return [4 /*yield*/, this.getSelectedSource()];
case 4:
selectedSource = _a.sent();
return [4 /*yield*/, this.device.api.pressKey(SoundTouchAccessoryWrapper.presetValues[index - 1])];
case 5:
success = _a.sent();
if (!success) {
return [2 /*return*/, false];
}
if (success) {
if (this.device.verbose) {
this.log("[" + this.getDisplayName() + "] Set preset n\u00B0" + index);
}
this.switchSelectedSource(false, selectedSource);
}
return [3 /*break*/, 8];
case 6: return [4 /*yield*/, this.setOn(on, true)];
case 7:
success = _a.sent();
_a.label = 8;
case 8: return [2 /*return*/, success];
}
});
});
};
SoundTouchAccessoryWrapper.prototype.deviceDidTurnOn = function (updateOn, updateVolume) {
return __awaiter(this, void 0, void 0, function () {
var success;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
success = true;
if (this.device.verbose) {
this.log("[" + this.getDisplayName() + "] Turn on");
}
if (updateOn === true) {
this.onService.getCharacteristic(this.Characteristic.On).updateValue(true);
if (this.volume !== undefined) {
this.volume.getMuteCharacteristic().updateValue(true);
}
}
if (!(this.volume !== undefined && updateVolume === true && this.device.volumeSettings.onValue >= 0)) return [3 /*break*/, 2];
return [4 /*yield*/, this.volume.setVolume(this.device.volumeSettings.onValue, true)];
case 1:
success = _a.sent();
_a.label = 2;
case 2: return [2 /*return*/, success];
}
});
});
};
SoundTouchAccessoryWrapper.prototype.deviceDidTurnOff = function (updateOn, updateVolume) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (this.device.verbose) {
this.log("[" + this.getDisplayName() + "] Turn off");
}
if (updateOn === true) {
this.onService.getCharacteristic(this.Characteristic.On).updateValue(false);
}
if (updateVolume === true) {
this.volume.getMuteCharacteristic().updateValue(false);
}
return [2 /*return*/, true];
});
});
};
SoundTouchAccessoryWrapper.prototype.switchSelectedSource = function (on, selectedSource) {
if (selectedSource) {
if (selectedSource.presetIndex !== undefined) {
this.switchPresetService(on, selectedSource.presetIndex);
}
else if (selectedSource.sourceItem !== undefined) {
this.switchSourceService(on, selectedSource.sourceItem.source, selectedSource.sourceItem.sourceAccount);
}
}
};
SoundTouchAccessoryWrapper.prototype.switchService = function (on, type, services) {
for (var _i = 0, services_1 = services; _i < services_1.length; _i++) {
var service = services_1[_i];
if (service.subtype === type) {
service.getCharacteristic(this.Characteristic.On).updateValue(on);
return;
}
}
};
SoundTouchAccessoryWrapper.prototype.switchPresetService = function (on, index) {
this.switchService(on, _presetIndexToServiceType(index), this.presetServices);
};
SoundTouchAccessoryWrapper.prototype.switchSourceService = function (on, source, account) {
this.switchService(on, _sourceToServiceType(source, account), this.sourceServices);
};
SoundTouchAccessoryWrapper.prototype.isSelectedSource = function (source, account) {
return __awaiter(this, void 0, void 0, function () {
var selectedSource, sourceItem;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getSelectedSource()];
case 1:
selectedSource = _a.sent();
if (selectedSource !== undefined) {
sourceItem = selectedSource.sourceItem;
if (sourceItem !== undefined && sourceItem.source === source && sourceItem.sourceAccount === account) {
if (this.device.verbose) {
this.log("[" + this.getDisplayName() + "] Current source: '" + source + "', account: '" + account + "'");
}
return [2 /*return*/, true];
}
}
return [2 /*return*/, false];
}
});
});
};
SoundTouchAccessoryWrapper.prototype.setSource = function (on, source, account) {
return __awaiter(this, void 0, void 0, function () {
var success, isOn, selectedSource;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
success = false;
if (!on) return [3 /*break*/, 6];
return [4 /*yield*/, sound_touch_device_1.deviceIsOn(this.device)];
case 1:
isOn = _a.sent();
if (!!isOn) return [3 /*break*/, 3];
return [4 /*yield*/, this.setOn(on, true)];
case 2:
success = _a.sent();
if (!success) {
return [2 /*return*/, false];
}
_a.label = 3;
case 3: return [4 /*yield*/, this.getSelectedSource()];
case 4:
selectedSource = _a.sent();
return [4 /*yield*/, this.device.api.selectSource({
source: source,
sourceAccount: account
})];
case 5:
success = _a.sent();
if (success) {
if (this.device.verbose) {
this.log("[" + this.getDisplayName() + "] Select source: '" + source + "', account: '" + account + "'");
}
this.switchSelectedSource(false, selectedSource);
}
return [3 /*break*/, 8];
case 6: return [4 /*yield*/, this.setOn(on, true)];
case 7:
success = _a.sent();
_a.label = 8;
case 8: return [2 /*return*/, success];
}
});
});
};
SoundTouchAccessoryWrapper.prototype._refreshDeviceServices = function () {
return __awaiter(this, void 0, void 0, function () {
var selectedSource, onCharacteristic, presetType_1, sourceType_1, deviceVolume;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, new Promise((function (resolve) { return setTimeout(resolve, _this.device.pollingInterval); }))];
case 1:
_a.sent();
return [4 /*yield*/, this.getSelectedSource()];
case 2:
selectedSource = _a.sent();
onCharacteristic = this.onService.getCharacteristic(this.Characteristic.On);
if (!(selectedSource !== undefined)) return [3 /*break*/, 4];
onCharacteristic.updateValue(true);
presetType_1 = selectedSource.presetIndex !== undefined ? _presetIndexToServiceType(selectedSource.presetIndex) : undefined;
this.presetServices.forEach(function (service) {
var presetOnCharacteristic = service.getCharacteristic(_this.Characteristic.On);
presetOnCharacteristic.updateValue(service.subtype === presetType_1);
});
sourceType_1 = selectedSource.sourceItem !== undefined ? _sourceToServiceType(selectedSource.sourceItem.source, selectedSource.sourceItem.sourceAccount) : undefined;
this.sourceServices.forEach(function (service) {
var sourceOnCharacteristic = service.getCharacteristic(_this.Characteristic.On);
sourceOnCharacteristic.updateValue(service.subtype === sourceType_1);
});
return [4 /*yield*/, this.device.api.getVolume()];
case 3:
deviceVolume = _a.sent();
if (this.volume) {
this.volume.getVolumeCharacteristic().updateValue(deviceVolume.actual);
this.volume.getMuteCharacteristic().updateValue(!deviceVolume.isMuted);
}
return [3 /*break*/, 5];
case 4:
onCharacteristic.setValue(false);
this.presetServices.forEach(function (service) { return service.getCharacteristic(_this.Characteristic.On).updateValue(false); });
this.sourceServices.forEach(function (service) { return service.getCharacteristic(_this.Characteristic.On).updateValue(false); });
if (this.volume) {
this.volume.getMuteCharacteristic().updateValue(false);
}
_a.label = 5;
case 5: return [2 /*return*/, this._refreshDeviceServices()];
}
});
});
};
SoundTouchAccessoryWrapper.presetValues = [
soundtouch_api_1.KeyValue.preset1,
soundtouch_api_1.KeyValue.preset2,
soundtouch_api_1.KeyValue.preset3,
soundtouch_api_1.KeyValue.preset4,
soundtouch_api_1.KeyValue.preset5,
soundtouch_api_1.KeyValue.preset6
];
return SoundTouchAccessoryWrapper;
}(homebridge_base_platform_1.HomebridgeAccessoryWrapper));
exports.SoundTouchAccessoryWrapper = SoundTouchAccessoryWrapper;
function _sourceToServiceType(source, account) {
return "" + source.toLowerCase() + (account || '') + "Service";
}
function _presetIndexToServiceType(index) {
return "preset" + index + "Service";
}
//# sourceMappingURL=sound-touch-accessory-wrapper.js.map