forked from custom-cards/group-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group-card.js
33 lines (28 loc) · 1.09 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
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';
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;
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)) {
config.card.entities = entities;
}
this.lastChild.setConfig(config.card);
this.lastChild.hass = hass;
}
getCardSize() {
return 'getCardSize' in this.lastChild ? this.lastChild.getCardSize() : 1;
}
}
customElements.define('group-card', GroupCard);