homebridge-soundtouch-platform
Version:
Homebridge SoundTouch platform
213 lines • 11.7 kB
JavaScript
;
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.deviceIsOn = exports.deviceFromConfig = exports.searchAllDevices = void 0;
var accessory_config_1 = require("./accessory-config");
var soundtouch_api_1 = require("soundtouch-api");
var errors_1 = require("./errors");
var utils_1 = require("./utils");
var homebridge_base_platform_1 = require("homebridge-base-platform");
function searchAllDevices(globalConfig, accessoryConfigs, log) {
return __awaiter(this, void 0, void 0, function () {
var apis;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, soundtouch_api_1.APIDiscovery.search()];
case 1:
apis = _a.sent();
return [2 /*return*/, Promise.all(apis.map(function (api) { return __awaiter(_this, void 0, void 0, function () {
var info, accessoryConfig;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, api.getInfo()];
case 1:
info = _a.sent();
accessoryConfig = accessoryConfigs.find(function (ac) { return ac.room === info.name || ac.ip === api.host; });
return [2 /*return*/, _deviceFromApi(api, info, globalConfig, accessoryConfig || {}, log)];
}
});
}); }))];
}
});
});
}
exports.searchAllDevices = searchAllDevices;
function deviceFromConfig(globalConfig, accessoryConfig, log) {
return __awaiter(this, void 0, void 0, function () {
var api, _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!accessoryConfig.ip) return [3 /*break*/, 1];
api = new soundtouch_api_1.API(accessoryConfig.ip, accessoryConfig.port);
return [3 /*break*/, 3];
case 1:
if (!accessoryConfig.room) return [3 /*break*/, 3];
return [4 /*yield*/, soundtouch_api_1.APIDiscovery.find(accessoryConfig.room)];
case 2:
api = _c.sent();
if (!api) {
throw errors_1.apiNotFoundWithName(accessoryConfig.name);
}
_c.label = 3;
case 3:
_a = _deviceFromApi;
_b = [api];
return [4 /*yield*/, api.getInfo()];
case 4: return [2 /*return*/, _a.apply(void 0, _b.concat([_c.sent(), globalConfig, accessoryConfig, log]))];
}
});
});
}
exports.deviceFromConfig = deviceFromConfig;
function _deviceFromApi(api, info, globalConfig, accessoryConfig, log) {
return __awaiter(this, void 0, void 0, function () {
var displayName, isVerbose, pollingInterval, component, presets, sources, globalVolume, accessoryVolume, onValue;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
displayName = accessoryConfig.name || info.name;
isVerbose = homebridge_base_platform_1.isVerboseInConfigs(globalConfig, accessoryConfig);
pollingInterval = accessoryConfig.pollingInterval || globalConfig.pollingInterval;
if (isVerbose) {
log("[" + displayName + "] Found device");
}
component = info.components.find(function (c) { return c.serialNumber.toLowerCase() === info.deviceId.toLowerCase(); });
return [4 /*yield*/, _availablePresets(api, displayName, accessoryConfig.presets, globalConfig.presets, isVerbose ? log : undefined)];
case 1:
presets = _a.sent();
return [4 /*yield*/, _availableSources(api, displayName, accessoryConfig.sources, globalConfig.sources, isVerbose ? log : undefined)];
case 2:
sources = _a.sent();
globalVolume = globalConfig.volume || {};
accessoryVolume = accessoryConfig.volume || {};
onValue = globalVolume.onValue || accessoryVolume.onValue;
return [2 /*return*/, {
api: api,
name: displayName,
id: info.deviceId,
model: info.type,
version: component ? component.softwareVersion : undefined,
verbose: isVerbose,
pollingInterval: pollingInterval,
volumeSettings: {
onValue: onValue || -1,
maxValue: globalVolume.maxValue || accessoryVolume.maxValue || 100,
unmuteValue: globalVolume.unmuteValue || accessoryVolume.unmuteValue || onValue || 35,
mode: globalVolume.mode || accessoryVolume.mode || accessory_config_1.VolumeMode.lightbulb
},
presets: presets,
sources: sources
}];
}
});
});
}
function deviceIsOn(device) {
return __awaiter(this, void 0, void 0, function () {
var nowPlaying;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, device.api.getNowPlaying()];
case 1:
nowPlaying = _a.sent();
return [2 /*return*/, nowPlaying.source !== soundtouch_api_1.SourceStatus.standBy];
}
});
});
}
exports.deviceIsOn = deviceIsOn;
function _availablePresets(api, deviceName, accessoryPresets, globalPresets, log) {
return __awaiter(this, void 0, void 0, function () {
var presets;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, api.getPresets()];
case 1:
presets = (_a.sent()) || [];
return [2 /*return*/, soundtouch_api_1.compactMap(presets, function (preset) {
var presetConfig = _findConfig(function (p) { return p.index === preset.id; }, accessoryPresets, globalPresets) || { index: preset.id };
if (log !== undefined) {
log("[" + deviceName + "] Found preset n\u00B0" + preset.id + " '" + preset.contentItem.itemName + "' on device");
}
if (presetConfig.enabled === false) {
return undefined;
}
return {
name: presetConfig.name || preset.contentItem.itemName,
index: preset.id
};
})];
}
});
});
}
function _availableSources(api, deviceName, accessorySources, globalSources, log) {
return __awaiter(this, void 0, void 0, function () {
var sources, localSources;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, api.getSources()];
case 1:
sources = _a.sent();
localSources = sources.items.filter(function (src) { return src.isLocal; });
return [2 /*return*/, localSources.map(function (ls) {
if (log !== undefined) {
log("[" + deviceName + "] Found local source '" + ls.source + "' with account '" + (ls.sourceAccount || '') + "' on device");
}
var sourceConfig = _findConfig(function (p) { return p.source === ls.source && (p.account !== undefined ? p.account === ls.sourceAccount : true); }, accessorySources, globalSources) || { source: ls.source };
return {
name: sourceConfig.name || deviceName + " " + (ls.name ? ls.name : utils_1.stringUpperCaseFirst(sourceConfig.source)),
source: sourceConfig.source,
account: ls.sourceAccount,
enabled: sourceConfig.enabled !== false
};
})];
}
});
});
}
function _findConfig(predicate, accessoryConfigs, globalConfigs) {
var config = accessoryConfigs ? accessoryConfigs.find(predicate) : undefined;
if (config !== undefined) {
return config;
}
return globalConfigs ? globalConfigs.find(predicate) : undefined;
}
//# sourceMappingURL=sound-touch-device.js.map