From 7c7a260174bc626b91331d2629a7485ac423ccf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Thu, 2 Sep 2021 23:59:19 +0200 Subject: [PATCH] Add: Use native Kodi channel functionality if available --- dist/plex-meets-homeassistant.js | 41 ++++++++++++++++++++++---------- src/modules/PlayController.ts | 39 +++++++++++++++++++++--------- src/modules/Plex.ts | 4 +--- 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index 9863ff9..a35e0c3 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -19033,7 +19033,7 @@ class Plex { let res = await axios.get(this.authorizeURL(decisionURL), { timeout: this.requestTimeout }); - while (parseFloat(res.data.MediaContainer.Metadata[0].Media[0].Part[0].key.split('offset=')[1].split('&')[0]) < 10) { + while (parseFloat(res.data.MediaContainer.Metadata[0].Media[0].Part[0].key.split('offset=')[1].split('&')[0]) < 3) { // eslint-disable-next-line no-await-in-loop await sleep(500); // eslint-disable-next-line no-await-in-loop @@ -19333,7 +19333,7 @@ class PlayController { this.getKodiSearchResults = async () => { return JSON.parse((await getState(this.hass, 'sensor.kodi_media_sensor_search')).attributes.data); }; - this.getKodiSearch = async (search) => { + this.getKodiSearch = async (search, silent = false) => { await this.hass.callService('kodi_media_sensors', 'call_method', { // eslint-disable-next-line @typescript-eslint/camelcase entity_id: 'sensor.kodi_media_sensor_search', @@ -19351,8 +19351,12 @@ class PlayController { foundResult = result; return false; } + if (lodash.isEqual(result.label, search)) { + foundResult = result; + return false; + } }); - if (lodash.isEmpty(foundResult)) { + if (lodash.isEmpty(foundResult) && !silent) { // eslint-disable-next-line no-alert alert(`Title ${search} not found in Kodi.`); throw Error(`Title ${search} not found in Kodi.`); @@ -19549,15 +19553,28 @@ class PlayController { }; this.playViaKodi = async (entityName, data, type) => { if (!lodash.isNil(lodash.get(data, 'epg.Media[0].channelCallSign'))) { - const streamLink = `${this.plex.getBasicURL()}${await this.plex.tune(data.channelIdentifier, 'todo')}`; - await this.hass.callService('kodi', 'call_method', { - // eslint-disable-next-line @typescript-eslint/camelcase - entity_id: entityName, - method: 'Player.Open', - item: { - file: streamLink - } - }); + try { + const kodiData = await this.getKodiSearch(lodash.get(data, 'epg.Media[0].channelCallSign'), true); + await this.hass.callService('kodi', 'call_method', { + // eslint-disable-next-line @typescript-eslint/camelcase + entity_id: entityName, + method: 'Player.Open', + item: { + channelid: kodiData.channelid + } + }); + } + catch (err) { + const streamLink = `${this.plex.getBasicURL()}${await this.plex.tune(data.channelIdentifier, 'todo')}`; + await this.hass.callService('kodi', 'call_method', { + // eslint-disable-next-line @typescript-eslint/camelcase + entity_id: entityName, + method: 'Player.Open', + item: { + file: streamLink + } + }); + } } else if (type === 'movie') { const kodiData = await this.getKodiSearch(data.title); diff --git a/src/modules/PlayController.ts b/src/modules/PlayController.ts index 097a570..4caba55 100644 --- a/src/modules/PlayController.ts +++ b/src/modules/PlayController.ts @@ -49,7 +49,7 @@ class PlayController { return JSON.parse((await getState(this.hass, 'sensor.kodi_media_sensor_search')).attributes.data); }; - private getKodiSearch = async (search: string): Promise> => { + private getKodiSearch = async (search: string, silent = false): Promise> => { await this.hass.callService('kodi_media_sensors', 'call_method', { // eslint-disable-next-line @typescript-eslint/camelcase entity_id: 'sensor.kodi_media_sensor_search', @@ -67,8 +67,12 @@ class PlayController { foundResult = result; return false; } + if (_.isEqual(result.label, search)) { + foundResult = result; + return false; + } }); - if (_.isEmpty(foundResult)) { + if (_.isEmpty(foundResult) && !silent) { // eslint-disable-next-line no-alert alert(`Title ${search} not found in Kodi.`); throw Error(`Title ${search} not found in Kodi.`); @@ -280,15 +284,28 @@ class PlayController { private playViaKodi = async (entityName: string, data: Record, type: string): Promise => { if (!_.isNil(_.get(data, 'epg.Media[0].channelCallSign'))) { - const streamLink = `${this.plex.getBasicURL()}${await this.plex.tune(data.channelIdentifier, 'todo')}`; - await this.hass.callService('kodi', 'call_method', { - // eslint-disable-next-line @typescript-eslint/camelcase - entity_id: entityName, - method: 'Player.Open', - item: { - file: streamLink - } - }); + try { + const kodiData = await this.getKodiSearch(_.get(data, 'epg.Media[0].channelCallSign'), true); + + await this.hass.callService('kodi', 'call_method', { + // eslint-disable-next-line @typescript-eslint/camelcase + entity_id: entityName, + method: 'Player.Open', + item: { + channelid: kodiData.channelid + } + }); + } catch (err) { + const streamLink = `${this.plex.getBasicURL()}${await this.plex.tune(data.channelIdentifier, 'todo')}`; + await this.hass.callService('kodi', 'call_method', { + // eslint-disable-next-line @typescript-eslint/camelcase + entity_id: entityName, + method: 'Player.Open', + item: { + file: streamLink + } + }); + } } else if (type === 'movie') { const kodiData = await this.getKodiSearch(data.title); await this.hass.callService('kodi', 'call_method', { diff --git a/src/modules/Plex.ts b/src/modules/Plex.ts index 41f3a67..78ca757 100644 --- a/src/modules/Plex.ts +++ b/src/modules/Plex.ts @@ -475,9 +475,7 @@ class Plex { let res = await axios.get(this.authorizeURL(decisionURL), { timeout: this.requestTimeout }); - while ( - parseFloat(res.data.MediaContainer.Metadata[0].Media[0].Part[0].key.split('offset=')[1].split('&')[0]) < 10 - ) { + while (parseFloat(res.data.MediaContainer.Metadata[0].Media[0].Part[0].key.split('offset=')[1].split('&')[0]) < 3) { // eslint-disable-next-line no-await-in-loop await sleep(500); // eslint-disable-next-line no-await-in-loop