From 480b5ccdc67dfeaeb4110ec740144daee7711500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Sun, 4 Jul 2021 22:00:31 +0200 Subject: [PATCH] Add: Workaround for CORS error on playMedia --- dist/plex-meets-homeassistant.js | 15 ++++++++++++++- src/modules/PlayController.ts | 14 +++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index 3843c15..3760da1 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -18977,7 +18977,20 @@ class PlayController { throw Error('Defined plexPlayer is currently not available for playback.'); } else { - throw err; + // if we caught CORS error, try to use workaround. Todo: figure out why is the CORS issue happening + try { + await this.hass.callService('rest_command', 'pmha_playmedia', { + // eslint-disable-next-line @typescript-eslint/camelcase + url, + // eslint-disable-next-line @typescript-eslint/camelcase + target_client_identifier: machineID, + // eslint-disable-next-line @typescript-eslint/camelcase + client_identifier: 'PlexMeetsHomeAssistant' + }); + } + catch (homeAssistantErr) { + throw err; + } } } }; diff --git a/src/modules/PlayController.ts b/src/modules/PlayController.ts index d1b2f97..9a7beed 100644 --- a/src/modules/PlayController.ts +++ b/src/modules/PlayController.ts @@ -185,7 +185,19 @@ class PlayController { if (_.includes(err.message, '404')) { throw Error('Defined plexPlayer is currently not available for playback.'); } else { - throw err; + // if we caught CORS error, try to use workaround. Todo: figure out why is the CORS issue happening + try { + await this.hass.callService('rest_command', 'pmha_playmedia', { + // eslint-disable-next-line @typescript-eslint/camelcase + url, + // eslint-disable-next-line @typescript-eslint/camelcase + target_client_identifier: machineID, + // eslint-disable-next-line @typescript-eslint/camelcase + client_identifier: 'PlexMeetsHomeAssistant' + }); + } catch (homeAssistantErr) { + throw err; + } } } };