From ae5554529c59454df21be3bdcac327be84d934ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Wed, 5 May 2021 17:10:17 +0200 Subject: [PATCH] Update: Move getting server ID into Plex class --- dist/plex-meets-homeassistant.js | 14 +++++++++++--- src/modules/Plex.ts | 12 +++++++++++- src/plex-meets-homeassistant.ts | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index 2887f86..cfe489f 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -18654,9 +18654,17 @@ var axios = axios_1; /* eslint-disable @typescript-eslint/no-explicit-any */ class Plex { constructor(ip, port = 32400, token, protocol = 'http') { + this.serverInfo = {}; + this.getServerID = async () => { + if (lodash.isEmpty(this.serverInfo)) { + await this.getServerInfo(); + } + return this.serverInfo.machineIdentifier; + }; this.getServerInfo = async () => { const url = `${this.protocol}://${this.ip}:${this.port}/?X-Plex-Token=${this.token}`; - return (await axios.get(url)).data.MediaContainer; + this.serverInfo = (await axios.get(url)).data.MediaContainer; + return this.serverInfo; }; this.getSections = async () => { const url = `${this.protocol}://${this.ip}:${this.port}/library/sections?X-Plex-Token=${this.token}`; @@ -19218,9 +19226,9 @@ class PlexMeetsHomeAssistant extends HTMLElement { this.renderPage(); try { if (this.plex) { - const [plexInfo, plexSections] = await Promise.all([this.plex.getServerInfo(), this.plex.getSectionsData()]); + const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); // eslint-disable-next-line @typescript-eslint/camelcase - this.data.serverID = plexInfo.machineIdentifier; + this.data.serverID = serverID; lodash.forEach(plexSections, section => { this.data[section.title1] = section.Metadata; }); diff --git a/src/modules/Plex.ts b/src/modules/Plex.ts index 7cb3f2e..2738095 100644 --- a/src/modules/Plex.ts +++ b/src/modules/Plex.ts @@ -11,6 +11,8 @@ class Plex { protocol: string; + serverInfo: Record = {}; + constructor(ip: string, port = 32400, token: string, protocol: 'http' | 'https' = 'http') { this.ip = ip; this.port = port; @@ -18,9 +20,17 @@ class Plex { this.protocol = protocol; } + getServerID = async (): Promise => { + if (_.isEmpty(this.serverInfo)) { + await this.getServerInfo(); + } + return this.serverInfo.machineIdentifier; + }; + getServerInfo = async (): Promise => { const url = `${this.protocol}://${this.ip}:${this.port}/?X-Plex-Token=${this.token}`; - return (await axios.get(url)).data.MediaContainer; + this.serverInfo = (await axios.get(url)).data.MediaContainer; + return this.serverInfo; }; getSections = async (): Promise => { diff --git a/src/plex-meets-homeassistant.ts b/src/plex-meets-homeassistant.ts index c601e1b..2bcd54a 100644 --- a/src/plex-meets-homeassistant.ts +++ b/src/plex-meets-homeassistant.ts @@ -60,9 +60,9 @@ class PlexMeetsHomeAssistant extends HTMLElement { this.renderPage(); try { if (this.plex) { - const [plexInfo, plexSections] = await Promise.all([this.plex.getServerInfo(), this.plex.getSectionsData()]); + const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); // eslint-disable-next-line @typescript-eslint/camelcase - this.data.serverID = plexInfo.machineIdentifier; + this.data.serverID = serverID; _.forEach(plexSections, section => { this.data[section.title1] = section.Metadata; });