Skip to content

Commit

Permalink
Merge pull request #109 from swiergot/master
Browse files Browse the repository at this point in the history
Dashboard variables
  • Loading branch information
iantrich authored Jan 20, 2023
2 parents 410ed24 + d07ba28 commit d616b6c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ type: 'custom:config-template-card'
name: '${ setTempMessage(currentTemp) }'
````
## Dashboard wide variables
If you need to use the same variable in multiple cards, then instead of defining it in each card's `variables` you can do that once for the entire dashboard.

```yaml
title: My dashboard
config_template_card_vars:
- states['sensor.light'].state
views:
```

Both arrays and objects are supported, just like in card's local variables. It is allowed to mix the two types, i.e. use an array in dashboard variables and an object in card variables, or the other way around. If both definitions are arrays, then dashboard variables are put first in `vars`. In the mixed mode, `vars` have array indices and as well as variable names.

### Note: All templates must be enclosed by `${}`

[Troubleshooting](https://github.com/thomasloven/hass-config/wiki/Lovelace-Plugins)
Expand Down
68 changes: 52 additions & 16 deletions src/config-template-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ export class ConfigTemplateCard extends LitElement {
this.loadCardHelpers();
}

private getLovelacePanel() {
const ha = document.querySelector("home-assistant");

if (ha && ha.shadowRoot) {
const haMain = ha.shadowRoot.querySelector("home-assistant-main");

if (haMain && haMain.shadowRoot) {
return haMain.shadowRoot.querySelector('ha-panel-lovelace');
}
}

return null
}

private getLovelaceConfig() {
const panel = this.getLovelacePanel() as any;

if (panel && panel.lovelace && panel.lovelace.config && panel.lovelace.config.config_template_card_vars) {
return panel.lovelace.config.config_template_card_vars
}

return {}
}

protected shouldUpdate(changedProps: PropertyValues): boolean {
if (!this._initialized) {
this._initialize();
Expand Down Expand Up @@ -194,29 +218,41 @@ export class ConfigTemplateCard extends LitElement {
/* eslint-disable @typescript-eslint/no-unused-vars */
const user = this.hass ? this.hass.user : undefined;
const states = this.hass ? this.hass.states : undefined;
let vars: any[] | { [key: string]: any };
const vars: any[] = [];
const namedVars: { [key: string]: any } = {};
const arrayVars: string[] = [];
let varDef = '';

if (this._config) {
if (Array.isArray(this._config.variables)) {
// if variables are an array, create vars as an array
vars = [];
for (const v in this._config.variables) {
const newV = eval(this._config.variables[v]);
vars.push(newV);
}
arrayVars.push(...this._config.variables);
} else {
// if it is an object, then create a key-value map containing
// the values
vars = {};
for (const varName in this._config.variables) {
const newV = eval(this._config.variables[varName]);
vars[varName] = newV;
// create variable definitions to be injected:
varDef = varDef + `var ${varName} = vars['${varName}'];\n`;
}
Object.assign(namedVars, this._config.variables);
}
}

const localVars = this.getLovelaceConfig();

if (localVars) {
if (Array.isArray(localVars)) {
arrayVars.push(...localVars);
} else {
Object.assign(namedVars, localVars);
}
}

for (const v in arrayVars) {
const newV = eval(arrayVars[v]);
vars.push(newV);
}

for (const varName in namedVars) {
const newV = eval(namedVars[varName]);
vars[varName] = newV;
// create variable definitions to be injected:
varDef = varDef + `var ${varName} = vars['${varName}'];\n`;
}

return eval(varDef + template.substring(2, template.length - 1));
}
}

0 comments on commit d616b6c

Please sign in to comment.