Skip to content

Commit

Permalink
Merge pull request #80 from smonesi/master
Browse files Browse the repository at this point in the history
Apply templated style to elements, both for element and card
  • Loading branch information
iantrich authored Oct 4, 2021
2 parents 053f73a + d82010c commit d4a494a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ resources:
| card | object | **Optional** | Card configuration. (A card, row, or element configuaration must be provided) |
| row | object | **Optional** | Row configuration. (A card, row, or element configuaration must be provided) |
| element | object | **Optional** | Element configuration. (A card, row, or element configuaration must be provided) |
| style | object | **Optional** | Style configuration. (Required if you use an element) |
| style | object | **Optional** | Style configuration. |

### Available variables for templating

Expand Down Expand Up @@ -107,14 +107,17 @@ elements:
- states['light.bed_light'].state
entities:
- light.bed_light
- sensor.light_icon_color
element:
type: icon
icon: "${vars[0] === 'on' ? 'mdi:home' : 'mdi:circle'}"
style:
'--paper-item-icon-color': '${ states[''sensor.light_icon_color''].state }'
style:
top: 47%
left: 75%
```
Note how the `style` object is on the config-template-card itself and not within the element configuration.
The `style` object on the element configuration is applied to the element itself, the `style` object on the `config-template-card` is applied to the surrounding card, both can contain templated values. For example, in order to place the card properly, the `top` and `left` attributes must always be configured on the `config-template-card`.

### Entities card example

Expand Down
22 changes: 20 additions & 2 deletions src/config-template-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class ConfigTemplateCard extends LitElement {
);
}

if (config.element && !config.style) {
throw new Error('No style defined for element');
if (config.element && !config.element.type) {
throw new Error('No element type defined');
}

if (!config.entities) {
Expand Down Expand Up @@ -101,7 +101,12 @@ export class ConfigTemplateCard extends LitElement {
? deepClone(this._config.row)
: deepClone(this._config.element);

let style = this._config.style ? deepClone(this._config.style) : {};

config = this._evaluateConfig(config);
if (style) {
style = this._evaluateConfig(style);
}

const element = this._config.card
? this._helpers.createCardElement(config)
Expand All @@ -110,6 +115,19 @@ export class ConfigTemplateCard extends LitElement {
: this._helpers.createHuiElement(config);
element.hass = this.hass;

if (this._config.element) {
if (style) {
Object.keys(style).forEach(prop => {
this.style.setProperty(prop, style[prop]);
});
}
if (config.style) {
Object.keys(config.style).forEach(prop => {
element.style.setProperty(prop, config.style[prop]);
});
}
}

return html`
${element}
`;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export interface ConfigTemplateConfig {
card?: LovelaceCardConfig;
row?: EntitiesCardEntityConfig;
element?: LovelaceElementConfigBase;
style?: string[];
style?: Record<string, string>;
}

0 comments on commit d4a494a

Please sign in to comment.