Add: PlayController for managing playing

pull/16/head
Juraj Nyíri 4 years ago
parent ae5554529c
commit a3e3cc40bf

@ -18696,6 +18696,28 @@ class Plex {
} }
} }
class PlayController {
constructor(hass, plex, entity) {
this.play = async (mediaID) => {
const serverID = await this.plex.getServerID();
const command = `am start -a android.intent.action.VIEW 'plex://server://${serverID}/com.plexapp.plugins.library/library/metadata/${mediaID}'`;
this.hass.callService('androidtv', 'adb_command', {
// eslint-disable-next-line @typescript-eslint/camelcase
entity_id: this.entity,
command
});
};
this.isPlaySupported = () => {
return (this.hass.states[this.entity] &&
this.hass.states[this.entity].attributes &&
this.hass.states[this.entity].attributes.adb_response !== undefined);
};
this.hass = hass;
this.plex = plex;
this.entity = entity;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const escapeHtml = (unsafe) => { const escapeHtml = (unsafe) => {
if (unsafe) { if (unsafe) {
@ -19208,11 +19230,8 @@ class PlexMeetsHomeAssistant extends HTMLElement {
constructor() { constructor() {
super(...arguments); super(...arguments);
this.plexProtocol = 'http'; this.plexProtocol = 'http';
this.plex = undefined;
this.movieElems = []; this.movieElems = [];
this.seasonElemFreshlyLoaded = false; this.seasonElemFreshlyLoaded = false;
this.detailElem = undefined;
this.seasonsElem = undefined;
this.data = {}; this.data = {};
this.config = {}; this.config = {};
this.requestTimeout = 3000; this.requestTimeout = 3000;
@ -19555,16 +19574,8 @@ class PlexMeetsHomeAssistant extends HTMLElement {
event.stopPropagation(); event.stopPropagation();
const keyParts = data.key.split('/'); const keyParts = data.key.split('/');
const movieID = keyParts[3]; const movieID = keyParts[3];
const command = `am start -a android.intent.action.VIEW 'plex://server://${serverID}/com.plexapp.plugins.library/library/metadata/${movieID}'`; if (this.hassObj && this.playController) {
console.log(command); this.playController.play(movieID);
// eslint-disable-next-line @typescript-eslint/camelcase
const { entity_id } = this.config;
if (this.hassObj) {
this.hassObj.callService('androidtv', 'adb_command', {
// eslint-disable-next-line @typescript-eslint/camelcase
entity_id,
command
});
} }
}); });
const titleElem = document.createElement('div'); const titleElem = document.createElement('div');
@ -19619,11 +19630,13 @@ class PlexMeetsHomeAssistant extends HTMLElement {
} }
set hass(hass) { set hass(hass) {
this.hassObj = hass; this.hassObj = hass;
if (this.plex) {
this.playController = new PlayController(this.hassObj, this.plex, this.config.entity_id);
}
if (!this.content) { if (!this.content) {
this.playSupported = if (this.playController) {
hass.states[this.config.entity_id] && this.playSupported = this.playController.isPlaySupported();
hass.states[this.config.entity_id].attributes && }
hass.states[this.config.entity_id].attributes.adb_response !== undefined;
this.error = ''; this.error = '';
if (!this.loading) { if (!this.loading) {
this.loadInitialData(); this.loadInitialData();

@ -0,0 +1,37 @@
import { HomeAssistant } from 'custom-card-helpers';
import Plex from './Plex';
class PlayController {
entity: string;
hass: HomeAssistant;
plex: Plex;
constructor(hass: HomeAssistant, plex: Plex, entity: string) {
this.hass = hass;
this.plex = plex;
this.entity = entity;
}
play = async (mediaID: number): Promise<void> => {
const serverID = await this.plex.getServerID();
const command = `am start -a android.intent.action.VIEW 'plex://server://${serverID}/com.plexapp.plugins.library/library/metadata/${mediaID}'`;
this.hass.callService('androidtv', 'adb_command', {
// eslint-disable-next-line @typescript-eslint/camelcase
entity_id: this.entity,
command
});
};
isPlaySupported = (): boolean => {
return (
this.hass.states[this.entity] &&
this.hass.states[this.entity].attributes &&
this.hass.states[this.entity].attributes.adb_response !== undefined
);
};
}
export default PlayController;

@ -3,6 +3,7 @@
import { HomeAssistant } from 'custom-card-helpers'; import { HomeAssistant } from 'custom-card-helpers';
import _ from 'lodash'; import _ from 'lodash';
import Plex from './modules/Plex'; import Plex from './modules/Plex';
import PlayController from './modules/PlayController';
import { escapeHtml } from './modules/utils'; import { escapeHtml } from './modules/utils';
import { CSS_STYLE, LOREM_IPSUM } from './const'; import { CSS_STYLE, LOREM_IPSUM } from './const';
import style from './modules/style'; import style from './modules/style';
@ -10,15 +11,17 @@ import style from './modules/style';
class PlexMeetsHomeAssistant extends HTMLElement { class PlexMeetsHomeAssistant extends HTMLElement {
plexProtocol: 'http' | 'https' = 'http'; plexProtocol: 'http' | 'https' = 'http';
plex: Plex | undefined = undefined; plex: Plex | undefined;
playController: PlayController | undefined;
movieElems: any = []; movieElems: any = [];
seasonElemFreshlyLoaded = false; seasonElemFreshlyLoaded = false;
detailElem: HTMLElement | undefined = undefined; detailElem: HTMLElement | undefined;
seasonsElem: HTMLElement | undefined = undefined; seasonsElem: HTMLElement | undefined;
data: Record<string, any> = {}; data: Record<string, any> = {};
@ -42,11 +45,14 @@ class PlexMeetsHomeAssistant extends HTMLElement {
set hass(hass: HomeAssistant) { set hass(hass: HomeAssistant) {
this.hassObj = hass; this.hassObj = hass;
if (this.plex) {
this.playController = new PlayController(this.hassObj, this.plex, this.config.entity_id);
}
if (!this.content) { if (!this.content) {
this.playSupported = if (this.playController) {
hass.states[this.config.entity_id] && this.playSupported = this.playController.isPlaySupported();
hass.states[this.config.entity_id].attributes && }
hass.states[this.config.entity_id].attributes.adb_response !== undefined;
this.error = ''; this.error = '';
if (!this.loading) { if (!this.loading) {
@ -438,17 +444,9 @@ class PlexMeetsHomeAssistant extends HTMLElement {
event.stopPropagation(); event.stopPropagation();
const keyParts = data.key.split('/'); const keyParts = data.key.split('/');
const movieID = keyParts[3]; const movieID = keyParts[3];
const command = `am start -a android.intent.action.VIEW 'plex://server://${serverID}/com.plexapp.plugins.library/library/metadata/${movieID}'`;
console.log(command); if (this.hassObj && this.playController) {
// eslint-disable-next-line @typescript-eslint/camelcase this.playController.play(movieID);
const { entity_id } = this.config;
if (this.hassObj) {
this.hassObj.callService('androidtv', 'adb_command', {
// eslint-disable-next-line @typescript-eslint/camelcase
entity_id,
command
});
} }
}); });

Loading…
Cancel
Save