Love: Allow custom title per entity in entities card (#1374)

* Allow custom title per entity in entities card

* Change entityConfig to config

* Only pass title to state-cards
This commit is contained in:
Jerad Meisner 2018-07-01 08:28:25 -07:00 committed by Paulus Schoutsen
parent 1a9af5595f
commit 54fcb21917
15 changed files with 99 additions and 19 deletions

View File

@ -82,12 +82,13 @@ class StateInfo extends PolymerElement {
}, },
hass: Object, hass: Object,
stateObj: Object, stateObj: Object,
inDialog: Boolean inDialog: Boolean,
overrideName: String
}; };
} }
computeStateName(stateObj) { computeStateName(stateObj) {
return computeStateName(stateObj); return this.overrideName || computeStateName(stateObj);
} }
} }

View File

@ -5,6 +5,8 @@ import { PolymerElement } from '@polymer/polymer/polymer-element.js';
import stateCardType from '../../../common/entity/state_card_type.js'; import stateCardType from '../../../common/entity/state_card_type.js';
import computeDomain from '../../../common/entity/compute_domain.js'; import computeDomain from '../../../common/entity/compute_domain.js';
import { DOMAINS_HIDE_MORE_INFO } from '../../../common/const.js'; import { DOMAINS_HIDE_MORE_INFO } from '../../../common/const.js';
import computeConfigEntities from '../common/compute-config-entities';
import validateEntitiesConfig from '../common/validate-entities-config';
import '../../../components/ha-card.js'; import '../../../components/ha-card.js';
import '../components/hui-entities-toggle.js'; import '../components/hui-entities-toggle.js';
@ -98,6 +100,10 @@ class HuiEntitiesCard extends EventsMixin(PolymerElement) {
} }
setConfig(config) { setConfig(config) {
if (!validateEntitiesConfig(config)) {
throw Error('Error in card config.');
}
this._config = config; this._config = config;
if (this.$) this._buildConfig(); if (this.$) this._buildConfig();
} }
@ -105,6 +111,7 @@ class HuiEntitiesCard extends EventsMixin(PolymerElement) {
_buildConfig() { _buildConfig() {
const config = this._config; const config = this._config;
const root = this.$.states; const root = this.$.states;
const entities = computeConfigEntities(config);
while (root.lastChild) { while (root.lastChild) {
root.removeChild(root.lastChild); root.removeChild(root.lastChild);
@ -112,8 +119,9 @@ class HuiEntitiesCard extends EventsMixin(PolymerElement) {
this._elements = []; this._elements = [];
for (let i = 0; i < config.entities.length; i++) { for (let i = 0; i < entities.length; i++) {
const entityId = config.entities[i]; const entity = entities[i];
const entityId = entity.entity;
if (!(entityId in this.hass.states)) continue; if (!(entityId in this.hass.states)) continue;
const stateObj = this.hass.states[entityId]; const stateObj = this.hass.states[entityId];
const tag = stateObj ? `state-card-${stateCardType(this.hass, stateObj)}` : 'state-card-display'; const tag = stateObj ? `state-card-${stateCardType(this.hass, stateObj)}` : 'state-card-display';
@ -124,6 +132,9 @@ class HuiEntitiesCard extends EventsMixin(PolymerElement) {
} }
element.stateObj = stateObj; element.stateObj = stateObj;
element.hass = this.hass; element.hass = this.hass;
if (entity.title) {
element.overrideName = entity.title;
}
this._elements.push({ entityId, element }); this._elements.push({ entityId, element });
const container = document.createElement('div'); const container = document.createElement('div');
container.appendChild(element); container.appendChild(element);

View File

@ -30,7 +30,12 @@ class StateCardClimate extends PolymerElement {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -42,6 +47,7 @@ class StateCardClimate extends PolymerElement {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
overrideName: String
}; };
} }
} }

View File

@ -38,7 +38,12 @@ class StateCardConfigurator extends LocalizeMixin(PolymerElement) {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -50,6 +55,7 @@ class StateCardConfigurator extends LocalizeMixin(PolymerElement) {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
overrideName: String
}; };
} }

View File

@ -29,7 +29,12 @@ class StateCardCover extends PolymerElement {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -45,6 +50,7 @@ class StateCardCover extends PolymerElement {
type: Object, type: Object,
computed: 'computeEntityObj(hass, stateObj)', computed: 'computeEntityObj(hass, stateObj)',
}, },
overrideName: String
}; };
} }

View File

@ -46,7 +46,12 @@ class StateCardDisplay extends LocalizeMixin(PolymerElement) {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -58,6 +63,7 @@ class StateCardDisplay extends LocalizeMixin(PolymerElement) {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
overrideName: String
}; };
} }

View File

@ -51,7 +51,12 @@ class StateCardInputNumber extends mixinBehaviors([
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -108,7 +113,8 @@ class StateCardInputNumber extends mixinBehaviors([
}, },
mode: { mode: {
type: String, type: String,
} },
overrideName: String
}; };
} }

View File

@ -60,11 +60,12 @@ class StateCardInputSelect extends PolymerElement {
type: String, type: String,
observer: 'selectedOptionChanged', observer: 'selectedOptionChanged',
}, },
overrideName: String
}; };
} }
_computeStateName(stateObj) { _computeStateName(stateObj) {
return computeStateName(stateObj); return this.overrideName || computeStateName(stateObj);
} }
computeSelected(stateObj) { computeSelected(stateObj) {

View File

@ -25,7 +25,12 @@ class StateCardInputText extends PolymerElement {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -45,7 +50,8 @@ class StateCardInputText extends PolymerElement {
}, },
pattern: String, pattern: String,
value: String value: String,
overrideName: String
}; };
} }

View File

@ -53,7 +53,12 @@ class StateCardMediaPlayer extends LocalizeMixin(PolymerElement) {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -69,6 +74,7 @@ class StateCardMediaPlayer extends LocalizeMixin(PolymerElement) {
type: Object, type: Object,
computed: 'computePlayerObj(hass, stateObj)', computed: 'computePlayerObj(hass, stateObj)',
}, },
overrideName: String
}; };
} }

View File

@ -32,7 +32,12 @@ class StateCardScene extends LocalizeMixin(PolymerElement) {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -44,6 +49,7 @@ class StateCardScene extends LocalizeMixin(PolymerElement) {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
overrideName: String
}; };
} }

View File

@ -43,7 +43,12 @@ class StateCardScript extends LocalizeMixin(PolymerElement) {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -55,6 +60,7 @@ class StateCardScript extends LocalizeMixin(PolymerElement) {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
overrideName: String
}; };
} }

View File

@ -36,7 +36,12 @@ class StateCardTimer extends LocalizeMixin(PolymerElement) {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -52,6 +57,7 @@ class StateCardTimer extends LocalizeMixin(PolymerElement) {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
overrideName: String
}; };
} }

View File

@ -25,7 +25,12 @@ class StateCardToggle extends PolymerElement {
static get stateInfoTemplate() { static get stateInfoTemplate() {
return html` return html`
<state-info hass="[[hass]]" state-obj="[[stateObj]]" in-dialog="[[inDialog]]"></state-info> <state-info
hass="[[hass]]"
state-obj="[[stateObj]]"
in-dialog="[[inDialog]]"
override-name="[[overrideName]]">
</state-info>
`; `;
} }
@ -37,6 +42,7 @@ class StateCardToggle extends PolymerElement {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
overrideName: String
}; };
} }
} }

View File

@ -41,6 +41,7 @@ class StateCardWeblink extends PolymerElement {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
overrideName: String
}; };
} }
@ -50,7 +51,7 @@ class StateCardWeblink extends PolymerElement {
} }
_computeStateName(stateObj) { _computeStateName(stateObj) {
return computeStateName(stateObj); return this.overrideName || computeStateName(stateObj);
} }
onTap(ev) { onTap(ev) {