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 1/2] 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; + } } } }; From 4c9410714197f795ca3afb5f242dd0e3715da955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Sun, 4 Jul 2021 22:40:19 +0200 Subject: [PATCH 2/2] Add: Workaround now works also for 404 --- README.md | 28 ++++++++++++++++++++++++++++ dist/plex-meets-homeassistant.js | 28 ++++++++++++++-------------- src/modules/PlayController.ts | 26 +++++++++++++------------- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 30a85c4..4e9d7eb 100644 --- a/README.md +++ b/README.md @@ -377,6 +377,34 @@ You can use _:desc_ or _:asc_ after every value to change the order from ascendi | mediaHeight | Sorts by resolution | | mediaBitrate | Sorts by bitrate | +## FAQ + +
+ I am using plexPlayer and nothing happens when play button is pressed + +- Make sure that your device is turned on, and has Plex running +- Reload tab +- Try hitting play button again + +If it still doesn't work and everything else works, you are probably encountering CORS issue. You can check this by opening developer tools and looking into console log or network tab. + +The reason why this is happening only for this call is right now unknown, but there is a workaround. + +Edit configuration.yaml of your Home Assistant and add following rest_command: + +``` +rest_command: + pmha_playmedia: + url: "{{ url }}" + headers: + X-Plex-Target-Client-Identifier: "{{ target_client_identifier }}" + X-Plex-Client-Identifier: "{{ client_identifier }}" +``` + +Restart Home Assistant, clear browser cache and try hitting play again. It should now work. + +
+ ## Ask for help or help development Join [Discord](https://discord.gg/jqqz9jQXWx) or [Home Assistant Community](https://community.home-assistant.io/t/custom-component-card-plex-meets-home-assistant/304349). diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index 3760da1..d411e8f 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -18973,24 +18973,24 @@ class PlayController { } } catch (err) { + 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) { + // pass + } if (lodash.includes(err.message, '404')) { throw Error('Defined plexPlayer is currently not available for playback.'); } else { - // 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; - } + throw err; } } }; diff --git a/src/modules/PlayController.ts b/src/modules/PlayController.ts index 9a7beed..b72f74e 100644 --- a/src/modules/PlayController.ts +++ b/src/modules/PlayController.ts @@ -182,22 +182,22 @@ class PlayController { throw Error('Error while asking plex to play a movie - target device not available.'); } } catch (err) { + 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) { + // pass + } if (_.includes(err.message, '404')) { throw Error('Defined plexPlayer is currently not available for playback.'); } else { - // 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; - } + throw err; } } };