From 4f62db145a0ade624f981894ac08051859fbe378 Mon Sep 17 00:00:00 2001 From: Steven Rollason <2099542+gadgetchnnel@users.noreply.github.com> Date: Mon, 25 Nov 2019 13:21:08 +0000 Subject: [PATCH] Added option to set state attributes when templating entity states --- custom_card.json | 2 +- lovelace-card-templater.js | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/custom_card.json b/custom_card.json index 597a16c..5b840db 100644 --- a/custom_card.json +++ b/custom_card.json @@ -1,6 +1,6 @@ { "lovelace-card-templater": { - "version": "0.0.5", + "version": "0.0.6b1", "remote_location": "https://raw.githubusercontent.com/gadgetchnnel/lovelace-card-templater/master/lovelace-card-templater.js", "visit_repo": "https://github.com/gadgetchnnel/lovelace-card-templater", "changelog": "https://github.com/gadgetchnnel/lovelace-card-templater" diff --git a/lovelace-card-templater.js b/lovelace-card-templater.js index 395f279..2f18fee 100644 --- a/lovelace-card-templater.js +++ b/lovelace-card-templater.js @@ -26,7 +26,7 @@ customElements.whenDefined('card-tools').then(() => { this.yaml = window.YAML; }); } - + createRenderRoot() { return this; } @@ -81,12 +81,14 @@ customElements.whenDefined('card-tools').then(() => { return false; } - getMockedState(stateObj, state){ + getMockedState(stateObj, state, attributes){ var newStateObj = {}; Object.assign(newStateObj, stateObj); newStateObj.attributes = {}; Object.assign(newStateObj.attributes, stateObj.attributes); - + if(attributes) { + Object.assign(newStateObj.attributes, attributes); + } newStateObj.state = state; return newStateObj; @@ -99,7 +101,8 @@ customElements.whenDefined('card-tools').then(() => { let stateObj = this._hass.states[entityConf.entity]; if(stateObj) { let state = await this.applyTemplate(entityConf.state_template); - let mockState = this.getMockedState(stateObj, state); + let attributes = entityConf.attributes ? await this.getCardConfig(entityConf.attributes) : null; + let mockState = this.getMockedState(stateObj, state, attributes); this._mockHass.states[entityConf.entity] = mockState; } }