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; break;
case "device": case "device":
if(!window.cardToolsData || !window.cardToolsData.devices)
return false;
let _deviceMatch = false; let _deviceMatch = false;
for(const d of window.cardToolsData.devices) { for(const d of window.cardToolsData.devices) {
if (match(value, d.name)){ if (match(value, d.name)){
@ -103,6 +105,8 @@ export function entity_filter(hass, filter) {
break; break;
case "area": case "area":
if(!window.cardToolsData || !window.cardToolsData.areas)
return false;
let _areaMatch = false; let _areaMatch = false;
for (const a of window.cardToolsData.areas) { for (const a of window.cardToolsData.areas) {
if(match(value, a.name)) { if(match(value, a.name)) {
@ -119,4 +123,4 @@ export function entity_filter(hass, filter) {
} }
return true; return true;
} }
} }

View File

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

View File

@ -63,4 +63,4 @@ export function entity_sorter(hass, method) {
return 0; return 0;
} }
} }
} }