Skip to content

Commit

Permalink
WIP: triggers_update with template support
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider committed Jul 30, 2023
1 parent f737107 commit 9d99936
Showing 1 changed file with 88 additions and 51 deletions.
139 changes: 88 additions & 51 deletions src/button-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,20 @@ class ButtonCard extends LitElement {

private _entities: string[] = [];

private _initial_setup_complete = false;
private _initialSetupComplete = false;

private get _doIHaveEverything(): boolean {
return !!this._hass && !!this._config && this.isConnected;
}

public set hass(hass: HomeAssistant) {
this._hass = hass;
Object.keys(this._cards).forEach((element) => {
const el = this._cards[element];
el.hass = this._hass;
});
if (!this._initial_setup_complete) {
this._initConnected();
if (!this._initialSetupComplete) {
this._finishSetup();
}
}

Expand All @@ -140,19 +144,87 @@ class ButtonCard extends LitElement {

public connectedCallback(): void {
super.connectedCallback();
if (!this._initial_setup_complete) {
this._initConnected();
if (!this._initialSetupComplete) {
this._finishSetup();
} else {
this._startTimerCountdown();
}
}

private _initConnected(): void {
if (this._hass === undefined) return;
if (this._config === undefined) return;
if (!this.isConnected) return;
this._initial_setup_complete = true;
this._startTimerCountdown();
private _evaluateVariablesNoError() {}

private _finishSetup(): void {
if (!this._initialSetupComplete && this._doIHaveEverything) {
this._evaledVariables = {};
if (this._config?.variables) {
const variablesNameOrdered = Object.keys(this._config.variables).sort();
variablesNameOrdered.forEach((variable) => {
try {
this._evaledVariables[variable] = this._objectEvalTemplate(undefined, this._config!.variables![variable]);
} catch (e) {}
});
}

if (this._config!.entity) {
const entityEvaled = this._getTemplateOrValue(undefined, this._config!.entity);
this._config!.entity = entityEvaled;
this._stateObj = this._hass!.states[entityEvaled];
}

this._evaledVariables = {};
if (this._config?.variables) {
const variablesNameOrdered = Object.keys(this._config.variables).sort();
variablesNameOrdered.forEach((variable) => {
try {
this._evaledVariables[variable] = this._objectEvalTemplate(undefined, this._config!.variables![variable]);
} catch (e) {}
});
}

if (this._config!.entity && DOMAINS_TOGGLE.has(computeDomain(this._config!.entity))) {
this._config = {
tap_action: { action: 'toggle' },
...this._config!,
};
} else if (this._config!.entity) {
this._config = {
tap_action: { action: 'more-info' },
...this._config!,
};
} else {
this._config = {
tap_action: { action: 'none' },
...this._config!,
};
}
this._config!.default_color = 'var(--primary-text-color)';

const jsonConfig = JSON.stringify(this._config);
this._entities = [];
if (Array.isArray(this._config!.triggers_update)) {
this._entities = [...this._config!.triggers_update];
} else if (typeof this._config!.triggers_update === 'string' && this._config!.triggers_update !== 'all') {
this._entities.push(this._config!.triggers_update);
}
if (this._config!.triggers_update !== 'all') {
const entitiesRxp = new RegExp(/states\[\s*('|\\")([a-zA-Z0-9_]+\.[a-zA-Z0-9_]+)\1\s*\]/, 'gm');
const entitiesRxp2 = new RegExp(/states\[\s*('|\\")([a-zA-Z0-9_]+\.[a-zA-Z0-9_]+)\1\s*\]/, 'm');
const matched = jsonConfig.match(entitiesRxp);
matched?.forEach((match) => {
const res = match.match(entitiesRxp2);
if (res && !this._entities.includes(res[2])) this._entities.push(res[2]);
});
}
if (this._config!.entity && !this._entities.includes(this._config!.entity))
this._entities.push(this._config!.entity);
this._expandTriggerGroups();

const rxp = new RegExp('\\[\\[\\[.*\\]\\]\\]', 'm');
this._hasTemplate = this._config!.triggers_update === 'all' && jsonConfig.match(rxp) ? true : false;

this._startTimerCountdown();
this._initialSetupComplete = true;
}
}

private _startTimerCountdown(): void {
Expand Down Expand Up @@ -1134,6 +1206,9 @@ class ButtonCard extends LitElement {
if (!config) {
throw new Error('Invalid configuration');
}
if (this._initialSetupComplete) {
this._initialSetupComplete = false;
}

this._cards = {};
this._cardsConfig = {};
Expand Down Expand Up @@ -1165,47 +1240,9 @@ class ButtonCard extends LitElement {
...template.lock,
},
};
if (this._config!.entity && DOMAINS_TOGGLE.has(computeDomain(this._config!.entity))) {
this._config = {
tap_action: { action: 'toggle' },
...this._config,
};
} else if (this._config!.entity) {
this._config = {
tap_action: { action: 'more-info' },
...this._config,
};
} else {
this._config = {
tap_action: { action: 'none' },
...this._config,
};
}
this._config!.default_color = 'var(--primary-text-color)';

const jsonConfig = JSON.stringify(this._config);
this._entities = [];
if (Array.isArray(this._config.triggers_update)) {
this._entities = [...this._config.triggers_update];
} else if (typeof this._config.triggers_update === 'string' && this._config.triggers_update !== 'all') {
this._entities.push(this._config.triggers_update);
}
if (this._config.triggers_update !== 'all') {
const entitiesRxp = new RegExp(/states\[\s*('|\\")([a-zA-Z0-9_]+\.[a-zA-Z0-9_]+)\1\s*\]/, 'gm');
const entitiesRxp2 = new RegExp(/states\[\s*('|\\")([a-zA-Z0-9_]+\.[a-zA-Z0-9_]+)\1\s*\]/, 'm');
const matched = jsonConfig.match(entitiesRxp);
matched?.forEach((match) => {
const res = match.match(entitiesRxp2);
if (res && !this._entities.includes(res[2])) this._entities.push(res[2]);
});
}
if (this._config.entity && !this._entities.includes(this._config.entity)) this._entities.push(this._config.entity);
this._expandTriggerGroups();

const rxp = new RegExp('\\[\\[\\[.*\\]\\]\\]', 'm');
this._hasTemplate = this._config.triggers_update === 'all' && jsonConfig.match(rxp) ? true : false;
if (!this._initial_setup_complete) {
this._initConnected();
if (!this._initialSetupComplete) {
this._finishSetup();
}
}

Expand Down

0 comments on commit 9d99936

Please sign in to comment.