From d1c143e71536525807158b901954209a0f4ae3be Mon Sep 17 00:00:00 2001 From: tmjo <54450177+tmjo@users.noreply.github.com> Date: Tue, 21 Feb 2023 04:36:52 +0100 Subject: [PATCH] Improve brand selection --- src/editor.ts | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/editor.ts b/src/editor.ts index 1de8556..47d0190 100644 --- a/src/editor.ts +++ b/src/editor.ts @@ -308,13 +308,21 @@ export class ChargerCardEditor extends ScopedRegistryHost(LitElement) implements private _setBrandTemplateDetails(ev): void { if (this != undefined && this._config != undefined && ev != null && ev.target !=null){ // SKIP EQUAL OR EMPTY BRAND CONFIG - if (this._config["brand"] == ev.target.value || ev.target.value == '') return; + if (this._config["brand"] == ev.target.value || ev.target.value == ''){ + console.info("Not changing config since brand is equal or empty."); + return; + } // SKIP EMPTY ENTITY, MUST BE SELECTED FIRST - if (this._config["entity"] === undefined || this._config["entity"] == '') return; + if (this._config["entity"] === undefined || this._config["entity"] == ''){ + console.info("Not changing config since entity is undefined or empty."); + return; + } - this._valueChanged(ev); const brand = ev.target.value; + console.info("Setting brand template for brand: " +brand +" entity_id: " +this._entity); + this._valueChanged(ev); + let entityprefix, serviceid; const cardtemplate = CARDTEMPLATES[CARDTEMPLATES.findIndex((cfg:template) => cfg.config.domain === brand)]; @@ -346,32 +354,32 @@ export class ChargerCardEditor extends ScopedRegistryHost(LitElement) implements // Set prefix by domain entityprefix = this._config.entity.split('.')[1].replace(cardtemplate.config.domainbase, ''); + //Make deepcopy to not overwrite actual template + let current_template = {...cardtemplate}; + // Replace template with actual data try { - let templateconfig_str = JSON.stringify(cardtemplate.details); + let templateconfig_str = JSON.stringify(current_template); templateconfig_str = this.replaceAll(templateconfig_str, cconst.TEMPLATE_EDITOR.ENTITYPREFIX, entityprefix); templateconfig_str = this.replaceAll(templateconfig_str, cconst.TEMPLATE_EDITOR.SERVICEID, this._config.entity); templateconfig_str = this.replaceAll(templateconfig_str, cconst.TEMPLATE_EDITOR.SERVICEID_DEVICE, serviceid); templateconfig_str = this.replaceAll(templateconfig_str, cconst.TEMPLATE_EDITOR.SERVICEID_ENTITY, serviceid); templateconfig_str = this.replaceAll(templateconfig_str, cconst.TEMPLATE_EDITOR.SERVICEID_STATE, serviceid); templateconfig_str = this.replaceAll(templateconfig_str, cconst.TEMPLATE_EDITOR.SERVICEID_ATTR, serviceid); - cardtemplate.details = JSON.parse(templateconfig_str); + current_template = JSON.parse(templateconfig_str); } catch (err) { console.error("Something went wrong with the default setup, please check your YAML configuration or enable debugging to see details.") } this.log("domain: " + brand +", entityprefix: " +entityprefix +", serviceid: " +serviceid); - this.log(cardtemplate); - - // Set config - const details:cardDetails = {}; - for (const data in cardtemplate.details) { - details[`${data}`] = cardtemplate.details[data]; - } + this.log(current_template); + + // CONFIG + // Merge current config with template defaults this._config = { ...this._config, ...cardtemplate.defaults}; - if(this._config !== undefined){ - this._config["details"] = { ...this._config.details, ...details }; - } + // Merge current config with replaced template details + // this._config["details"] = { ...this._config.details, ...current_template.details }; + this._config["details"] = {...current_template.details }; fireEvent(this, 'config-changed', { config: this._config }); return; }