From a41077987333cc890518444084175f5abea734e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Sun, 4 Jul 2021 19:51:47 +0200 Subject: [PATCH] Add: WIP Support for client servers for plexPlayer --- dist/plex-meets-homeassistant.js | 68 ++++++++++++++++++++++++++---- src/modules/PlayController.ts | 72 ++++++++++++++++++++++++++++++-- src/plex-meets-homeassistant.ts | 18 ++++---- 3 files changed, 138 insertions(+), 20 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index ab331bf..25fa417 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -19062,9 +19062,59 @@ class PlayController { }); return service; }; - this.getPlexPlayerMachineIdentifier = (entityName) => { + this.init = async () => { + if (!lodash.isNil(this.entity.plexPlayer)) { + if (lodash.isArray(this.entity.plexPlayer)) { + for (let i = 0; i < this.entity.plexPlayer.length; i += 1) { + if (lodash.isObjectLike(this.entity.plexPlayer[i]) && !lodash.isNil(this.entity.plexPlayer[i].server)) { + let port = false; + if (!lodash.isNil(this.entity.plexPlayer[i].server.port)) { + port = this.entity.plexPlayer[i].server.port; + } + let protocol = 'http'; + if (!lodash.isNil(this.entity.plexPlayer[i].server.protocol)) { + protocol = this.entity.plexPlayer[i].server.protocol; + } + // eslint-disable-next-line no-param-reassign + this.entity.plexPlayer[i].plex = new Plex(this.entity.plexPlayer[i].server.ip, port, this.entity.plexPlayer[i].server.token, protocol); + // eslint-disable-next-line no-await-in-loop + await this.entity.plexPlayer[i].plex.getClients(); + } + } + } + else if (!lodash.isNil(this.entity.plexPlayer.server) && + !lodash.isNil(this.entity.plexPlayer.server.ip) && + !lodash.isNil(this.entity.plexPlayer.server.token)) { + let port = false; + if (!lodash.isNil(this.entity.plexPlayer.server.port)) { + port = this.entity.plexPlayer.server.port; + } + let protocol = 'http'; + if (!lodash.isNil(this.entity.plexPlayer.server.protocol)) { + protocol = this.entity.plexPlayer.server.protocol; + } + // eslint-disable-next-line no-param-reassign + this.entity.plexPlayer.plex = new Plex(this.entity.plexPlayer.server.ip, port, this.entity.plexPlayer.server.token, protocol); + // eslint-disable-next-line no-await-in-loop + await this.entity.plexPlayer.plex.getClients(); + } + } + }; + this.getPlexPlayerMachineIdentifier = (entity) => { let machineIdentifier = ''; - lodash.forEach(this.plex.clients, plexClient => { + let { plex } = this; + let entityName = ''; + if (lodash.isString(entity)) { + entityName = entity; + } + else if (lodash.isObjectLike(entity) && !lodash.isNil(entity.identifier)) { + entityName = entity.identifier; + if (!lodash.isNil(entity.plex) && entity.plex) { + plex = entity.plex; + } + } + console.log(plex.clients); + lodash.forEach(plex.clients, plexClient => { if (lodash.isEqual(plexClient.machineIdentifier, entityName) || lodash.isEqual(plexClient.product, entityName) || lodash.isEqual(plexClient.name, entityName) || @@ -19079,9 +19129,9 @@ class PlayController { this.isPlaySupported = (data) => { return !lodash.isEmpty(this.getPlayService(data)); }; - this.isPlexPlayerSupported = (entityName) => { + this.isPlexPlayerSupported = (entity) => { let found = false; - if (this.getPlexPlayerMachineIdentifier(entityName)) { + if (this.getPlexPlayerMachineIdentifier(entity)) { found = true; } return found || !lodash.isEqual(this.runBefore, false); @@ -20136,6 +20186,13 @@ class PlexMeetsHomeAssistant extends HTMLElement { } this.renderNewElementsIfNeeded(); }); + if (this.hassObj && this.plex) { + const entityConfig = JSON.parse(JSON.stringify(this.config.entity)); // todo: find a nicer solution + this.playController = new PlayController(this.hassObj, this.plex, entityConfig, this.runBefore, this.runAfter); + if (this.playController) { + await this.playController.init(); + } + } if (this.card) { this.previousPageWidth = this.card.offsetWidth; } @@ -21287,9 +21344,6 @@ class PlexMeetsHomeAssistant extends HTMLElement { } set hass(hass) { this.hassObj = hass; - if (this.plex) { - this.playController = new PlayController(this.hassObj, this.plex, this.config.entity, this.runBefore, this.runAfter); - } if (!this.content) { this.error = ''; if (!this.loading) { diff --git a/src/modules/PlayController.ts b/src/modules/PlayController.ts index 47899df..790122e 100644 --- a/src/modules/PlayController.ts +++ b/src/modules/PlayController.ts @@ -285,9 +285,72 @@ class PlayController { return service; }; - private getPlexPlayerMachineIdentifier = (entityName: string): string => { + init = async (): Promise => { + if (!_.isNil(this.entity.plexPlayer)) { + if (_.isArray(this.entity.plexPlayer)) { + for (let i = 0; i < this.entity.plexPlayer.length; i += 1) { + if (_.isObjectLike(this.entity.plexPlayer[i]) && !_.isNil(this.entity.plexPlayer[i].server)) { + let port: number | false = false; + if (!_.isNil(this.entity.plexPlayer[i].server.port)) { + port = this.entity.plexPlayer[i].server.port; + } + let protocol: 'http' | 'https' = 'http'; + if (!_.isNil(this.entity.plexPlayer[i].server.protocol)) { + protocol = this.entity.plexPlayer[i].server.protocol; + } + // eslint-disable-next-line no-param-reassign + this.entity.plexPlayer[i].plex = new Plex( + this.entity.plexPlayer[i].server.ip, + port, + this.entity.plexPlayer[i].server.token, + protocol + ); + // eslint-disable-next-line no-await-in-loop + await this.entity.plexPlayer[i].plex.getClients(); + } + } + } else if ( + !_.isNil(this.entity.plexPlayer.server) && + !_.isNil(this.entity.plexPlayer.server.ip) && + !_.isNil(this.entity.plexPlayer.server.token) + ) { + let port: number | false = false; + if (!_.isNil(this.entity.plexPlayer.server.port)) { + port = this.entity.plexPlayer.server.port; + } + let protocol: 'http' | 'https' = 'http'; + if (!_.isNil(this.entity.plexPlayer.server.protocol)) { + protocol = this.entity.plexPlayer.server.protocol; + } + // eslint-disable-next-line no-param-reassign + this.entity.plexPlayer.plex = new Plex( + this.entity.plexPlayer.server.ip, + port, + this.entity.plexPlayer.server.token, + protocol + ); + // eslint-disable-next-line no-await-in-loop + await this.entity.plexPlayer.plex.getClients(); + } + } + }; + + private getPlexPlayerMachineIdentifier = (entity: string | Record): string => { let machineIdentifier = ''; - _.forEach(this.plex.clients, plexClient => { + + let { plex } = this; + let entityName = ''; + if (_.isString(entity)) { + entityName = entity; + } else if (_.isObjectLike(entity) && !_.isNil(entity.identifier)) { + entityName = entity.identifier; + if (!_.isNil(entity.plex) && entity.plex) { + plex = entity.plex; + } + } + + console.log(plex.clients); + _.forEach(plex.clients, plexClient => { if ( _.isEqual(plexClient.machineIdentifier, entityName) || _.isEqual(plexClient.product, entityName) || @@ -306,11 +369,12 @@ class PlayController { return !_.isEmpty(this.getPlayService(data)); }; - private isPlexPlayerSupported = (entityName: string): boolean => { + private isPlexPlayerSupported = (entity: string | Record): boolean => { let found = false; - if (this.getPlexPlayerMachineIdentifier(entityName)) { + if (this.getPlexPlayerMachineIdentifier(entity)) { found = true; } + return found || !_.isEqual(this.runBefore, false); }; diff --git a/src/plex-meets-homeassistant.ts b/src/plex-meets-homeassistant.ts index dc93f8a..6565c84 100644 --- a/src/plex-meets-homeassistant.ts +++ b/src/plex-meets-homeassistant.ts @@ -117,15 +117,6 @@ class PlexMeetsHomeAssistant extends HTMLElement { set hass(hass: HomeAssistant) { this.hassObj = hass; - if (this.plex) { - this.playController = new PlayController( - this.hassObj, - this.plex, - this.config.entity, - this.runBefore, - this.runAfter - ); - } if (!this.content) { this.error = ''; @@ -211,6 +202,15 @@ class PlexMeetsHomeAssistant extends HTMLElement { } this.renderNewElementsIfNeeded(); }); + + if (this.hassObj && this.plex) { + const entityConfig: Record = JSON.parse(JSON.stringify(this.config.entity)); // todo: find a nicer solution + this.playController = new PlayController(this.hassObj, this.plex, entityConfig, this.runBefore, this.runAfter); + if (this.playController) { + await this.playController.init(); + } + } + if (this.card) { this.previousPageWidth = this.card.offsetWidth; }