-
Notifications
You must be signed in to change notification settings - Fork 15
/
secondaryinfo-entity-row.js
62 lines (53 loc) · 2.42 KB
/
secondaryinfo-entity-row.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
customElements.whenDefined('card-tools').then(() => {
class SecondaryInfoEntityRow extends cardTools.LitElement {
version() { return "0.5"; }
render() {
return cardTools.LitHtml`
${this._wrappedElement}
`;
}
setConfig(config) {
cardTools.checkVersion(2.0);
this._config = config;
this._wrappedElement = this._createElement(config);
this.requestUpdate();
}
set hass(hass) {
this._hass = hass;
this._stateObj = this._config.entity in hass.states ? hass.states[this._config.entity] : null;
this._updateElement(this._wrappedElement, hass);
}
_createElement(config) {
// Override the custom row type in order to create the 'standard' row for this entity
let defaultRowConfig = Object.assign({}, config);
delete defaultRowConfig.type;
const element = cardTools.createEntityRow(defaultRowConfig);
return element;
}
async _updateElement(wrappedElement, hass) {
if (!wrappedElement) return;
this._wrappedElement.hass = hass;
await this._wrappedElement.updateComplete;
await this._wrappedElement.shadowRoot.querySelector("hui-generic-entity-row");
let secondaryInfoDiv = this._wrappedElement.shadowRoot.querySelector("hui-generic-entity-row").shadowRoot.querySelector(".secondary");
if (secondaryInfoDiv && this._config.secondary_info) {
let text;
if (this._config.secondary_info.includes('{{') || this._config.secondary_info.includes('{%')) {
text = await window.cardTools.parseTemplate(hass, this._config.secondary_info, {entity: this._config.entity})
} else {
text = window.cardTools.parseTemplate(this._config.secondary_info, {entity: this._config.entity});
}
secondaryInfoDiv.innerHTML = text;
}
}
}
customElements.define('secondaryinfo-entity-row', SecondaryInfoEntityRow);
});
setTimeout(() => {
if (customElements.get('card-tools')) return;
customElements.define('secondaryinfo-entity-row', class extends HTMLElement {
setConfig() {
throw new Error("Can't find card-tools. See https://github.com/thomasloven/lovelace-card-tools");
}
});
}, 2000);