Skip to content

Commit

Permalink
🚀Only update on relevant state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
iantrich committed Mar 19, 2019
1 parent 9788321 commit ce5fd6c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This card is for [Lovelace](https://www.home-assistant.io/lovelace) on [Home Ass
| ---- | ---- | ------- | -----------
| type | string | **Required** | `custom:config-template-card`
| config | object | **Required** | Card object
| entities | list | **Optional** | List of entity strings that should be watched for updates

## Installation

Expand Down Expand Up @@ -57,6 +58,11 @@ Add a custom element in your `ui-lovelace.yaml` or in the UI Editor as a Manual

```yaml
type: 'custom:config-template-card'
entities:
- light.bed_light
- cover.garage_door
- alarm_control_panel.ha_alarm
- climate.ecobee
config:
type: "${states['light.bed_light'].state === 'on' ? 'custom:hui-glance-card' : 'custom:hui-entities-card'}"
entities:
Expand Down
14 changes: 14 additions & 0 deletions dist/config-template-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,20 @@ let ConfigTemplateCard = class ConfigTemplateCard extends LitElement {
}
this._config = config;
}
shouldUpdate(changedProps) {
if (changedProps.has("_config") || !this._config.entities) {
return true;
}
const oldHass = changedProps.get("hass");
if (oldHass) {
let changed = false;
this._config.entities.forEach(entity => {
changed = changed || oldHass.states[entity] !== this.hass.states[entity];
});
return changed;
}
return true;
}
render() {
if (!this._config || !this.hass) {
return html ``;
Expand Down
22 changes: 21 additions & 1 deletion src/config-template-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
html,
customElement,
property,
TemplateResult
TemplateResult,
PropertyValues
} from "lit-element";
import deepClone from "deep-clone-simple";

Expand All @@ -24,6 +25,25 @@ class ConfigTemplateCard extends LitElement {
this._config = config;
}

protected shouldUpdate(changedProps: PropertyValues): boolean {
if (changedProps.has("_config") || !this._config!.entities) {
return true;
}

const oldHass = changedProps.get("hass") as HomeAssistant | undefined;

if (oldHass) {
let changed = false;
this._config!.entities.forEach(entity => {
changed = changed || oldHass.states[entity] !== this.hass!.states[entity]
});

return changed;
}

return true;
}

protected render(): TemplateResult | void {
if (!this._config || !this.hass) {
return html``;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {

export interface ConfigTemplateConfig {
type: string;
entities?: string[];
config?: any;
}

Expand Down

0 comments on commit ce5fd6c

Please sign in to comment.