forked from custom-cards/state-attribute-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state-attribute-element.js
40 lines (39 loc) · 1.26 KB
/
state-attribute-element.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
class StateAttributeElement extends HTMLElement {
set hass(hass) {
const entityId = this.config.entity;
const prefix_string = this.config.prefix || ''
const suffix_string = this.config.suffix || ''
const show_empty = this.config.show_empty
const attr = this.config.attribute
const sub_attribute = this.config.sub_attribute || ''
this.state = hass.states[entityId].attributes[attr]
if (this.config.sub_attribute) {
this.state = hass.states[entityId].attributes[attr][sub_attribute]
}
const card = document.createElement('state-attribute-element');
if (this.state) {
if (this.state.length != 0 || show_empty === true) {
this.innerHTML = `${prefix_string}${this.state}${suffix_string}`
}
}
else {
this.innerHTML = 'Attribute: ' + attr + ' does not exist in entity: ' + entityId + '.'
}
}
setConfig(config) {
if (!config.entity) {
throw new Error('You need to define an entity');
}
if (!config.attribute) {
throw new Error('You need to define an attribute');
}
if (!config.show_empty) {
config.show_empty = false;
}
this.config = config;
}
getCardSize() {
return 1;
}
}
customElements.define('state-attribute-element', StateAttributeElement);