Less clever, more stable. Really fix #37

This commit is contained in:
Thomas Lovén 2019-11-15 01:00:24 +01:00
parent 52b2a77bfa
commit bc73013f6d
4 changed files with 323 additions and 324 deletions

File diff suppressed because one or more lines are too long

View File

@ -92,6 +92,8 @@ export function entity_filter(hass, filter) {
break;
case "device":
if(!window.cardToolsData || !window.cardToolsData.devices)
return false;
let _deviceMatch = false;
for(const d of window.cardToolsData.devices) {
if (match(value, d.name)){
@ -103,6 +105,8 @@ export function entity_filter(hass, filter) {
break;
case "area":
if(!window.cardToolsData || !window.cardToolsData.areas)
return false;
let _areaMatch = false;
for (const a of window.cardToolsData.areas) {
if(match(value, a.name)) {

View File

@ -1,9 +1,10 @@
import { LitElement, html, css } from "card-tools/src/lit-element";
import "card-tools/src/card-maker";
import { entity_filter } from "./filter";
import { entity_sorter } from "./sort";
import { getData } from "card-tools/src/devices";
import { fireEvent } from "card-tools/src/event";
import { createCard } from "card-tools/src/lovelace-element";
import { hass } from "card-tools/src/hass";
class AutoEntities extends LitElement {
@ -18,15 +19,21 @@ class AutoEntities extends LitElement {
}
if(!this._config) {
this._config = config;
this.cardConfig = {entities: [], ...config.card};
this.entities = [];
this.hass = hass();
this._getEntities();
this.cardConfig = {entities: this.entities, ...config.card};
this.card = createCard(this.cardConfig);
} else {
this._config = config;
this.hass = this.hass;
}
// Reevaluate all filters once areas have been loaded
getData().then(() => this._getEntities());
}
async _getEntities()
_getEntities()
{
let entities = [];
// Start with any entities added by the `entities` parameter
@ -50,9 +57,6 @@ class AutoEntities extends LitElement {
entities.push(f);
continue;
}
if(f.device || f.area) {
await getData();
}
let add = all_entities.filter(entity_filter(this.hass, f))
.map((e) => new Object({...e, ...f.options}));
@ -94,7 +98,7 @@ class AutoEntities extends LitElement {
}
entities = newEntities;
}
return entities;
this.entities = entities;
}
set entities(ent) {
@ -121,7 +125,6 @@ class AutoEntities extends LitElement {
this.style.display = null;
this.style.margin = null;
}
this.requestUpdate();
}
}
get entities() {
@ -130,43 +133,37 @@ class AutoEntities extends LitElement {
set cardConfig(cardConfig) {
this._cardConfig = cardConfig;
if(this.querySelector("card-maker"))
this.querySelector("card-maker").config = cardConfig;
if(this.card)
this.card.setConfig(cardConfig);
}
get cardConfig() {
return this._cardConfig;
}
firstUpdated() {
this.cardConfig = this._cardConfig;
}
updated(changedProperties) {
if(changedProperties.has("hass") && this.hass) {
this.card.hass = this.hass;
// Run this in a timeout to improve performance
setTimeout(() => this._getEntities().then((e) => this.entities = e), 0);
setTimeout(() => this._getEntities(), 0);
}
}
createRenderRoot() {
return this;
}
render() {
return html`
<card-maker
.hass=${this.hass}
></card-maker>`;
${this.card}`;
}
getCardSize() {
let len = 0;
if(this.querySelector("card-maker") && this.querySelector("card-maker").getCardSize)
len = this.querySelector("card-maker").getCardSize();
if(this.card && this.card.getCardSize)
len = this.card.getCardSize();
if(len === 1 && this.entities.length)
len = this.entities.length;
if(len === 0 && this._config.filter && this._config.filter.include)
return Object.keys(this._config.filter.include).length;
len = Object.keys(this._config.filter.include).length;
return len || 1;
}
}