From 52c3ce67de50572ac70f5a6a175e3705b7a8a782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Fri, 11 Jun 2021 00:32:00 +0200 Subject: [PATCH] Add: runBefore parameter --- dist/plex-meets-homeassistant.js | 49 ++++++++++++++++++---------- src/modules/PlayController.ts | 55 ++++++++++++++++++++------------ src/plex-meets-homeassistant.ts | 7 +++- 3 files changed, 72 insertions(+), 39 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index a04448d..aa151f9 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -18830,8 +18830,9 @@ class Plex { } class PlayController { - constructor(hass, plex, entity) { + constructor(hass, plex, entity, runBefore) { this.plexPlayerEntity = ''; + this.runBefore = false; this.supported = supported; this.getState = async (entityID) => { return this.hass.callApi('GET', `states/${entityID}`); @@ -18891,6 +18892,9 @@ class PlayController { return foundResult; }; this.play = async (data, instantPlay = false) => { + if (lodash.isArray(this.runBefore)) { + await this.hass.callService(this.runBefore[0], this.runBefore[1], {}); + } const entity = this.getPlayService(data); switch (entity.key) { case 'kodi': @@ -19053,13 +19057,6 @@ class PlayController { }); return service; }; - this.isPlexPlayerSupported = (entityName) => { - let found = false; - if (this.getPlexPlayerMachineIdentifier(entityName)) { - found = true; - } - return found; - }; this.getPlexPlayerMachineIdentifier = (entityName) => { let machineIdentifier = ''; lodash.forEach(this.plex.clients, plexClient => { @@ -19077,30 +19074,44 @@ class PlayController { this.isPlaySupported = (data) => { return !lodash.isEmpty(this.getPlayService(data)); }; + this.isPlexPlayerSupported = (entityName) => { + let found = false; + if (this.getPlexPlayerMachineIdentifier(entityName)) { + found = true; + } + return found || !lodash.isEqual(this.runBefore, false); + }; this.isKodiSupported = (entityName) => { if (entityName) { - return (this.hass.states[entityName] && - this.hass.states['sensor.kodi_media_sensor_search'] && - this.hass.states['sensor.kodi_media_sensor_search'].state !== 'unavailable' && + const hasKodiMediaSearchInstalled = this.hass.states['sensor.kodi_media_sensor_search'] && + this.hass.states['sensor.kodi_media_sensor_search'].state !== 'unavailable'; + return ((this.hass.states[entityName] && this.hass.states[entityName].state !== 'off' && - this.hass.states[entityName].state !== 'unavailable'); + this.hass.states[entityName].state !== 'unavailable' && + hasKodiMediaSearchInstalled) || + (!lodash.isEqual(this.runBefore, false) && hasKodiMediaSearchInstalled)); } return false; }; this.isCastSupported = (entityName) => { - return (this.hass.states[entityName] && + return ((this.hass.states[entityName] && !lodash.isNil(this.hass.states[entityName].attributes) && - this.hass.states[entityName].state !== 'unavailable'); + this.hass.states[entityName].state !== 'unavailable') || + !lodash.isEqual(this.runBefore, false)); }; this.isAndroidTVSupported = (entityName) => { - return (this.hass.states[entityName] && + return ((this.hass.states[entityName] && !lodash.isEqual(this.hass.states[entityName].state, 'off') && this.hass.states[entityName].attributes && - this.hass.states[entityName].attributes.adb_response !== undefined); + this.hass.states[entityName].attributes.adb_response !== undefined) || + !lodash.isEqual(this.runBefore, false)); }; this.hass = hass; this.plex = plex; this.entity = entity; + if (!lodash.isEmpty(runBefore) && this.hass.states[runBefore]) { + this.runBefore = runBefore.split('.'); + } } } @@ -19898,6 +19909,7 @@ class PlexMeetsHomeAssistant extends HTMLElement { super(...arguments); this.plexProtocol = 'http'; this.detailsShown = false; + this.runBefore = ''; this.columnsCount = 0; this.renderedItems = 0; this.maxRenderCount = false; @@ -21018,6 +21030,9 @@ class PlexMeetsHomeAssistant extends HTMLElement { if (config.maxCount) { this.maxCount = config.maxCount; } + if (config.runBefore) { + this.runBefore = config.runBefore; + } this.plex = new Plex(this.config.ip, this.config.port, this.config.token, this.plexProtocol, this.config.sort); }; this.getCardSize = () => { @@ -21027,7 +21042,7 @@ 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.playController = new PlayController(this.hassObj, this.plex, this.config.entity, this.runBefore); } if (!this.content) { this.error = ''; diff --git a/src/modules/PlayController.ts b/src/modules/PlayController.ts index c1fa847..074eb64 100644 --- a/src/modules/PlayController.ts +++ b/src/modules/PlayController.ts @@ -16,12 +16,17 @@ class PlayController { plex: Plex; + runBefore: Array | false = false; + supported: any = supported; - constructor(hass: HomeAssistant, plex: Plex, entity: Record) { + constructor(hass: HomeAssistant, plex: Plex, entity: Record, runBefore: string) { this.hass = hass; this.plex = plex; this.entity = entity; + if (!_.isEmpty(runBefore) && this.hass.states[runBefore]) { + this.runBefore = runBefore.split('.'); + } } private getState = async (entityID: string): Promise> => { @@ -86,6 +91,9 @@ class PlayController { }; play = async (data: Record, instantPlay = false): Promise => { + if (_.isArray(this.runBefore)) { + await this.hass.callService(this.runBefore[0], this.runBefore[1], {}); + } const entity = this.getPlayService(data); switch (entity.key) { case 'kodi': @@ -268,14 +276,6 @@ class PlayController { return service; }; - isPlexPlayerSupported = (entityName: string): boolean => { - let found = false; - if (this.getPlexPlayerMachineIdentifier(entityName)) { - found = true; - } - return found; - }; - private getPlexPlayerMachineIdentifier = (entityName: string): string => { let machineIdentifier = ''; _.forEach(this.plex.clients, plexClient => { @@ -297,14 +297,25 @@ class PlayController { return !_.isEmpty(this.getPlayService(data)); }; + private isPlexPlayerSupported = (entityName: string): boolean => { + let found = false; + if (this.getPlexPlayerMachineIdentifier(entityName)) { + found = true; + } + return found || !_.isEqual(this.runBefore, false); + }; + private isKodiSupported = (entityName: string): boolean => { if (entityName) { - return ( - this.hass.states[entityName] && + const hasKodiMediaSearchInstalled = this.hass.states['sensor.kodi_media_sensor_search'] && - this.hass.states['sensor.kodi_media_sensor_search'].state !== 'unavailable' && - this.hass.states[entityName].state !== 'off' && - this.hass.states[entityName].state !== 'unavailable' + this.hass.states['sensor.kodi_media_sensor_search'].state !== 'unavailable'; + return ( + (this.hass.states[entityName] && + this.hass.states[entityName].state !== 'off' && + this.hass.states[entityName].state !== 'unavailable' && + hasKodiMediaSearchInstalled) || + (!_.isEqual(this.runBefore, false) && hasKodiMediaSearchInstalled) ); } return false; @@ -312,18 +323,20 @@ class PlayController { private isCastSupported = (entityName: string): boolean => { return ( - this.hass.states[entityName] && - !_.isNil(this.hass.states[entityName].attributes) && - this.hass.states[entityName].state !== 'unavailable' + (this.hass.states[entityName] && + !_.isNil(this.hass.states[entityName].attributes) && + this.hass.states[entityName].state !== 'unavailable') || + !_.isEqual(this.runBefore, false) ); }; private isAndroidTVSupported = (entityName: string): boolean => { return ( - this.hass.states[entityName] && - !_.isEqual(this.hass.states[entityName].state, 'off') && - this.hass.states[entityName].attributes && - this.hass.states[entityName].attributes.adb_response !== undefined + (this.hass.states[entityName] && + !_.isEqual(this.hass.states[entityName].state, 'off') && + this.hass.states[entityName].attributes && + this.hass.states[entityName].attributes.adb_response !== undefined) || + !_.isEqual(this.runBefore, false) ); }; } diff --git a/src/plex-meets-homeassistant.ts b/src/plex-meets-homeassistant.ts index 6baca16..e5eaca1 100644 --- a/src/plex-meets-homeassistant.ts +++ b/src/plex-meets-homeassistant.ts @@ -23,6 +23,8 @@ class PlexMeetsHomeAssistant extends HTMLElement { detailsShown = false; + runBefore = ''; + renderNewElementsIfNeededTimeout: any; columnsCount = 0; @@ -106,7 +108,7 @@ 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.playController = new PlayController(this.hassObj, this.plex, this.config.entity, this.runBefore); } if (!this.content) { @@ -1341,6 +1343,9 @@ class PlexMeetsHomeAssistant extends HTMLElement { if (config.maxCount) { this.maxCount = config.maxCount; } + if (config.runBefore) { + this.runBefore = config.runBefore; + } this.plex = new Plex(this.config.ip, this.config.port, this.config.token, this.plexProtocol, this.config.sort); };