homebridge-kodi
Version:
Kodi plugin for Homebridge
682 lines (681 loc) • 60.8 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KodiPlatform = void 0;
const internal_1 = require("./internal");
const kodi = require("./lib/kodi");
const utils = require("./util/utils");
const characteristics_1 = __importDefault(require("./lib/characteristics"));
const WebSockets = require("rpc-websockets");
const WebSocket = WebSockets.Client;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const setIntervalPlus = require("setinterval-plus");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJSON = require('../package.json');
class KodiPlatform {
constructor(log, config, api) {
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.accessories = [];
this.shownAccessories = [];
this.subscribed = false;
this.closedByPlugin = false;
this.checkKodiStatus = async function () {
kodi.getStatus(this.config)
.then(status => {
var _a, _b, _c, _d, _e, _f, _g;
if (status) {
if (!this.closedByPlugin) {
this.log.debug('Kodi is up and running - Check again in ' + (this.config.retrytime || 30) + ' seconds');
(_a = this.powerSwitchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.api.hap.Characteristic.On).updateValue(true);
(_b = this.televisionControlsService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(true);
}
else {
this.log.warn('Kodi was closed by the plugin, but is still running. ' +
'If Kodi is still running please check your power off command. If not check if the Kodi process is unresponsive and/or still in the memory.');
this.intervalKodiSubscriptions.stop();
this.resetAllCharacteristics(true);
}
}
else {
this.log.debug('Kodi is not running! - Retry in ' + (this.config.retrytime || 30) + ' seconds');
(_c = this.powerSwitchService) === null || _c === void 0 ? void 0 : _c.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_d = this.televisionControlsService) === null || _d === void 0 ? void 0 : _d.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(false);
(_e = this.televisionChannelsService) === null || _e === void 0 ? void 0 : _e.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(false);
(_f = this.applicationVolumeLightbulbService) === null || _f === void 0 ? void 0 : _f.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_g = this.applicationVolumeLightbulbService) === null || _g === void 0 ? void 0 : _g.getCharacteristic(this.api.hap.Characteristic.Brightness).updateValue(0);
this.intervalKodiSubscriptions.start();
this.resetAllCharacteristics(true);
this.closedByPlugin = false;
}
})
.catch(error => {
this.log.error('Kodi Check Status: ' + error.message);
});
};
this.subscribeToKodiNotifications = async function (api) {
if (!this.subscribed) {
kodi.getStatus(this.config)
.then(status => {
if (status) {
const ws = new WebSocket('ws://' + (this.config.host || 'localhost') + ':9090/jsonrpc');
ws.on('open', () => {
const subscriptions = [
'System.OnSleep',
'System.OnWake',
'System.OnQuit',
'System.OnRestart',
'Application.OnVolumeChanged',
'Player.OnPlay',
'Player.OnResume',
'Player.OnPause',
'Player.OnStop',
'Player.OnSeek',
'Player.OnSpeedChanged',
'VideoLibrary.OnScanStarted',
'VideoLibrary.OnScanFinished',
'VideoLibrary.OnCleanStarted',
'VideoLibrary.OnCleanFinished',
'AudioLibrary.OnScanStarted',
'AudioLibrary.OnScanFinished',
'AudioLibrary.OnCleanStarted',
'AudioLibrary.OnCleanFinished',
];
// Always throws 'Method not found' warning after successfully subscribing?!
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ws.subscribe(subscriptions).catch(_ => {
this.onSubscribe();
});
// System.OnSleep
ws.on('System.OnSleep', () => {
this.log.debug('Notification Received: System.OnSleep');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ws.unsubscribe(subscriptions).catch(_ => {
this.onUnsubscribe();
});
this.resetAllCharacteristics(true);
});
// System.OnWake
ws.on('System.OnWake', () => {
this.log.debug('Notification Received: System.OnWake');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ws.subscribe(subscriptions).catch(_ => {
this.onSubscribe();
});
});
// System.OnQuit
ws.on('System.OnQuit', () => {
this.log.debug('Notification Received: System.OnQuit');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ws.unsubscribe(subscriptions).catch(_ => {
this.onUnsubscribe();
});
this.resetAllCharacteristics(true);
});
// System.OnRestart
ws.on('System.OnRestart', () => {
this.log.debug('Notification Received: System.OnRestart');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ws.unsubscribe(subscriptions).catch(_ => {
this.onUnsubscribe();
});
this.resetAllCharacteristics(true);
});
// Player.OnVolumeChanged
ws.on('Application.OnVolumeChanged', () => {
this.log.debug('Notification Received: Application.OnVolumeChanged');
this.updateApplicationVolumeService();
});
// Player.OnPlay
ws.on('Player.OnPlay', () => {
var _a, _b, _c;
this.log.debug('Notification Received: Player.OnPlay');
(_a = this.playerLightbulbService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
(_b = this.playerPlaySwitchService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
(_c = this.playerPauseSwitchService) === null || _c === void 0 ? void 0 : _c.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
this.updateTelevisionChannelsService();
this.updateKodiPlayer(true);
this.intervalKodiUpdate.start();
});
// Player.OnResume
ws.on('Player.OnResume', () => {
var _a, _b, _c;
this.log.debug('Notification Received: Player.OnResume');
(_a = this.playerLightbulbService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
(_b = this.playerPlaySwitchService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
(_c = this.playerPauseSwitchService) === null || _c === void 0 ? void 0 : _c.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
this.updateTelevisionChannelsService();
this.updateKodiPlayer(false);
this.intervalKodiUpdate.start();
});
// Player.OnPause
ws.on('Player.OnPause', () => {
var _a, _b, _c;
this.log.debug('Notification Received: Player.OnPause');
(_a = this.playerLightbulbService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
(_b = this.playerPlaySwitchService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
(_c = this.playerPauseSwitchService) === null || _c === void 0 ? void 0 : _c.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
this.updateTelevisionChannelsService();
this.intervalKodiUpdate.stop();
});
// Player.OnStop
ws.on('Player.OnStop', () => {
var _a, _b, _c, _d;
this.log.debug('Notification Received: Player.OnStop');
(_a = this.televisionChannelsService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.Active).updateValue(false);
(_b = this.playerLightbulbService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
(_c = this.playerPlaySwitchService) === null || _c === void 0 ? void 0 : _c.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
(_d = this.playerPauseSwitchService) === null || _d === void 0 ? void 0 : _d.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
this.intervalKodiUpdate.stop();
this.resetAllCharacteristics(false);
});
// Player.OnSeek
ws.on('Player.OnSeek', () => {
this.log.debug('Notification Received: Player.OnSeek');
kodi.playerGetActivePlayers(this.config)
.then(playerid => {
if (playerid !== null && playerid !== -1) {
kodi.playerGetProperties(this.config, playerid, ['percentage'])
.then(result => {
var _a;
if (result && result.percentage) {
(_a = this.playerLightbulbService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.Brightness).updateValue(Math.round(result.percentage));
}
})
.catch(error => {
this.log.error('Error getting player properties: ' + error.message);
});
}
else {
this.log.error('Error getting active players');
}
})
.catch(error => {
this.log.error('Error getting active players: ' + error.message);
});
});
// Player.OnSpeedChanged
ws.on('Player.OnSpeedChanged', () => {
this.log.debug('Notification Received: Application.OnSpeedChanged');
kodi.isPlaying(this.config)
.then(([playing, paused]) => {
var _a, _b, _c, _d, _e, _f;
if (playing) {
this.intervalKodiUpdate.start();
(_a = this.playerLightbulbService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
(_b = this.playerPlaySwitchService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
(_c = this.playerPauseSwitchService) === null || _c === void 0 ? void 0 : _c.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
}
else if (paused) {
this.intervalKodiUpdate.stop();
(_d = this.playerLightbulbService) === null || _d === void 0 ? void 0 : _d.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
(_e = this.playerPlaySwitchService) === null || _e === void 0 ? void 0 : _e.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
(_f = this.playerPauseSwitchService) === null || _f === void 0 ? void 0 : _f.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
}
else {
this.intervalKodiUpdate.stop();
this.resetAllCharacteristics(false);
}
})
.catch(error => {
this.log.error('Error getting playing status: ' + error.message);
});
});
// VideoLibrary.OnScanStarted
ws.on('VideoLibrary.OnScanStarted', () => {
var _a;
this.log.debug('Notification Received: VideoLibrary.OnScanStarted');
kodi.storageSetItem(api.user.persistPath(), (_a = this.videoLibraryScanSwitchService) === null || _a === void 0 ? void 0 : _a.name, 'true')
.then(() => {
var _a;
(_a = this.videoLibraryScanSwitchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
})
.catch(error => {
this.log.error('Error on storing Item: ' + error.message);
});
});
// VideoLibrary.OnScanFinished
ws.on('VideoLibrary.OnScanFinished', () => {
var _a;
this.log.debug('Notification Received: VideoLibrary.OnScanFinished');
kodi.storageSetItem(api.user.persistPath(), (_a = this.videoLibraryScanSwitchService) === null || _a === void 0 ? void 0 : _a.name, 'false')
.then(() => {
var _a;
(_a = this.videoLibraryScanSwitchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
})
.catch(error => {
this.log.error('Error on storing Item: ' + error.message);
});
});
// VideoLibrary.OnCleanStarted
ws.on('VideoLibrary.OnCleanStarted', () => {
var _a;
this.log.debug('Notification Received: VideoLibrary.OnCleanStarted');
kodi.storageSetItem(api.user.persistPath(), (_a = this.videoLibraryCleanSwitchService) === null || _a === void 0 ? void 0 : _a.name, 'true')
.then(() => {
var _a;
(_a = this.videoLibraryCleanSwitchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
})
.catch(error => {
this.log.error('Error on storing Item: ' + error.message);
});
});
// VideoLibrary.OnCleanFinished
ws.on('VideoLibrary.OnCleanFinished', () => {
var _a;
this.log.debug('Notification Received: VideoLibrary.OnCleanFinished');
kodi.storageSetItem(api.user.persistPath(), (_a = this.videoLibraryCleanSwitchService) === null || _a === void 0 ? void 0 : _a.name, 'false')
.then(() => {
var _a;
(_a = this.videoLibraryCleanSwitchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
})
.catch(error => {
this.log.error('Error on storing Item: ' + error.message);
});
});
// AudioLibrary.OnScanStarted
ws.on('AudioLibrary.OnScanStarted', () => {
var _a;
this.log.debug('Notification Received: AudioLibrary.OnScanStarted');
kodi.storageSetItem(api.user.persistPath(), (_a = this.audioLibraryScanSwitchService) === null || _a === void 0 ? void 0 : _a.name, 'true')
.then(() => {
var _a;
(_a = this.audioLibraryScanSwitchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
})
.catch(error => {
this.log.error('Error on storing Item: ' + error.message);
});
});
// AudioLibrary.OnScanFinished
ws.on('AudioLibrary.OnScanFinished', () => {
var _a;
this.log.debug('Notification Received: AudioLibrary.OnScanFinished');
kodi.storageSetItem(api.user.persistPath(), (_a = this.audioLibraryScanSwitchService) === null || _a === void 0 ? void 0 : _a.name, 'false')
.then(() => {
var _a;
(_a = this.audioLibraryScanSwitchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
})
.catch(error => {
this.log.error('Error on storing Item: ' + error.message);
});
});
// AudioLibrary.OnCleanStarted
ws.on('AudioLibrary.OnCleanStarted', () => {
var _a;
this.log.debug('Notification Received: AudioLibrary.OnCleanStarted');
kodi.storageSetItem(api.user.persistPath(), (_a = this.audioLibraryCleanSwitchService) === null || _a === void 0 ? void 0 : _a.name, 'true')
.then(() => {
var _a;
(_a = this.audioLibraryCleanSwitchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(true);
})
.catch(error => {
this.log.error('Error on storing Item: ' + error.message);
});
});
// AudioLibrary.OnCleanFinished
ws.on('AudioLibrary.OnCleanFinished', () => {
var _a;
this.log.debug('Notification Received: AudioLibrary.OnCleanFinished');
kodi.storageSetItem(api.user.persistPath(), (_a = this.audioLibraryCleanSwitchService) === null || _a === void 0 ? void 0 : _a.name, 'false')
.then(() => {
var _a;
(_a = this.audioLibraryCleanSwitchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(api.hap.Characteristic.On).updateValue(false);
})
.catch(error => {
this.log.error('Error on storing Item: ' + error.message);
});
});
});
}
else {
this.log.debug('Kodi Notifications: Kodi does not seem to be running - Retry in ' + (this.config.retrytime || 30) + ' seconds');
}
})
.catch(error => {
this.log.error('Kodi Notification: ' + error.message);
});
}
else {
this.log.debug('Kodi Notification: Already subscribed.');
}
};
this.onSubscribe = function () {
var _a;
(_a = this.televisionControlsService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(true);
this.updateApplicationVolumeService();
this.updateTelevisionChannelsService();
this.intervalKodiSubscriptions.stop();
this.log.info('Kodi Notifications: Subscribed successfully');
this.subscribed = true;
};
this.onUnsubscribe = function () {
var _a, _b, _c, _d;
(_a = this.televisionControlsService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(false);
(_b = this.televisionChannelsService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(false);
(_c = this.applicationVolumeLightbulbService) === null || _c === void 0 ? void 0 : _c.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_d = this.applicationVolumeLightbulbService) === null || _d === void 0 ? void 0 : _d.getCharacteristic(this.api.hap.Characteristic.Brightness).updateValue(0);
this.intervalKodiSubscriptions.start();
this.log.info('Kodi Notifications: Unsubscribed successfully');
this.subscribed = false;
};
this.updateKodiPlayer = async function (onplay) {
kodi.playerGetActivePlayers(this.config)
.then(playerid => {
if (playerid !== null) {
if (playerid !== -1) {
kodi.playerGetItem(this.config, playerid, ['artist', 'album', 'showtitle', 'season', 'episode', 'duration'])
.then(itemresult => {
var _a, _b, _c, _d, _e, _f;
if (itemresult && itemresult.item) {
const artist = typeof itemresult.item.artist !== 'undefined' && itemresult.item.artist.length !== 0 ?
itemresult.item.artist.join(', ').substring(0, 64) :
'-';
const album = typeof itemresult.item.album !== 'undefined' && itemresult.item.album !== '' ?
itemresult.item.album.substring(0, 64) :
'-';
const itemtype = typeof itemresult.item.type !== 'undefined' && itemresult.item.type !== '' ?
itemresult.item.type.substring(0, 64) :
'-';
const title = typeof itemresult.item.label !== 'undefined' && itemresult.item.label !== '' ?
itemresult.item.label.substring(0, 64) :
'-';
const showtitle = typeof itemresult.item.showtitle !== 'undefined' && itemresult.item.showtitle !== '' ?
itemresult.item.showtitle.substring(0, 64) :
'-';
let seasonEpisode = '-';
if (itemtype === 'episode') {
if ((itemresult.item.season !== -1 && itemresult.item.episode !== -1) ||
(typeof itemresult.item.season !== 'undefined' && typeof itemresult.item.episode !== 'undefined')) {
seasonEpisode = 'S' + itemresult.item.season.toString().padStart(2, '0') +
'E' + itemresult.item.episode.toString().padStart(2, '0');
}
else if ((itemresult.item.season === -1 && itemresult.item.episode !== -1) ||
(typeof itemresult.item.season === 'undefined' && typeof itemresult.item.episode !== 'undefined')) {
seasonEpisode = 'E' + itemresult.item.episode.toString().padStart(2, '0');
}
}
(_a = this.playerLightbulbService) === null || _a === void 0 ? void 0 : _a.updateCharacteristic(this.customCharacteristics.Type, itemtype);
(_b = this.playerLightbulbService) === null || _b === void 0 ? void 0 : _b.updateCharacteristic(this.customCharacteristics.Title, title);
(_c = this.playerLightbulbService) === null || _c === void 0 ? void 0 : _c.updateCharacteristic(this.customCharacteristics.ShowTitle, showtitle);
(_d = this.playerLightbulbService) === null || _d === void 0 ? void 0 : _d.updateCharacteristic(this.customCharacteristics.SeasonEpisode, seasonEpisode);
(_e = this.playerLightbulbService) === null || _e === void 0 ? void 0 : _e.updateCharacteristic(this.customCharacteristics.Artist, artist);
(_f = this.playerLightbulbService) === null || _f === void 0 ? void 0 : _f.updateCharacteristic(this.customCharacteristics.Album, album);
kodi.playerGetProperties(this.config, playerid, ['speed', 'percentage', 'time', 'totaltime'])
.then(result => {
var _a, _b, _c, _d, _e, _f, _g, _h;
if (result) {
const speed = result.speed !== 0 ? result.speed !== 0 : 0;
let percentage = Math.round(result.percentage ? result.percentage : 0);
let timeAndTotaltime = result.time.hours + ':' +
result.time.minutes.toString().padStart(2, '0') + ':' +
result.time.seconds.toString().padStart(2, '0') + ' / ' +
result.totaltime.hours + ':' +
result.totaltime.minutes.toString().padStart(2, '0') + ':' +
result.totaltime.seconds.toString().padStart(2, '0');
if (timeAndTotaltime === '0:00:00 / 0:00:00') {
timeAndTotaltime = '-';
}
if (percentage === 0 && timeAndTotaltime === '-') {
percentage = 100;
}
(_a = this.playerLightbulbService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.api.hap.Characteristic.On).updateValue(speed);
(_b = this.playerLightbulbService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(this.api.hap.Characteristic.Brightness).updateValue(Math.round(percentage));
(_c = this.playerPlaySwitchService) === null || _c === void 0 ? void 0 : _c.getCharacteristic(this.api.hap.Characteristic.On).updateValue(speed);
(_d = this.playerPauseSwitchService) === null || _d === void 0 ? void 0 : _d.getCharacteristic(this.api.hap.Characteristic.On).updateValue(!speed);
(_e = this.playerLightbulbService) === null || _e === void 0 ? void 0 : _e.setCharacteristic(this.customCharacteristics.Position, timeAndTotaltime);
let activeIdentifier = -1;
const tvChannelsChannelsConfig = this.config.television && this.config.television.tv && this.config.television.tv.channels || [];
const logprefix = 'Setting Info (' + itemtype + '): ';
let logmessage = '';
switch (itemtype) {
case 'movie':
logmessage = '"' + title + '" - ' + timeAndTotaltime + ' (' + percentage + ' %)';
break;
case 'episode':
logmessage = showtitle + ' ' + seasonEpisode + ' "' + title + '" - ' + timeAndTotaltime + ' (' + percentage + ' %)';
break;
case 'song':
logmessage = artist + ' "' + title + '" (' + album + ') - ' + timeAndTotaltime + ' (' + percentage + ' %)';
break;
case 'unknown':
logmessage = '"' + title + '" - ' + timeAndTotaltime + ' (' + percentage + ' %)';
break;
case 'channel':
(_f = this.televisionChannelsService) === null || _f === void 0 ? void 0 : _f.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(true);
for (let index = 0; index < tvChannelsChannelsConfig.length; index++) {
if (title === tvChannelsChannelsConfig[index]) {
activeIdentifier = index + 1;
}
}
if (activeIdentifier && activeIdentifier !== -1) {
(_g = this.televisionChannelsService) === null || _g === void 0 ? void 0 : _g.getCharacteristic(this.api.hap.Characteristic.ActiveIdentifier).updateValue(activeIdentifier);
}
else {
(_h = this.televisionChannelsService) === null || _h === void 0 ? void 0 : _h.getCharacteristic(this.api.hap.Characteristic.ActiveIdentifier).updateValue(1);
}
logmessage = '"' + title + '" (' + activeIdentifier + ')';
break;
default:
logmessage = '"' + title + '"';
}
this.log.debug(logprefix + logmessage);
}
else {
this.log.debug('Getting player properties: no result');
}
})
.catch(error => {
this.log.error('Error getting player properties: ' + error.message);
});
}
else if (!onplay) {
this.intervalKodiUpdate.stop();
}
else {
this.log.debug('Getting player item: no result');
}
})
.catch(error => {
this.log.error('Error getting player item: ' + error.message);
});
}
else if (!onplay) {
this.intervalKodiUpdate.stop();
}
}
else {
this.log.debug('Getting active players: no result');
}
})
.catch(error => {
this.log.error('Error getting active players: ' + error.message);
});
};
this.resetAllCharacteristics = function (completely) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
this.log.debug('Reset All Characteristics');
if (completely) {
(_a = this.televisionControlsService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(false);
(_b = this.applicationVolumeLightbulbService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_c = this.applicationVolumeLightbulbService) === null || _c === void 0 ? void 0 : _c.getCharacteristic(this.api.hap.Characteristic.Brightness).updateValue(0);
}
(_d = this.televisionChannelsService) === null || _d === void 0 ? void 0 : _d.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(false);
(_e = this.playerLightbulbService) === null || _e === void 0 ? void 0 : _e.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_f = this.playerLightbulbService) === null || _f === void 0 ? void 0 : _f.getCharacteristic(this.api.hap.Characteristic.Brightness).updateValue(0);
(_g = this.playerLightbulbService) === null || _g === void 0 ? void 0 : _g.setCharacteristic(this.customCharacteristics.Type, '-');
(_h = this.playerLightbulbService) === null || _h === void 0 ? void 0 : _h.setCharacteristic(this.customCharacteristics.Title, '-');
(_j = this.playerLightbulbService) === null || _j === void 0 ? void 0 : _j.setCharacteristic(this.customCharacteristics.ShowTitle, '-');
(_k = this.playerLightbulbService) === null || _k === void 0 ? void 0 : _k.setCharacteristic(this.customCharacteristics.SeasonEpisode, '-');
(_l = this.playerLightbulbService) === null || _l === void 0 ? void 0 : _l.setCharacteristic(this.customCharacteristics.Artist, '-');
(_m = this.playerLightbulbService) === null || _m === void 0 ? void 0 : _m.setCharacteristic(this.customCharacteristics.Album, '-');
(_o = this.playerLightbulbService) === null || _o === void 0 ? void 0 : _o.setCharacteristic(this.customCharacteristics.Position, '-');
(_p = this.playerPlaySwitchService) === null || _p === void 0 ? void 0 : _p.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_q = this.playerPauseSwitchService) === null || _q === void 0 ? void 0 : _q.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_r = this.playerStopSwitchService) === null || _r === void 0 ? void 0 : _r.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_s = this.videoLibraryScanSwitchService) === null || _s === void 0 ? void 0 : _s.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_t = this.videoLibraryCleanSwitchService) === null || _t === void 0 ? void 0 : _t.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_u = this.audioLibraryScanSwitchService) === null || _u === void 0 ? void 0 : _u.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
(_v = this.audioLibraryCleanSwitchService) === null || _v === void 0 ? void 0 : _v.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false);
};
this.updateApplicationVolumeService = function () {
kodi.applicationGetProperties(this.config, ['volume', 'muted'])
.then(result => {
var _a, _b;
if (result) {
const muted = result.muted ? result.muted : false;
const volume = result.volume ? result.volume : 0;
(_a = this.applicationVolumeLightbulbService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.api.hap.Characteristic.On).updateValue(!muted && volume !== 0);
(_b = this.applicationVolumeLightbulbService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(this.api.hap.Characteristic.Brightness).updateValue(volume);
}
else {
this.log.error('Error getting application properties: no result');
}
})
.catch(error => {
this.log.error('Error getting application properties: ' + error.message);
});
};
this.updateTelevisionChannelsService = function () {
kodi.tvIsPlaying(this.config)
.then(tvisplaying => {
var _a;
(_a = this.televisionChannelsService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.api.hap.Characteristic.Active).updateValue(tvisplaying);
})
.catch(error => {
this.log.error('Error getting tv playing status: ' + error.message);
});
};
this.log = new internal_1.KodiLogger(log, this.config.debug);
this.log.debug('Finished initializing platform:', this.config.name);
this.customCharacteristics = (0, characteristics_1.default)(this.api.hap.Characteristic);
this.api.on('didFinishLaunching', () => {
this.log.debug('Executed didFinishLaunching callback');
this.discoverDevices();
});
}
configureAccessory(accessory) {
this.log.debug('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
}
discoverDevices() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
this.log.info('Init Homebridge-Kodi');
const platformname = utils.checkStringConfig(this.config.name, 'Kodi');
const polling = utils.checkNumberConfig(this.config.polling, 10);
const retrytime = utils.checkNumberConfig(this.config.retrytime, 30);
const powerSwitchConfig = this.config.power && utils.checkBooleanConfig(this.config.power.switch, true);
const tvControlConfig = this.config.television && utils.checkConfig(this.config.television.controls, false);
const tvControlMenuItemsConfig = tvControlConfig && utils.checkStringArrayConfig(this.config.television.controls.menuitems, []);
const tvChannelsConfig = this.config.television && utils.checkConfig(this.config.television.tv, false);
const tvChannelsChannelsConfig = tvChannelsConfig && utils.checkStringArrayConfig(this.config.television.tv.channels, []);
const playerMainConfig = this.config.player && utils.checkBooleanConfig(this.config.player.main, true);
const playerPlayConfig = this.config.player && utils.checkBooleanConfig(this.config.player.play, false);
const playerPauseConfig = this.config.player && utils.checkBooleanConfig(this.config.player.pause, false);
const playerStopConfig = this.config.player && utils.checkBooleanConfig(this.config.player.stop, false);
const applicationVolumeConfig = this.config.application && utils.checkBooleanConfig(this.config.application.volume, false);
const videoLibraryScanConfig = this.config.videolibrary && utils.checkBooleanConfig(this.config.videolibrary.scan, false);
const videoLibraryCleanConfig = this.config.videolibrary && utils.checkBooleanConfig(this.config.videolibrary.clean, false);
const audioLibraryScanConfig = this.config.audiolibrary && utils.checkBooleanConfig(this.config.audiolibrary.scan, false);
const audioLibraryCleanConfig = this.config.audiolibrary && utils.checkBooleanConfig(this.config.audiolibrary.clean, false);
const commandsConfig = utils.checkArrayConfig(this.config.commands, []);
this.powerSwitchService = (_a = this.addAccessory(platformname + ' Power', powerSwitchConfig, internal_1.PowerSwitchAccessory)) === null || _a === void 0 ? void 0 : _a.getService(this.Service.Switch);
let name = platformname + ' Controls';
if (tvControlConfig) {
this.log.info('Adding ' + name);
const inputNames = [];
const inputIdentifiers = [];
for (let index = 0; index < tvControlMenuItemsConfig.length; index++) {
let inputName = tvControlMenuItemsConfig[index];
let inputIdentifier;
switch (inputName) {
case 'home':
inputName = 'Home';
inputIdentifier = 1;
break;
case 'settings':
inputName = 'Settings';
inputIdentifier = 2;
break;
case 'movies':
inputName = 'Movies';
inputIdentifier = 3;
break;
case 'tvshows':
inputName = 'TV shows';
inputIdentifier = 4;
break;
case 'tv':
inputName = 'TV';
inputIdentifier = 5;
break;
case 'music':
inputName = 'Music';
inputIdentifier = 6;
break;
case 'musicvideos':
inputName = 'Music videos';
inputIdentifier = 7;
break;
case 'radio':
inputName = 'Radio';
inputIdentifier = 8;
break;
case 'games':
inputName = 'Games';
inputIdentifier = 9;
break;
case 'addons':
inputName = 'Add-ons';
inputIdentifier = 10;
break;
case 'pictures':
inputName = 'Pictures';
inputIdentifier = 11;
break;
case 'videos':
inputName = 'Videos';
inputIdentifier = 12;
break;
case 'favorites':
inputName = 'Favorites';
inputIdentifier = 13;
break;
case 'weather':
inputName = 'Weather';
inputIdentifier = 14;
break;
default:
inputName = null;
inputIdentifier = null;
break;
}
if (inputName && inputIdentifier) {
this.log.info('Adding input for ' + name + ': ' + inputName);
inputNames.push(inputName);
inputIdentifiers.push(inputIdentifier);
}
}
const accessory = this.addTelevisionAccessory(name, internal_1.TelevisionAccessoryType.Controls, inputNames, inputIdentifiers, internal_1.TelevisionAccessory);
this.televisionControlsService = accessory === null || accessory === void 0 ? void 0 : accessory.getService(this.Service.Television);
this.televisionControlsSpeakerService = accessory === null || accessory === void 0 ? void 0 : accessory.getService(this.Service.TelevisionSpeaker);
}
name = platformname + ' Channels';
if (tvChannelsConfig) {
this.log.info('Adding ' + name);
const inputNames = [];
const inputIdentifiers = [];
for (let index = 0; index < tvChannelsChannelsConfig.length; index++) {
const inputName = tvChannelsChannelsConfig[index];
this.log.info('Adding Input for ' + name + ': ' + inputName);
inputNames.push(inputName);
inputIdentifiers.push(index + 1);
}
const accessory = this.addTelevisionAccessory(name, internal_1.TelevisionAccessoryType.Channels, inputNames, inputIdentifiers, internal_1.TelevisionAccessory);
this.televisionChannelsService = accessory === null || accessory === void 0 ? void 0 : accessory.getService(this.Service.Television);
this.televisionChannelsSpeakerService = accessory === null || accessory === void 0 ? void 0 : accessory.getService(this.Service.TelevisionSpeaker);
}