-
Notifications
You must be signed in to change notification settings - Fork 9
/
group-card.js
61 lines (55 loc) · 2.01 KB
/
group-card.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
class GroupCard extends HTMLElement {
setConfig(config) {
if (!config.group) {
throw new Error('Please specify a group');
}
if (this.lastChild) this.removeChild(this.lastChild);
const cardConfig = Object.assign({}, config);
if (!cardConfig.card) cardConfig.card = {};
if (!cardConfig.card.type) cardConfig.card.type = 'entities';
if (!cardConfig.entities_vars) cardConfig.entities_vars = { type: 'entity' };
const element = document.createElement(`hui-${cardConfig.card.type}-card`);
this.appendChild(element);
this._config = JSON.parse(JSON.stringify(cardConfig));
}
set hass(hass) {
const config = this._config;
if (!hass.states[config.group]) {
throw new Error(`${config.group} not found`);
}
const entities = hass.states[config.group].attributes['entity_id'];
if (!config.card.entities || config.card.entities.length !== entities.length ||
!config.card.entities.every((value, index) => value.entity === entities[index].entity)) {
if (!config.row) {
config.card.entities = entities;
}
else {
const fmtentities = [];
entities.forEach(function (item) {
const stateObj = new Object;
for (var k in config.row)
stateObj[k] = config.row[k];
stateObj.entity = item;
fmtentities.push(stateObj);
});
config.card.entities = fmtentities;
if (config.card.type == "entities" || config.card.type == "glance") {
config.card.entities = entities;
} else {
config.card.cards = []
for (const entity of entities) {
const card = JSON.parse(JSON.stringify(config.entities_vars));
card.entity = entity
config.card.cards.push(card)
}
}
}
this.lastChild.setConfig(config.card);
this.lastChild.hass = hass;
}
}
getCardSize() {
return 'getCardSize' in this.lastChild ? this.lastChild.getCardSize() : 1;
}
}
customElements.define('group-card', GroupCard);