Add: Ability to use input select as an entity

pull/47/head
Juraj Nyíri 3 years ago
parent a23435d873
commit 254abcb03d

@ -19399,7 +19399,7 @@ const isScrolledIntoView = (elem) => {
}; };
class PlayController { class PlayController {
constructor(card, hass, plex, entity, runBefore, runAfter, libraryName) { constructor(card, hass, plex, entity, runBefore, runAfter, libraryName, entityRegistry) {
this.playButtons = []; this.playButtons = [];
this.readyPlayersForType = {}; this.readyPlayersForType = {};
this.entityStates = {}; this.entityStates = {};
@ -19409,6 +19409,7 @@ class PlayController {
this.supported = supported; this.supported = supported;
this.playActionButton = document.createElement('button'); this.playActionButton = document.createElement('button');
this.playActionClickFunction = false; this.playActionClickFunction = false;
this.entityRegistry = [];
this.getKodiSearchResults = async () => { this.getKodiSearchResults = async () => {
return JSON.parse((await getState(this.hass, 'sensor.kodi_media_sensor_search')).attributes.data); return JSON.parse((await getState(this.hass, 'sensor.kodi_media_sensor_search')).attributes.data);
}; };
@ -19879,6 +19880,55 @@ class PlayController {
await sleep(1000); await sleep(1000);
} }
}; };
this.exportEntity = (entityID, key) => {
const entities = [];
if (lodash.isEqual(key, 'inputSelect')) {
// special processing for templates
if (lodash.isArray(entityID)) {
for (let i = 0; i < entityID.length; i += 1) {
const realEntityID = lodash.get(this.entityStates[entityID[i]], 'state');
let realEntityKey = 'unknown';
lodash.forEach(this.entityRegistry, entityInRegister => {
if (lodash.isEqual(entityInRegister.entity_id, realEntityID)) {
realEntityKey = entityInRegister.platform;
}
});
entities.push({
value: realEntityID,
key: realEntityKey
});
}
}
else {
const realEntityID = lodash.get(this.entityStates[entityID], 'state');
let realEntityKey = 'unknown';
lodash.forEach(this.entityRegistry, entityInRegister => {
if (lodash.isEqual(entityInRegister.entity_id, realEntityID)) {
realEntityKey = entityInRegister.platform;
}
});
entities.push({
value: realEntityID,
key: realEntityKey
});
}
}
else if (lodash.isArray(entityID)) {
lodash.forEach(entityID, entity => {
entities.push({
value: entity,
key
});
});
}
else {
entities.push({
value: entityID,
key
});
}
return entities;
};
this.getPlayService = (data, forceRefresh = false) => { this.getPlayService = (data, forceRefresh = false) => {
if (!lodash.isNil(this.readyPlayersForType[data.type]) && forceRefresh === false) { if (!lodash.isNil(this.readyPlayersForType[data.type]) && forceRefresh === false) {
return this.readyPlayersForType[data.type]; return this.readyPlayersForType[data.type];
@ -19886,30 +19936,20 @@ class PlayController {
let service = {}; let service = {};
lodash.forEach(this.entity, (value, key) => { lodash.forEach(this.entity, (value, key) => {
if (lodash.isEmpty(service)) { if (lodash.isEmpty(service)) {
const entityVal = value; const entities = this.exportEntity(value, key);
if (lodash.isArray(entityVal)) { lodash.forEach(entities, entity => {
lodash.forEach(entityVal, entity => { if (lodash.includes(this.supported[entity.key], data.type)) {
if (lodash.includes(this.supported[key], data.type)) { // todo: load info in this.entityStates otherwise this will never work for input selects and templates
if ((key === 'kodi' && this.isKodiSupported(entity)) || if ((entity.key === 'kodi' && this.isKodiSupported(entity.value)) ||
(key === 'androidtv' && this.isAndroidTVSupported(entity)) || (entity.key === 'androidtv' && this.isAndroidTVSupported(entity.value)) ||
(key === 'plexPlayer' && this.isPlexPlayerSupported(entity)) || (entity.key === 'plexPlayer' && this.isPlexPlayerSupported(entity.value)) ||
(key === 'cast' && this.isCastSupported(entity))) { (entity.key === 'cast' && this.isCastSupported(entity.value))) {
service = { key, value: entity }; service = { key: entity.key, value: entity.value };
return false; return false;
} }
} }
}); });
} }
else if (lodash.includes(this.supported[key], data.type)) {
if ((key === 'kodi' && this.isKodiSupported(entityVal)) ||
(key === 'androidtv' && this.isAndroidTVSupported(entityVal)) ||
(key === 'plexPlayer' && this.isPlexPlayerSupported(entityVal)) ||
(key === 'cast' && this.isCastSupported(entityVal))) {
service = { key, value: entityVal };
return false;
}
}
}
}); });
this.readyPlayersForType[data.type] = service; this.readyPlayersForType[data.type] = service;
return this.readyPlayersForType[data.type]; return this.readyPlayersForType[data.type];
@ -19989,6 +20029,23 @@ class PlayController {
catch (err) { catch (err) {
// pass // pass
} }
// get values for template entities
for (const [key, value] of Object.entries(this.entity)) {
if (lodash.isEqual(key, 'inputSelect')) {
const entities = this.exportEntity(value, key);
for (const entity of entities) {
if (!lodash.isNil(this.hass.states[entity.value])) {
try {
// eslint-disable-next-line no-await-in-loop
this.entityStates[entity.value] = await getState(this.hass, entity.value);
}
catch (err) {
// pass
}
}
}
}
}
return this.entityStates; return this.entityStates;
}; };
this.getPlexPlayerMachineIdentifier = (entity) => { this.getPlexPlayerMachineIdentifier = (entity) => {
@ -20051,6 +20108,7 @@ class PlayController {
this.entityStates[entityName].attributes.adb_response !== undefined) || this.entityStates[entityName].attributes.adb_response !== undefined) ||
!lodash.isEqual(this.runBefore, false)); !lodash.isEqual(this.runBefore, false));
}; };
this.entityRegistry = entityRegistry;
this.card = card; this.card = card;
this.hass = hass; this.hass = hass;
this.plex = plex; this.plex = plex;
@ -20291,7 +20349,8 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
lodash.forEach(this.entitiesRegistry, entityRegistry => { lodash.forEach(this.entitiesRegistry, entityRegistry => {
if (lodash.isEqual(entityRegistry.platform, 'cast') || if (lodash.isEqual(entityRegistry.platform, 'cast') ||
lodash.isEqual(entityRegistry.platform, 'kodi') || lodash.isEqual(entityRegistry.platform, 'kodi') ||
lodash.isEqual(entityRegistry.platform, 'androidtv')) { lodash.isEqual(entityRegistry.platform, 'androidtv') ||
lodash.isEqual(entityRegistry.platform, 'input_select')) {
const entityName = `${entityRegistry.platform} | ${entityRegistry.entity_id}`; const entityName = `${entityRegistry.platform} | ${entityRegistry.entity_id}`;
entities.appendChild(addDropdownItem(entityName)); entities.appendChild(addDropdownItem(entityName));
addedEntityStrings.push(entityName); addedEntityStrings.push(entityName);
@ -21864,6 +21923,11 @@ class PlexMeetsHomeAssistant extends HTMLElement {
realEntityString = entityString.split(' | ')[1]; realEntityString = entityString.split(' | ')[1];
isPlexPlayer = false; isPlexPlayer = false;
} }
else if (lodash.startsWith(entityString, 'input_select | ')) {
// eslint-disable-next-line prefer-destructuring
realEntityString = entityString.split(' | ')[1];
isPlexPlayer = false;
}
if (isPlexPlayer) { if (isPlexPlayer) {
if (lodash.isNil(entityObj.plexPlayer)) { if (lodash.isNil(entityObj.plexPlayer)) {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
@ -21896,6 +21960,13 @@ class PlexMeetsHomeAssistant extends HTMLElement {
} }
entityObj.kodi.push(entityInRegister.entity_id); entityObj.kodi.push(entityInRegister.entity_id);
break; break;
case 'input_select':
if (lodash.isNil(entityObj.inputSelect)) {
// eslint-disable-next-line no-param-reassign
entityObj.inputSelect = [];
}
entityObj.inputSelect.push(entityInRegister.entity_id);
break;
// pass // pass
} }
} }
@ -21917,7 +21988,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.renderPage(); this.renderPage();
try { try {
if (this.plex && this.hassObj) { if (this.plex && this.hassObj) {
this.playController = new PlayController(this, this.hassObj, this.plex, entity, this.runBefore, this.runAfter, this.config.libraryName); this.playController = new PlayController(this, this.hassObj, this.plex, entity, this.runBefore, this.runAfter, this.config.libraryName, this.entityRegistry);
if (this.playController) { if (this.playController) {
await this.playController.init(); await this.playController.init();
} }

@ -278,7 +278,8 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
if ( if (
_.isEqual(entityRegistry.platform, 'cast') || _.isEqual(entityRegistry.platform, 'cast') ||
_.isEqual(entityRegistry.platform, 'kodi') || _.isEqual(entityRegistry.platform, 'kodi') ||
_.isEqual(entityRegistry.platform, 'androidtv') _.isEqual(entityRegistry.platform, 'androidtv') ||
_.isEqual(entityRegistry.platform, 'input_select')
) { ) {
const entityName = `${entityRegistry.platform} | ${entityRegistry.entity_id}`; const entityName = `${entityRegistry.platform} | ${entityRegistry.entity_id}`;
entities.appendChild(addDropdownItem(entityName)); entities.appendChild(addDropdownItem(entityName));

@ -38,6 +38,8 @@ class PlayController {
card: any; card: any;
entityRegistry: Array<Record<string, any>> = [];
constructor( constructor(
card: any, card: any,
hass: HomeAssistant, hass: HomeAssistant,
@ -45,8 +47,10 @@ class PlayController {
entity: Record<string, any>, entity: Record<string, any>,
runBefore: string, runBefore: string,
runAfter: string, runAfter: string,
libraryName: string libraryName: string,
entityRegistry: Array<Record<string, any>>
) { ) {
this.entityRegistry = entityRegistry;
this.card = card; this.card = card;
this.hass = hass; this.hass = hass;
this.plex = plex; this.plex = plex;
@ -581,6 +585,53 @@ class PlayController {
} }
}; };
private exportEntity = (entityID: Array<any> | string, key: string): Array<Record<string, any>> => {
const entities: Array<Record<string, any>> = [];
if (_.isEqual(key, 'inputSelect')) {
// special processing for templates
if (_.isArray(entityID)) {
for (let i = 0; i < entityID.length; i += 1) {
const realEntityID = _.get(this.entityStates[entityID[i]], 'state');
let realEntityKey = 'unknown';
_.forEach(this.entityRegistry, entityInRegister => {
if (_.isEqual(entityInRegister.entity_id, realEntityID)) {
realEntityKey = entityInRegister.platform;
}
});
entities.push({
value: realEntityID,
key: realEntityKey
});
}
} else {
const realEntityID = _.get(this.entityStates[entityID], 'state');
let realEntityKey = 'unknown';
_.forEach(this.entityRegistry, entityInRegister => {
if (_.isEqual(entityInRegister.entity_id, realEntityID)) {
realEntityKey = entityInRegister.platform;
}
});
entities.push({
value: realEntityID,
key: realEntityKey
});
}
} else if (_.isArray(entityID)) {
_.forEach(entityID, entity => {
entities.push({
value: entity,
key
});
});
} else {
entities.push({
value: entityID,
key
});
}
return entities;
};
private getPlayService = (data: Record<string, any>, forceRefresh = false): Record<string, string> => { private getPlayService = (data: Record<string, any>, forceRefresh = false): Record<string, string> => {
if (!_.isNil(this.readyPlayersForType[data.type]) && forceRefresh === false) { if (!_.isNil(this.readyPlayersForType[data.type]) && forceRefresh === false) {
return this.readyPlayersForType[data.type]; return this.readyPlayersForType[data.type];
@ -588,32 +639,22 @@ class PlayController {
let service: Record<string, string> = {}; let service: Record<string, string> = {};
_.forEach(this.entity, (value, key) => { _.forEach(this.entity, (value, key) => {
if (_.isEmpty(service)) { if (_.isEmpty(service)) {
const entityVal = value; const entities = this.exportEntity(value, key);
if (_.isArray(entityVal)) {
_.forEach(entityVal, entity => { _.forEach(entities, entity => {
if (_.includes(this.supported[key], data.type)) { if (_.includes(this.supported[entity.key], data.type)) {
// todo: load info in this.entityStates otherwise this will never work for input selects and templates
if ( if (
(key === 'kodi' && this.isKodiSupported(entity)) || (entity.key === 'kodi' && this.isKodiSupported(entity.value)) ||
(key === 'androidtv' && this.isAndroidTVSupported(entity)) || (entity.key === 'androidtv' && this.isAndroidTVSupported(entity.value)) ||
(key === 'plexPlayer' && this.isPlexPlayerSupported(entity)) || (entity.key === 'plexPlayer' && this.isPlexPlayerSupported(entity.value)) ||
(key === 'cast' && this.isCastSupported(entity)) (entity.key === 'cast' && this.isCastSupported(entity.value))
) { ) {
service = { key, value: entity }; service = { key: entity.key, value: entity.value };
return false; return false;
} }
} }
}); });
} else if (_.includes(this.supported[key], data.type)) {
if (
(key === 'kodi' && this.isKodiSupported(entityVal)) ||
(key === 'androidtv' && this.isAndroidTVSupported(entityVal)) ||
(key === 'plexPlayer' && this.isPlexPlayerSupported(entityVal)) ||
(key === 'cast' && this.isCastSupported(entityVal))
) {
service = { key, value: entityVal };
return false;
}
}
} }
}); });
this.readyPlayersForType[data.type] = service; this.readyPlayersForType[data.type] = service;
@ -707,6 +748,23 @@ class PlayController {
// pass // pass
} }
// get values for template entities
for (const [key, value] of Object.entries(this.entity)) {
if (_.isEqual(key, 'inputSelect')) {
const entities = this.exportEntity(value, key);
for (const entity of entities) {
if (!_.isNil(this.hass.states[entity.value])) {
try {
// eslint-disable-next-line no-await-in-loop
this.entityStates[entity.value] = await getState(this.hass, entity.value);
} catch (err) {
// pass
}
}
}
}
}
return this.entityStates; return this.entityStates;
}; };

@ -295,6 +295,10 @@ class PlexMeetsHomeAssistant extends HTMLElement {
// eslint-disable-next-line prefer-destructuring // eslint-disable-next-line prefer-destructuring
realEntityString = entityString.split(' | ')[1]; realEntityString = entityString.split(' | ')[1];
isPlexPlayer = false; isPlexPlayer = false;
} else if (_.startsWith(entityString, 'input_select | ')) {
// eslint-disable-next-line prefer-destructuring
realEntityString = entityString.split(' | ')[1];
isPlexPlayer = false;
} }
if (isPlexPlayer) { if (isPlexPlayer) {
if (_.isNil(entityObj.plexPlayer)) { if (_.isNil(entityObj.plexPlayer)) {
@ -327,6 +331,13 @@ class PlexMeetsHomeAssistant extends HTMLElement {
} }
entityObj.kodi.push(entityInRegister.entity_id); entityObj.kodi.push(entityInRegister.entity_id);
break; break;
case 'input_select':
if (_.isNil(entityObj.inputSelect)) {
// eslint-disable-next-line no-param-reassign
entityObj.inputSelect = [];
}
entityObj.inputSelect.push(entityInRegister.entity_id);
break;
default: default:
// pass // pass
} }
@ -356,7 +367,8 @@ class PlexMeetsHomeAssistant extends HTMLElement {
entity, entity,
this.runBefore, this.runBefore,
this.runAfter, this.runAfter,
this.config.libraryName this.config.libraryName,
this.entityRegistry
); );
if (this.playController) { if (this.playController) {
await this.playController.init(); await this.playController.init();

Loading…
Cancel
Save