2.0
Juraj Nyíri 3 years ago
parent 66334602c5
commit 8bb96a0e49

@ -19455,6 +19455,9 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.tabs = document.createElement('paper-tabs'); this.tabs = document.createElement('paper-tabs');
this.devicesTabs = 0; this.devicesTabs = 0;
this.entities = []; this.entities = [];
this.sections = [];
this.entitiesRegistry = false;
this.plexValidSection = document.createElement('div');
this.fireEvent = (node, type, detail, options = {}) => { this.fireEvent = (node, type, detail, options = {}) => {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
detail = detail === null || detail === undefined ? {} : detail; detail = detail === null || detail === undefined ? {} : detail;
@ -19473,10 +19476,12 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.config.token = this.token.value; this.config.token = this.token.value;
this.config.port = this.port.value; this.config.port = this.port.value;
this.config.libraryName = this.libraryName.value; this.config.libraryName = this.libraryName.value;
this.config.entity = []; if (!lodash.isEmpty(this.entities)) {
lodash.forEach(this.entities, entity => { this.config.entity = [];
this.config.entity.push(entity.value); lodash.forEach(this.entities, entity => {
}); this.config.entity.push(entity.value);
});
}
this.fireEvent(this, 'config-changed', { config: this.config }); this.fireEvent(this, 'config-changed', { config: this.config });
} }
}; };
@ -19486,31 +19491,33 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
libraryItem.innerHTML = text; libraryItem.innerHTML = text;
return libraryItem; return libraryItem;
}; };
const createEntitiesDropdown = (entitiesRegistry, selected, changeHandler) => { const createEntitiesDropdown = (selected, changeHandler) => {
const entitiesDropDown = document.createElement('paper-dropdown-menu'); if (this.entitiesRegistry) {
const entities = document.createElement('paper-listbox'); const entitiesDropDown = document.createElement('paper-dropdown-menu');
lodash.forEach(entitiesRegistry, entityRegistry => { const entities = document.createElement('paper-listbox');
if (lodash.isEqual(entityRegistry.platform, 'cast') || lodash.forEach(this.entitiesRegistry, entityRegistry => {
lodash.isEqual(entityRegistry.platform, 'kodi') || if (lodash.isEqual(entityRegistry.platform, 'cast') ||
lodash.isEqual(entityRegistry.platform, 'androidtv')) { lodash.isEqual(entityRegistry.platform, 'kodi') ||
entities.appendChild(addDropdownItem(entityRegistry.entity_id)); lodash.isEqual(entityRegistry.platform, 'androidtv')) {
} entities.appendChild(addDropdownItem(entityRegistry.entity_id));
}); }
entities.slot = 'dropdown-content'; });
entitiesDropDown.label = 'Entity'; entities.slot = 'dropdown-content';
entitiesDropDown.value = selected; entitiesDropDown.label = 'Entity';
entitiesDropDown.appendChild(entities); entitiesDropDown.value = selected;
entitiesDropDown.style.width = '100%'; entitiesDropDown.appendChild(entities);
entitiesDropDown.className = 'entitiesDropDown'; entitiesDropDown.style.width = '100%';
entitiesDropDown.addEventListener('value-changed', changeHandler); entitiesDropDown.className = 'entitiesDropDown';
this.entities.push(entitiesDropDown); entitiesDropDown.addEventListener('value-changed', changeHandler);
return entitiesDropDown; this.entities.push(entitiesDropDown);
return entitiesDropDown;
}
return false;
}; };
if (this.content) if (this.content)
this.content.remove(); this.content.remove();
let entitiesRegistry = false; if (this.hassObj && !this.entitiesRegistry) {
if (this.hassObj) { this.entitiesRegistry = await fetchEntityRegistry(this.hassObj.connection);
entitiesRegistry = await fetchEntityRegistry(this.hassObj.connection);
} }
this.entities = []; this.entities = [];
this.content = document.createElement('div'); this.content = document.createElement('div');
@ -19545,6 +19552,18 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.libraryName.style.width = '100%'; this.libraryName.style.width = '100%';
this.libraryName.addEventListener('value-changed', this.valueUpdated); this.libraryName.addEventListener('value-changed', this.valueUpdated);
this.content.appendChild(this.libraryName); this.content.appendChild(this.libraryName);
this.appendChild(this.content);
// todo: do verify better, do not query plex every time
this.sections = [];
if (this.plex) {
try {
this.sections = await this.plex.getSections();
}
catch (err) {
// pass
}
}
this.plexValidSection.style.display = 'none';
const devicesTitle = document.createElement('h2'); const devicesTitle = document.createElement('h2');
devicesTitle.innerHTML = `Devices Configuration`; devicesTitle.innerHTML = `Devices Configuration`;
devicesTitle.style.lineHeight = '29px'; devicesTitle.style.lineHeight = '29px';
@ -19556,40 +19575,33 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
addDeviceButton.style.cursor = 'pointer'; addDeviceButton.style.cursor = 'pointer';
addDeviceButton.innerHTML = '+'; addDeviceButton.innerHTML = '+';
addDeviceButton.addEventListener('click', () => { addDeviceButton.addEventListener('click', () => {
if (entitiesRegistry) { const entitiesDropdown = createEntitiesDropdown('', this.valueUpdated);
this.content.appendChild(createEntitiesDropdown(entitiesRegistry, '', this.valueUpdated)); if (entitiesDropdown) {
this.scrollTop = this.scrollHeight - this.clientHeight; this.content.appendChild(entitiesDropdown);
} }
}); });
devicesTitle.appendChild(addDeviceButton); devicesTitle.appendChild(addDeviceButton);
this.content.appendChild(devicesTitle); this.plexValidSection.innerHTML = '';
this.plexValidSection.appendChild(devicesTitle);
if (lodash.isString(this.config.entity)) { if (lodash.isString(this.config.entity)) {
this.config.entity = [this.config.entity]; this.config.entity = [this.config.entity];
} }
if (lodash.isArray(this.config.entity)) { if (lodash.isArray(this.config.entity)) {
lodash.forEach(this.config.entity, entity => { lodash.forEach(this.config.entity, entity => {
if (entitiesRegistry && lodash.isString(entity)) { if (lodash.isString(entity)) ;
this.content.appendChild(createEntitiesDropdown(entitiesRegistry, entity, this.valueUpdated));
}
}); });
} }
this.appendChild(this.content); if (!lodash.isEmpty(this.sections)) {
if (this.plex) { lodash.forEach(this.sections, (section) => {
try { libraryItems.appendChild(addDropdownItem(section.title));
const sections = await this.plex.getSections(); });
lodash.forEach(sections, (section) => { this.libraryName.disabled = false;
libraryItems.appendChild(addDropdownItem(section.title)); this.libraryName.value = this.config.libraryName;
}); this.plexValidSection.style.display = 'block';
this.libraryName.disabled = false;
this.libraryName.value = this.config.libraryName;
}
catch (err) {
// pass
}
} }
this.content.appendChild(this.plexValidSection);
}; };
this.setConfig = (config) => { this.setConfig = (config) => {
console.log(config);
this.config = JSON.parse(JSON.stringify(config)); this.config = JSON.parse(JSON.stringify(config));
if (config.port) { if (config.port) {
this.plexPort = config.port; this.plexPort = config.port;

@ -32,6 +32,12 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
entities: Array<any> = []; entities: Array<any> = [];
sections: Array<Record<string, any>> = [];
entitiesRegistry: false | Array<Record<string, any>> = false;
plexValidSection = document.createElement('div');
fireEvent = ( fireEvent = (
node: HTMLElement, node: HTMLElement,
type: string, type: string,
@ -56,10 +62,12 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.config.token = this.token.value; this.config.token = this.token.value;
this.config.port = this.port.value; this.config.port = this.port.value;
this.config.libraryName = this.libraryName.value; this.config.libraryName = this.libraryName.value;
this.config.entity = []; if (!_.isEmpty(this.entities)) {
_.forEach(this.entities, entity => { this.config.entity = [];
this.config.entity.push(entity.value); _.forEach(this.entities, entity => {
}); this.config.entity.push(entity.value);
});
}
this.fireEvent(this, 'config-changed', { config: this.config }); this.fireEvent(this, 'config-changed', { config: this.config });
} }
}; };
@ -70,37 +78,35 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
libraryItem.innerHTML = text; libraryItem.innerHTML = text;
return libraryItem; return libraryItem;
}; };
const createEntitiesDropdown = ( const createEntitiesDropdown = (selected: string, changeHandler: Function): HTMLElement | false => {
entitiesRegistry: Array<Record<string, any>>, if (this.entitiesRegistry) {
selected: string, const entitiesDropDown: any = document.createElement('paper-dropdown-menu');
changeHandler: Function const entities: any = document.createElement('paper-listbox');
): HTMLElement => {
const entitiesDropDown: any = document.createElement('paper-dropdown-menu'); _.forEach(this.entitiesRegistry, entityRegistry => {
const entities: any = document.createElement('paper-listbox'); if (
_.isEqual(entityRegistry.platform, 'cast') ||
_.forEach(entitiesRegistry, entityRegistry => { _.isEqual(entityRegistry.platform, 'kodi') ||
if ( _.isEqual(entityRegistry.platform, 'androidtv')
_.isEqual(entityRegistry.platform, 'cast') || ) {
_.isEqual(entityRegistry.platform, 'kodi') || entities.appendChild(addDropdownItem(entityRegistry.entity_id));
_.isEqual(entityRegistry.platform, 'androidtv') }
) { });
entities.appendChild(addDropdownItem(entityRegistry.entity_id)); entities.slot = 'dropdown-content';
} entitiesDropDown.label = 'Entity';
}); entitiesDropDown.value = selected;
entities.slot = 'dropdown-content'; entitiesDropDown.appendChild(entities);
entitiesDropDown.label = 'Entity'; entitiesDropDown.style.width = '100%';
entitiesDropDown.value = selected; entitiesDropDown.className = 'entitiesDropDown';
entitiesDropDown.appendChild(entities); entitiesDropDown.addEventListener('value-changed', changeHandler);
entitiesDropDown.style.width = '100%'; this.entities.push(entitiesDropDown);
entitiesDropDown.className = 'entitiesDropDown'; return entitiesDropDown;
entitiesDropDown.addEventListener('value-changed', changeHandler); }
this.entities.push(entitiesDropDown); return false;
return entitiesDropDown;
}; };
if (this.content) this.content.remove(); if (this.content) this.content.remove();
let entitiesRegistry: false | Array<Record<string, any>> = false; if (this.hassObj && !this.entitiesRegistry) {
if (this.hassObj) { this.entitiesRegistry = await fetchEntityRegistry(this.hassObj.connection);
entitiesRegistry = await fetchEntityRegistry(this.hassObj.connection);
} }
this.entities = []; this.entities = [];
@ -142,6 +148,19 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.libraryName.addEventListener('value-changed', this.valueUpdated); this.libraryName.addEventListener('value-changed', this.valueUpdated);
this.content.appendChild(this.libraryName); this.content.appendChild(this.libraryName);
this.appendChild(this.content);
// todo: do verify better, do not query plex every time
this.sections = [];
if (this.plex) {
try {
this.sections = await this.plex.getSections();
} catch (err) {
// pass
}
}
this.plexValidSection.style.display = 'none';
const devicesTitle = document.createElement('h2'); const devicesTitle = document.createElement('h2');
devicesTitle.innerHTML = `Devices Configuration`; devicesTitle.innerHTML = `Devices Configuration`;
devicesTitle.style.lineHeight = '29px'; devicesTitle.style.lineHeight = '29px';
@ -154,43 +173,42 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
addDeviceButton.style.cursor = 'pointer'; addDeviceButton.style.cursor = 'pointer';
addDeviceButton.innerHTML = '+'; addDeviceButton.innerHTML = '+';
addDeviceButton.addEventListener('click', () => { addDeviceButton.addEventListener('click', () => {
if (entitiesRegistry) { const entitiesDropdown = createEntitiesDropdown('', this.valueUpdated);
this.content.appendChild(createEntitiesDropdown(entitiesRegistry, '', this.valueUpdated)); if (entitiesDropdown) {
this.scrollTop = this.scrollHeight - this.clientHeight; this.content.appendChild(entitiesDropdown);
} }
}); });
devicesTitle.appendChild(addDeviceButton); devicesTitle.appendChild(addDeviceButton);
this.content.appendChild(devicesTitle); this.plexValidSection.innerHTML = '';
this.plexValidSection.appendChild(devicesTitle);
if (_.isString(this.config.entity)) { if (_.isString(this.config.entity)) {
this.config.entity = [this.config.entity]; this.config.entity = [this.config.entity];
} }
if (_.isArray(this.config.entity)) { if (_.isArray(this.config.entity)) {
_.forEach(this.config.entity, entity => { _.forEach(this.config.entity, entity => {
if (entitiesRegistry && _.isString(entity)) { if (_.isString(entity)) {
this.content.appendChild(createEntitiesDropdown(entitiesRegistry, entity, this.valueUpdated)); /*
const entitiesDropdown = createEntitiesDropdown(entity, this.valueUpdated);
if (entitiesDropdown) {
this.plexValidSection.appendChild(entitiesDropdown);
}
*/
} }
}); });
} }
if (!_.isEmpty(this.sections)) {
this.appendChild(this.content); _.forEach(this.sections, (section: Record<string, any>) => {
libraryItems.appendChild(addDropdownItem(section.title));
if (this.plex) { });
try { this.libraryName.disabled = false;
const sections = await this.plex.getSections(); this.libraryName.value = this.config.libraryName;
_.forEach(sections, (section: Record<string, any>) => { this.plexValidSection.style.display = 'block';
libraryItems.appendChild(addDropdownItem(section.title));
});
this.libraryName.disabled = false;
this.libraryName.value = this.config.libraryName;
} catch (err) {
// pass
}
} }
this.content.appendChild(this.plexValidSection);
}; };
setConfig = (config: Record<string, any>): void => { setConfig = (config: Record<string, any>): void => {
console.log(config);
this.config = JSON.parse(JSON.stringify(config)); this.config = JSON.parse(JSON.stringify(config));
if (config.port) { if (config.port) {

Loading…
Cancel
Save