Add: Try to autoidentify plexPlayer by name, product, or ip addresses if machineID not given

pull/16/head
Juraj Nyíri 4 years ago
parent ac4c746799
commit 3181ac0493

@ -18728,6 +18728,7 @@ class Plex {
class PlayController { class PlayController {
constructor(hass, plex, entity) { constructor(hass, plex, entity) {
this.plexPlayerEntity = '';
this.supported = supported; this.supported = supported;
this.getState = async (entityID) => { this.getState = async (entityID) => {
return this.hass.callApi('GET', `states/${entityID}`); return this.hass.callApi('GET', `states/${entityID}`);
@ -18819,6 +18820,7 @@ class PlayController {
}; };
}; };
this.playViaPlexPlayer = async (movieID) => { this.playViaPlexPlayer = async (movieID) => {
const machineID = !lodash.isEmpty(this.plexPlayerEntity) ? this.plexPlayerEntity : this.entity.plexPlayer;
const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID); const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID);
const url = `${this.plex.protocol}://${this.plex.ip}:${this.plex.port}/player/playback/playMedia?address=${this.plex.ip}&commandID=1&containerKey=/playQueues/${playQueueID}?window=100%26own=1&key=/library/metadata/${playQueueSelectedMetadataItemID}&machineIdentifier=${await this.plex.getServerID()}&offset=0&port=${this.plex.port}&token=${this.plex.token}&type=video&protocol=${this.plex.protocol}`; const url = `${this.plex.protocol}://${this.plex.ip}:${this.plex.port}/player/playback/playMedia?address=${this.plex.ip}&commandID=1&containerKey=/playQueues/${playQueueID}?window=100%26own=1&key=/library/metadata/${playQueueSelectedMetadataItemID}&machineIdentifier=${await this.plex.getServerID()}&offset=0&port=${this.plex.port}&token=${this.plex.token}&type=video&protocol=${this.plex.protocol}`;
try { try {
@ -18826,7 +18828,7 @@ class PlayController {
method: 'post', method: 'post',
url, url,
headers: { headers: {
'X-Plex-Target-Client-Identifier': this.entity.plexPlayer, 'X-Plex-Target-Client-Identifier': machineID,
'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant' 'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant'
} }
}); });
@ -18924,6 +18926,19 @@ class PlayController {
return false; return false;
} }
}); });
// Try to look into any other fields to identify machine ID
if (!found) {
lodash.forEach(this.plex.clients, plexClient => {
if (lodash.isEqual(plexClient.product, this.entity.plexPlayer) ||
lodash.isEqual(plexClient.name, this.entity.plexPlayer) ||
lodash.isEqual(plexClient.host, this.entity.plexPlayer) ||
lodash.isEqual(plexClient.address, this.entity.plexPlayer)) {
this.plexPlayerEntity = plexClient.machineIdentifier;
found = true;
return false;
}
});
}
return found; return found;
}; };
this.isPlaySupported = (data) => { this.isPlaySupported = (data) => {

@ -9,6 +9,8 @@ import { supported } from '../const';
class PlayController { class PlayController {
entity: Record<string, any>; entity: Record<string, any>;
plexPlayerEntity = '';
hass: HomeAssistant; hass: HomeAssistant;
plex: Plex; plex: Plex;
@ -120,6 +122,7 @@ class PlayController {
}; };
private playViaPlexPlayer = async (movieID: number): Promise<void> => { private playViaPlexPlayer = async (movieID: number): Promise<void> => {
const machineID = !_.isEmpty(this.plexPlayerEntity) ? this.plexPlayerEntity : this.entity.plexPlayer;
const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID); const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID);
const url = `${this.plex.protocol}://${this.plex.ip}:${this.plex.port}/player/playback/playMedia?address=${ const url = `${this.plex.protocol}://${this.plex.ip}:${this.plex.port}/player/playback/playMedia?address=${
@ -132,7 +135,7 @@ class PlayController {
method: 'post', method: 'post',
url, url,
headers: { headers: {
'X-Plex-Target-Client-Identifier': this.entity.plexPlayer, 'X-Plex-Target-Client-Identifier': machineID,
'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant' 'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant'
} }
}); });
@ -238,6 +241,21 @@ class PlayController {
return false; return false;
} }
}); });
// Try to look into any other fields to identify machine ID
if (!found) {
_.forEach(this.plex.clients, plexClient => {
if (
_.isEqual(plexClient.product, this.entity.plexPlayer) ||
_.isEqual(plexClient.name, this.entity.plexPlayer) ||
_.isEqual(plexClient.host, this.entity.plexPlayer) ||
_.isEqual(plexClient.address, this.entity.plexPlayer)
) {
this.plexPlayerEntity = plexClient.machineIdentifier;
found = true;
return false;
}
});
}
return found; return found;
}; };

Loading…
Cancel
Save