Skip to content

Commit

Permalink
Create group-card.js
Browse files Browse the repository at this point in the history
  • Loading branch information
iantrich authored May 17, 2019
1 parent 995b179 commit 0680a5d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions group-card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 = 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);

0 comments on commit 0680a5d

Please sign in to comment.