homebridge-appletv-enhanced
Version:
Plugin that exposes the Apple TV to HomeKit with much richer features than the vanilla Apple TV implementation of HomeKit.
81 lines • 3.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = tvOS18InputBugSolver;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const PrefixLogger_1 = __importDefault(require("./PrefixLogger"));
function tvOS18InputBugSolver(log, storagePath, mac) {
log = new PrefixLogger_1.default(log, 'tvOS 18 Input Bug Solver');
const dir = path_1.default.join(storagePath, 'appletv-enhanced', mac.replaceAll(':', ''));
const appConfigFilePath = path_1.default.join(dir, 'apps.json');
// load app configurations
if (!fs_1.default.existsSync(appConfigFilePath)) {
log.debug('No apps.json exists until now. Exiting ...');
return;
}
const appConfigs = JSON.parse(fs_1.default.readFileSync(appConfigFilePath, 'utf8'));
const appNames = [];
for (const appId in appConfigs) {
appNames.push(appConfigs[appId].configuredName);
}
// get the number of apps
const numOfApps = appNames.length;
if (numOfApps === 0) {
log.debug('apps.json is empty. Exiting ...');
return;
}
// cut off 42 from app names, e.g. "Input Source 42"
const trimmedAppNames = appNames.map((appName) => appName.replace(/\s\d+$/, '')).sort();
// count the occurrence of each app name, e.g. 10x Input Source, 1x Disney, ...
const countedNames = trimmedAppNames.reduce((acc, e) => acc.set(e, (acc.get(e) ?? 0) + 1), new Map());
// get the app name that was used most often
log.debug('Counting of same app names:');
let highestCount = 0;
let highestCountAppName = '';
for (const appName of countedNames.keys()) {
log.debug(`${appName}: ${countedNames.get(appName)}`);
if (countedNames.get(appName) > highestCount) {
highestCount = countedNames.get(appName);
highestCountAppName = appName;
}
}
const percentageWithSameName = Math.round(highestCount / numOfApps * 100);
if (percentageWithSameName < 30) {
log.debug('Most inputs have no similar app name. Exiting ...');
return;
}
log.warn(`${percentageWithSameName}% of apps have a configured name that starts with "${highestCountAppName}". This is likely caused \
by a bug in tvOS 18. Please refer to the following GitHub issues:`);
log.warn('- https://github.com/homebridge/homebridge/issues/3703');
log.warn('- https://github.com/maxileith/homebridge-appletv-enhanced/issues/627');
log.warn('To resolve this issue the configuration of the Apple TV will now be reset automatically. This issue only occurs during the \
pairing process.');
const commonConfigFilePath = path_1.default.join(dir, 'common.json');
const deviceStatesConfigFilePath = path_1.default.join(dir, 'deviceStates.json');
const mediaTypesConfigFilePath = path_1.default.join(dir, 'mediaTypes.json');
const remoteKeySwitchesConfigFilePath = path_1.default.join(dir, 'remoteKeySwitches.json');
unlink(log, appConfigFilePath);
unlink(log, commonConfigFilePath);
unlink(log, deviceStatesConfigFilePath);
unlink(log, mediaTypesConfigFilePath);
unlink(log, remoteKeySwitchesConfigFilePath);
log.success('The configuration of the Apple TV has successfully been reset.');
}
function unlink(log, path) {
try {
fs_1.default.unlinkSync(path);
log.debug(`${path} has been deleted.`);
}
catch (e) {
if (e instanceof Error && e.message.startsWith('ENOENT')) {
log.debug(`${path} has not been deleted since it is not existing.`);
}
else {
throw e;
}
}
}
//# sourceMappingURL=tvOS18InputBugSolver.js.map