Less clever, more stable. Really fix #37
This commit is contained in:
1 parent
52b2a77bfa
commit
bc73013f6d
4 files changed
+323
-324
No files matched your search
+2
-4
@@ -1,4 +1,2 @@
|
|||||||
!function(t){var e={};function r(i){if(e[i])return e[i].exports;var s=e[i]={i:i,l:!1,exports:{}};return t[i].call(s.exports,s,s.exports,r),s.l=!0,s.exports}r.m=t,r.c=e,r.d=function(t,e,i){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(r.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)r.d(i,s,function(e){return t[e]}.bind(null,s));return i},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";r.r(e);const i=customElements.get("home-assistant-main")?Object.getPrototypeOf(customElements.get("home-assistant-main")):Object.getPrototypeOf(customElements.get("hui-view")),s=i.prototype.html;i.prototype.css;function n(t,e,r=null){if((t=new Event(t,{bubbles:!0,cancelable:!1,composed:!0})).detail=e||{},r)r.dispatchEvent(t);else{var i=document.querySelector("home-assistant");(i=(i=(i=(i=(i=(i=(i=(i=(i=(i=(i=i&&i.shadowRoot)&&i.querySelector("home-assistant-main"))&&i.shadowRoot)&&i.querySelector("app-drawer-layout partial-panel-resolver"))&&i.shadowRoot||i)&&i.querySelector("ha-panel-lovelace"))&&i.shadowRoot)&&i.querySelector("hui-root"))&&i.shadowRoot)&&i.querySelector("ha-app-layout #view"))&&i.firstElementChild)&&i.dispatchEvent(t)}}const o="custom:";function a(t,e){const r=document.createElement("hui-error-card");return r.setConfig({type:"error",error:t,origConfig:e}),r}function c(t,e){if(!e||"object"!=typeof e||!e.type)return a(`No ${t} type configured`,e);let r=e.type;if(r=r.startsWith(o)?r.substr(o.length):`hui-${r}-${t}`,customElements.get(r))return function(t,e){const r=document.createElement(t);try{r.setConfig(e)}catch(t){return a(t,e)}return r}(r,e);const i=a(`Custom element doesn't exist: ${r}.`,e);i.style.display="None";const s=setTimeout(()=>{i.style.display=""},2e3);return customElements.whenDefined(r).then(()=>{clearTimeout(s),n("ll-rebuild",{},i)}),i}function u(){return document.querySelector("home-assistant").hass}const l=2;class f extends i{static get version(){return l}static get properties(){return{noHass:{type:Boolean}}}setConfig(t){var e;this._config=t,this.el?this.el.setConfig(t):(this.el=this.create(t),this._hass&&(this.el.hass=this._hass),this.noHass&&(e=this,document.querySelector("home-assistant").provideHass(e)))}set config(t){this.setConfig(t)}set hass(t){this._hass=t,this.el&&(this.el.hass=t)}createRenderRoot(){return this}render(){return s`${this.el}`}}const d=function(t,e){const r=Object.getOwnPropertyDescriptors(e.prototype);for(const[e,i]of Object.entries(r))"constructor"!==e&&Object.defineProperty(t.prototype,e,i);const i=Object.getOwnPropertyDescriptors(e);for(const[e,r]of Object.entries(i))"prototype"!==e&&Object.defineProperty(t,e,r);const s=Object.getPrototypeOf(e),n=Object.getOwnPropertyDescriptors(s.prototype);for(const[e,r]of Object.entries(n))"constructor"!==e&&Object.defineProperty(Object.getPrototypeOf(t).prototype,e,r);const o=Object.getOwnPropertyDescriptors(s);for(const[e,r]of Object.entries(o))"prototype"!==e&&Object.defineProperty(Object.getPrototypeOf(t),e,r)},h=customElements.get("card-maker");if(!h||!h.version||h.version<l){class t extends f{create(t){return function(t){return c("card",t)}(t)}getCardSize(){return this.firstElementChild&&this.firstElementChild.getCardSize?this.firstElementChild.getCardSize():1}}h?d(h,t):customElements.define("card-maker",t)}const p=customElements.get("element-maker");if(!p||!p.version||p.version<l){class t extends f{create(t){return function(t){return c("element",t)}(t)}}p?d(p,t):customElements.define("element-maker",t)}const y=customElements.get("entity-row-maker");if(!y||!y.version||y.version<l){class t extends f{create(t){return function(t){const e=new Set(["call-service","divider","section","weblink"]);if(!t)return a("Invalid configuration given.",t);if("string"==typeof t&&(t={entity:t}),"object"!=typeof t||!t.entity&&!t.type)return a("Invalid configuration given.",t);const r=t.type||"default";if(e.has(r)||r.startsWith(o))return c("row",t);const i=t.entity.split(".",1)[0];return Object.assign(t,{type:{alert:"toggle",automation:"toggle",climate:"climate",cover:"cover",fan:"toggle",group:"group",input_boolean:"toggle",input_number:"input-number",input_select:"input-select",input_text:"input-text",light:"toggle",lock:"lock",media_player:"media-player",remote:"toggle",scene:"scene",script:"script",sensor:"sensor",timer:"timer",switch:"toggle",vacuum:"toggle",water_heater:"climate",input_datetime:"input-datetime"}[i]||"text"}),c("entity-row",t)}(t)}}y?d(y,t):customElements.define("entity-row-makerLine truncated
|
!function(t){var e={};function i(s){if(e[s])return e[s].exports;var r=e[s]={i:s,l:!1,exports:{}};return t[s].call(r.exports,r,r.exports,i),r.l=!0,r.exports}i.m=t,i.c=e,i.d=function(t,e,s){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:s})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var s=Object.create(null);if(i.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)i.d(s,r,function(e){return t[e]}.bind(null,r));return s},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){"use strict";i.r(e);const s=customElements.get("home-assistant-main")?Object.getPrototypeOf(customElements.get("home-assistant-main")):Object.getPrototypeOf(customElements.get("hui-view")),r=s.prototype.html;s.prototype.css;function n(){return document.querySelector("home-assistant").hass}const o=n().callWS({type:"config/area_registry/list"}),a=n().callWS({type:"config/device_registry/list"}),c=n().callWS({type:"config/entity_registry/list"});async function u(){return window.cardToolsData=window.cardToolsData||{areas:await o,devices:await a,entities:await c},window.cardToolsData}function l(t){const e=window.cardToolsData;let i=[];if(!t)return i;for(const s of e.devices)s.area_id===t.area_id&&i.push(s);return i}function f(t){const e=window.cardToolsData;let i=[];if(!t)return i;for(const s of e.entities)s.device_id===t.id&&i.push(s.entity_id);return i}function d(t,e){if("string"==typeof e&&"string"==typeof t&&(t.startsWith("/")&&t.endsWith("/")||-1!==t.indexOf("*"))){return t.startsWith("/")||(t=`/^${t=t.replace(/\./g,".").replace(/\*/g,".*")}$/`),new RegExp(t.slice(1,-1)).test(e)}if("string"==typeof t){if(t.startsWith("<="))return parseFloat(e)<=parseFloat(t.substr(2));if(t.startsWith(">="))return parseFloat(e)>=parseFloat(t.substr(2));if(t.startsWith("<"))return parseFloat(e)<parseFloat(t.substr(1));if(t.startsWith(">"))return parseFloat(e)>parseFloat(t.substr(1));if(t.startsWith("!"))return parseFloat(e)!=parseFloat(t.substr(1));if(t.startsWith("="))return parseFloat(e)==parseFloat(t.substr(1))}return t===e}function h(t,e){return function(i){const s="string"==typeof i?t.states[i]:t.states[i.entity];if(!i)return!1;for(const[r,n]of Object.entries(e))switch(r.split(" ")[0]){case"options":case"sort":break;case"domain":if(!d(n,s.entity_id.split(".")[0]))return!1;break;case"entity_id":if(!d(n,s.entity_id))return!1;break;case"state":if(!d(n,s.state))return!1;break;case"name":if(!s.attributes.friendly_name||!d(n,s.attributes.friendly_name))return!1;break;case"group":if(!(n.startsWith("group.")&&t.states[n]&&t.states[n].attributes.entity_id&&t.states[n].attributes.entity_id.includes(s.entity_id)))return!1;break;case"attributes":for(const[t,e]of Object.entries(n)){let i=t.split(" ")[0],r=s.attributes;for(;i&&r;){let t;[t,i]=i.split(":"),r=r[t]}if(void 0===r||e&&!d(e,r))return!1}break;case"not":if(h(t,n)(i))return!1;break;case"device":if(!window.cardToolsData||!window.cardToolsData.devices)return!1;let e=!1;for(const t of window.cardToolsData.devices)d(n,t.name)&&f(t).includes(s.entity_id)&&(e=!0);if(!e)return!1;break;case"area":if(!window.cardToolsData||!window.cardToolsData.areas)return!1;let r=!1;for(const t of window.cardToolsData.areas)d(n,t.name)&&l(t).flatMap(f).includes(s.entity_id)&&(r=!0);if(!r)return!1;break;default:return!1}return!0}}function y(t,e){return"string"==typeof e&&(e={method:e}),function(i,s){const r="string"==typeof i?t.states[i]:t.states[i.entity],n="string"==typeof s?t.states[s]:t.states[s.entity];if(void 0===r||void 0===n)return 0;const[o,a]=e.reverse?[-1,1]:[1,-1];function c(t,i){return e.ignore_case&&t.toLowerCase&&(t=t.toLowerCase()),e.ignore_case&&i.toLowerCase&&(i=i.toLowerCase()),void 0===t&&void 0===i?0:void 0===t?o:void 0===i?a:t<i?a:t>i?o:0}switch(e.method){case"domain":return c(r.entity_id.split(".")[0],n.entity_id.split(".")[0]);case"entity_id":return c(r.entity_id,n.entity_id);case"friendly_name":case"name":return c(r.attributes.friendly_name||r.entity_id.split(".")[1],n.attributes.friendly_name||n.entity_id.split(".")[1]);case"state":return c(r.state,n.state);case"attribute":let t=r.attributes,i=n.attributes,s=e.attribute;for(;s;){let e;if([e,s]=s.split(":"),t=t[e],i=i[e],void 0===t&&void 0===i)return 0;if(void 0===t)return o;if(void 0===i)return a}return c(t,i);default:return 0}}}function p(t,e,i=null){if((t=new Event(t,{bubbles:!0,cancelable:!1,composed:!0})).detail=e||{},i)i.dispatchEvent(t);else{var s=document.querySelector("home-assistant");(s=(s=(s=(s=(s=(s=(s=(s=(s=(s=(s=s&&s.shadowRoot)&&s.querySelector("home-assistant-main"))&&s.shadowRoot)&&s.querySelector(Line truncated
|
||||||
<card-maker
|
${this.card}`}getCardSize(){let t=0;return this.card&&this.card.getCardSize&&(t=this.card.getCardSize()),1===t&&this.entities.length&&(t=this.entities.length),0===t&&this._config.filter&&this._config.filter.include&&(t=Object.keys(this._config.filter.include).length),t||1}}),p("ll-rebuild",{})}]);
|
||||||
.hass=${this.hass}
|
|
||||||
></card-maker>`}getCardSize(){let t=0;return this.querySelector("card-maker")&&this.querySelector("card-maker").getCardSize&&(t=this.querySelector("card-maker").getCardSize()),1===t&&this.entities.length&&(t=this.entities.length),0===t&&this._config.filter&&this._config.filter.include?Object.keys(this._config.filter.include).length:t||1}}),n("ll-rebuild",{})}]);
|
|
||||||
+108
-104
@@ -1,122 +1,126 @@
|
|||||||
import { areaByName, areaDevices, deviceByName, deviceEntities } from "card-tools/src/devices";
|
import { areaByName, areaDevices, deviceByName, deviceEntities } from "card-tools/src/devices";
|
||||||
|
|
||||||
function match(pattern, value) {
|
function match(pattern, value) {
|
||||||
if(typeof(value) === "string" && typeof(pattern) === "string") {
|
if(typeof(value) === "string" && typeof(pattern) === "string") {
|
||||||
if((pattern.startsWith('/') && pattern.endsWith('/')) || pattern.indexOf('*') !== -1) {
|
if((pattern.startsWith('/') && pattern.endsWith('/')) || pattern.indexOf('*') !== -1) {
|
||||||
if(!pattern.startsWith('/')) { // Convert globs to regex
|
if(!pattern.startsWith('/')) { // Convert globs to regex
|
||||||
pattern = pattern
|
pattern = pattern
|
||||||
.replace(/\./g, '\.')
|
.replace(/\./g, '\.')
|
||||||
.replace(/\*/g, '.*');
|
.replace(/\*/g, '.*');
|
||||||
pattern = `/^${pattern}$/`;
|
pattern = `/^${pattern}$/`;
|
||||||
}
|
}
|
||||||
let regex = new RegExp(pattern.slice(1,-1));
|
let regex = new RegExp(pattern.slice(1,-1));
|
||||||
return regex.test(value);
|
return regex.test(value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(typeof(pattern) === "string") {
|
if(typeof(pattern) === "string") {
|
||||||
// Comparisons assume numerical values
|
// Comparisons assume numerical values
|
||||||
if(pattern.startsWith("<=")) return parseFloat(value) <= parseFloat(pattern.substr(2));
|
if(pattern.startsWith("<=")) return parseFloat(value) <= parseFloat(pattern.substr(2));
|
||||||
if(pattern.startsWith(">=")) return parseFloat(value) >= parseFloat(pattern.substr(2));
|
if(pattern.startsWith(">=")) return parseFloat(value) >= parseFloat(pattern.substr(2));
|
||||||
if(pattern.startsWith("<")) return parseFloat(value) < parseFloat(pattern.substr(1));
|
if(pattern.startsWith("<")) return parseFloat(value) < parseFloat(pattern.substr(1));
|
||||||
if(pattern.startsWith(">")) return parseFloat(value) > parseFloat(pattern.substr(1));
|
if(pattern.startsWith(">")) return parseFloat(value) > parseFloat(pattern.substr(1));
|
||||||
if(pattern.startsWith("!")) return parseFloat(value) != parseFloat(pattern.substr(1));
|
if(pattern.startsWith("!")) return parseFloat(value) != parseFloat(pattern.substr(1));
|
||||||
if(pattern.startsWith("=")) return parseFloat(value) == parseFloat(pattern.substr(1));
|
if(pattern.startsWith("=")) return parseFloat(value) == parseFloat(pattern.substr(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
return pattern === value;
|
return pattern === value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function entity_filter(hass, filter) {
|
export function entity_filter(hass, filter) {
|
||||||
return function(e) {
|
return function(e) {
|
||||||
const entity = typeof(e) === "string"
|
const entity = typeof(e) === "string"
|
||||||
? hass.states[e]
|
? hass.states[e]
|
||||||
: hass.states[e.entity];
|
: hass.states[e.entity];
|
||||||
if(!e) return false;
|
if(!e) return false;
|
||||||
for (const [key, value] of Object.entries(filter)) {
|
for (const [key, value] of Object.entries(filter)) {
|
||||||
switch(key.split(" ")[0]) {
|
switch(key.split(" ")[0]) {
|
||||||
case "options":
|
case "options":
|
||||||
case "sort":
|
case "sort":
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "domain":
|
case "domain":
|
||||||
if(!match(value, entity.entity_id.split('.')[0]))
|
if(!match(value, entity.entity_id.split('.')[0]))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "entity_id":
|
case "entity_id":
|
||||||
if(!match(value, entity.entity_id))
|
if(!match(value, entity.entity_id))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "state":
|
case "state":
|
||||||
if(!match(value, entity.state))
|
if(!match(value, entity.state))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "name":
|
case "name":
|
||||||
if(!entity.attributes.friendly_name
|
if(!entity.attributes.friendly_name
|
||||||
|| !match(value, entity.attributes.friendly_name))
|
|| !match(value, entity.attributes.friendly_name))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "group":
|
case "group":
|
||||||
if(!value.startsWith("group.")
|
if(!value.startsWith("group.")
|
||||||
|| !hass.states[value]
|
|| !hass.states[value]
|
||||||
|| !hass.states[value].attributes.entity_id
|
|| !hass.states[value].attributes.entity_id
|
||||||
|| !hass.states[value].attributes.entity_id.includes(entity.entity_id)
|
|| !hass.states[value].attributes.entity_id.includes(entity.entity_id)
|
||||||
)
|
)
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "attributes":
|
case "attributes":
|
||||||
for(const [k, v] of Object.entries(value)) {
|
for(const [k, v] of Object.entries(value)) {
|
||||||
let attr = k.split(" ")[0];
|
let attr = k.split(" ")[0];
|
||||||
let entityAttribute = entity.attributes;
|
let entityAttribute = entity.attributes;
|
||||||
while(attr && entityAttribute) {
|
while(attr && entityAttribute) {
|
||||||
let step;
|
let step;
|
||||||
[step, attr] = attr.split(":");
|
[step, attr] = attr.split(":");
|
||||||
entityAttribute = entityAttribute[step];
|
entityAttribute = entityAttribute[step];
|
||||||
}
|
|
||||||
if(entityAttribute === undefined
|
|
||||||
|| (v && !match(v, entityAttribute))
|
|
||||||
)
|
|
||||||
return false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "not":
|
|
||||||
if(entity_filter(hass,value)(e))
|
|
||||||
return false;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "device":
|
|
||||||
let _deviceMatch = false;
|
|
||||||
for(const d of window.cardToolsData.devices) {
|
|
||||||
if (match(value, d.name)){
|
|
||||||
if(deviceEntities(d).includes(entity.entity_id))
|
|
||||||
_deviceMatch = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!_deviceMatch) return false;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "area":
|
|
||||||
let _areaMatch = false;
|
|
||||||
for (const a of window.cardToolsData.areas) {
|
|
||||||
if(match(value, a.name)) {
|
|
||||||
if(areaDevices(a).flatMap(deviceEntities).includes(entity.entity_id))
|
|
||||||
_areaMatch = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!_areaMatch) return false;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
if(entityAttribute === undefined
|
||||||
|
|| (v && !match(v, entityAttribute))
|
||||||
|
)
|
||||||
|
return false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "not":
|
||||||
|
if(entity_filter(hass,value)(e))
|
||||||
|
return false;
|
||||||
|
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)){
|
||||||
|
if(deviceEntities(d).includes(entity.entity_id))
|
||||||
|
_deviceMatch = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!_deviceMatch) return false;
|
||||||
|
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)) {
|
||||||
|
if(areaDevices(a).flatMap(deviceEntities).includes(entity.entity_id))
|
||||||
|
_areaMatch = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!_areaMatch) return false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+150
-153
@@ -1,174 +1,171 @@
|
|||||||
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 {
|
||||||
|
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
hass: {},
|
hass: {},
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
setConfig(config) {
|
||||||
|
if(!config || !config.card) {
|
||||||
|
throw new Error("Invalid configuration");
|
||||||
}
|
}
|
||||||
setConfig(config) {
|
if(!this._config) {
|
||||||
if(!config || !config.card) {
|
this._config = config;
|
||||||
throw new Error("Invalid configuration");
|
|
||||||
}
|
this.hass = hass();
|
||||||
if(!this._config) {
|
this._getEntities();
|
||||||
this._config = config;
|
this.cardConfig = {entities: this.entities, ...config.card};
|
||||||
this.cardConfig = {entities: [], ...config.card};
|
this.card = createCard(this.cardConfig);
|
||||||
this.entities = [];
|
} else {
|
||||||
} else {
|
this._config = config;
|
||||||
this._config = config;
|
this.hass = this.hass;
|
||||||
this.hass = this.hass;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getEntities()
|
// Reevaluate all filters once areas have been loaded
|
||||||
|
getData().then(() => this._getEntities());
|
||||||
|
}
|
||||||
|
|
||||||
|
_getEntities()
|
||||||
|
{
|
||||||
|
let entities = [];
|
||||||
|
// Start with any entities added by the `entities` parameter
|
||||||
|
if(this._config.entities)
|
||||||
|
entities = entities.concat(this._config.entities)
|
||||||
|
.map((e) => {
|
||||||
|
if(typeof(e) === "string")
|
||||||
|
return {entity: e};
|
||||||
|
return e;
|
||||||
|
});
|
||||||
|
|
||||||
|
if(!this.hass || !this._config.filter) return entities;
|
||||||
|
|
||||||
|
if(this._config.filter.include) {
|
||||||
|
const all_entities = Object.keys(this.hass.states)
|
||||||
|
.map((e) => new Object({entity: e}));
|
||||||
|
|
||||||
|
for(const f of this._config.filter.include) {
|
||||||
|
if(f.type !== undefined) {
|
||||||
|
// If the filter has a type, it's a special entry
|
||||||
|
entities.push(f);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let add = all_entities.filter(entity_filter(this.hass, f))
|
||||||
|
.map((e) => new Object({...e, ...f.options}));
|
||||||
|
|
||||||
|
if(f.sort !== undefined) {
|
||||||
|
// Sort per filter
|
||||||
|
add = add.sort(entity_sorter(this.hass, f.sort));
|
||||||
|
}
|
||||||
|
entities = entities.concat(add);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(this._config.filter.exclude) {
|
||||||
|
for(const f of this._config.filter.exclude) {
|
||||||
|
entities = entities.filter((e) => {
|
||||||
|
// Don't exclude special entries
|
||||||
|
if(typeof(e) !== "string" && e.entity === undefined) return true;
|
||||||
|
return !entity_filter(this.hass,f)(e)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this._config.sort) {
|
||||||
|
// Sort everything
|
||||||
|
entities = entities.sort(entity_sorter(this.hass, this._config.sort));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this._config.unique) {
|
||||||
|
function compare(a,b) {
|
||||||
|
if(typeof(a) !== typeof(b)) return false;
|
||||||
|
if(typeof(a) !== "object") return a===b;
|
||||||
|
if(Object.keys(a).some((k) => !Object.keys(b).includes(k))) return false;
|
||||||
|
|
||||||
|
return Object.keys(a).every((k) => compare(a[k], b[k]));
|
||||||
|
}
|
||||||
|
let newEntities = [];
|
||||||
|
for(const e of entities) {
|
||||||
|
if(newEntities.some((i) => compare(i,e))) continue;
|
||||||
|
newEntities.push(e);
|
||||||
|
}
|
||||||
|
entities = newEntities;
|
||||||
|
}
|
||||||
|
this.entities = entities;
|
||||||
|
}
|
||||||
|
|
||||||
|
set entities(ent) {
|
||||||
|
function compare(a,b) {
|
||||||
|
if( a === b )
|
||||||
|
return true;
|
||||||
|
if( a == null || b == null)
|
||||||
|
return false;
|
||||||
|
if(a.length != b.length)
|
||||||
|
return false;
|
||||||
|
for(var i = 0; i < a.length; i++)
|
||||||
|
if(JSON.stringify(a[i]) !== JSON.stringify(b[i]))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(!compare(ent, this._entities))
|
||||||
{
|
{
|
||||||
let entities = [];
|
this._entities = ent;
|
||||||
// Start with any entities added by the `entities` parameter
|
this.cardConfig = {...this.cardConfig, entities: this._entities};
|
||||||
if(this._config.entities)
|
if(ent.length === 0 && this._config.show_empty === false) {
|
||||||
entities = entities.concat(this._config.entities)
|
this.style.display = "none";
|
||||||
.map((e) => {
|
this.style.margin = "0";
|
||||||
if(typeof(e) === "string")
|
} else {
|
||||||
return {entity: e};
|
this.style.display = null;
|
||||||
return e;
|
this.style.margin = null;
|
||||||
});
|
}
|
||||||
|
|
||||||
if(!this.hass || !this._config.filter) return entities;
|
|
||||||
|
|
||||||
if(this._config.filter.include) {
|
|
||||||
const all_entities = Object.keys(this.hass.states)
|
|
||||||
.map((e) => new Object({entity: e}));
|
|
||||||
|
|
||||||
for(const f of this._config.filter.include) {
|
|
||||||
if(f.type !== undefined) {
|
|
||||||
// If the filter has a type, it's a special entry
|
|
||||||
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}));
|
|
||||||
|
|
||||||
if(f.sort !== undefined) {
|
|
||||||
// Sort per filter
|
|
||||||
add = add.sort(entity_sorter(this.hass, f.sort));
|
|
||||||
}
|
|
||||||
entities = entities.concat(add);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(this._config.filter.exclude) {
|
|
||||||
for(const f of this._config.filter.exclude) {
|
|
||||||
entities = entities.filter((e) => {
|
|
||||||
// Don't exclude special entries
|
|
||||||
if(typeof(e) !== "string" && e.entity === undefined) return true;
|
|
||||||
return !entity_filter(this.hass,f)(e)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(this._config.sort) {
|
|
||||||
// Sort everything
|
|
||||||
entities = entities.sort(entity_sorter(this.hass, this._config.sort));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(this._config.unique) {
|
|
||||||
function compare(a,b) {
|
|
||||||
if(typeof(a) !== typeof(b)) return false;
|
|
||||||
if(typeof(a) !== "object") return a===b;
|
|
||||||
if(Object.keys(a).some((k) => !Object.keys(b).includes(k))) return false;
|
|
||||||
|
|
||||||
return Object.keys(a).every((k) => compare(a[k], b[k]));
|
|
||||||
}
|
|
||||||
let newEntities = [];
|
|
||||||
for(const e of entities) {
|
|
||||||
if(newEntities.some((i) => compare(i,e))) continue;
|
|
||||||
newEntities.push(e);
|
|
||||||
}
|
|
||||||
entities = newEntities;
|
|
||||||
}
|
|
||||||
return entities;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
get entities() {
|
||||||
|
return this._entities;
|
||||||
|
}
|
||||||
|
|
||||||
set entities(ent) {
|
set cardConfig(cardConfig) {
|
||||||
function compare(a,b) {
|
this._cardConfig = cardConfig;
|
||||||
if( a === b )
|
if(this.card)
|
||||||
return true;
|
this.card.setConfig(cardConfig);
|
||||||
if( a == null || b == null)
|
}
|
||||||
return false;
|
get cardConfig() {
|
||||||
if(a.length != b.length)
|
return this._cardConfig;
|
||||||
return false;
|
}
|
||||||
for(var i = 0; i < a.length; i++)
|
|
||||||
if(JSON.stringify(a[i]) !== JSON.stringify(b[i]))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if(!compare(ent, this._entities))
|
|
||||||
{
|
|
||||||
this._entities = ent;
|
|
||||||
this.cardConfig = {...this.cardConfig, entities: this._entities};
|
|
||||||
if(ent.length === 0 && this._config.show_empty === false) {
|
|
||||||
this.style.display = "none";
|
|
||||||
this.style.margin = "0";
|
|
||||||
} else {
|
|
||||||
this.style.display = null;
|
|
||||||
this.style.margin = null;
|
|
||||||
}
|
|
||||||
this.requestUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
get entities() {
|
|
||||||
return this._entities;
|
|
||||||
}
|
|
||||||
|
|
||||||
set cardConfig(cardConfig) {
|
updated(changedProperties) {
|
||||||
this._cardConfig = cardConfig;
|
if(changedProperties.has("hass") && this.hass) {
|
||||||
if(this.querySelector("card-maker"))
|
this.card.hass = this.hass;
|
||||||
this.querySelector("card-maker").config = cardConfig;
|
// Run this in a timeout to improve performance
|
||||||
}
|
setTimeout(() => this._getEntities(), 0);
|
||||||
get cardConfig() {
|
|
||||||
return this._cardConfig;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
firstUpdated() {
|
createRenderRoot() {
|
||||||
this.cardConfig = this._cardConfig;
|
return this;
|
||||||
}
|
}
|
||||||
|
render() {
|
||||||
|
return html`
|
||||||
|
${this.card}`;
|
||||||
|
}
|
||||||
|
|
||||||
updated(changedProperties) {
|
getCardSize() {
|
||||||
if(changedProperties.has("hass") && this.hass) {
|
let len = 0;
|
||||||
// Run this in a timeout to improve performance
|
if(this.card && this.card.getCardSize)
|
||||||
setTimeout(() => this._getEntities().then((e) => this.entities = e), 0);
|
len = this.card.getCardSize();
|
||||||
}
|
if(len === 1 && this.entities.length)
|
||||||
}
|
len = this.entities.length;
|
||||||
|
if(len === 0 && this._config.filter && this._config.filter.include)
|
||||||
createRenderRoot() {
|
len = Object.keys(this._config.filter.include).length;
|
||||||
return this;
|
return len || 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
|
||||||
return html`
|
|
||||||
<card-maker
|
|
||||||
.hass=${this.hass}
|
|
||||||
></card-maker>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
getCardSize() {
|
|
||||||
let len = 0;
|
|
||||||
if(this.querySelector("card-maker") && this.querySelector("card-maker").getCardSize)
|
|
||||||
len = this.querySelector("card-maker").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;
|
|
||||||
return len || 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
customElements.define('auto-entities', AutoEntities);
|
customElements.define('auto-entities', AutoEntities);
|
||||||
|
|||||||
+63
-63
@@ -1,66 +1,66 @@
|
|||||||
export function entity_sorter(hass, method) {
|
export function entity_sorter(hass, method) {
|
||||||
if(typeof(method) === "string") {
|
if(typeof(method) === "string") {
|
||||||
method = {method};
|
method = {method};
|
||||||
|
}
|
||||||
|
return function(a, b) {
|
||||||
|
const entityA = typeof(a) === "string"
|
||||||
|
? hass.states[a]
|
||||||
|
: hass.states[a.entity];
|
||||||
|
const entityB = typeof(b) === "string"
|
||||||
|
? hass.states[b]
|
||||||
|
: hass.states[b.entity];
|
||||||
|
|
||||||
|
if(entityA === undefined || entityB === undefined) return 0;
|
||||||
|
|
||||||
|
const [lt, gt] = method.reverse ? [-1, 1] : [1, -1];
|
||||||
|
function compare(_a, _b) {
|
||||||
|
if(method.ignore_case && _a.toLowerCase) _a = _a.toLowerCase();
|
||||||
|
if(method.ignore_case && _b.toLowerCase) _b = _b.toLowerCase();
|
||||||
|
if(_a === undefined && _b === undefined) return 0;
|
||||||
|
if(_a === undefined) return lt;
|
||||||
|
if(_b === undefined) return gt;
|
||||||
|
if(_a < _b) return gt;
|
||||||
|
if(_a > _b) return lt;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
return function(a, b) {
|
switch(method.method) {
|
||||||
const entityA = typeof(a) === "string"
|
case "domain":
|
||||||
? hass.states[a]
|
return compare(
|
||||||
: hass.states[a.entity];
|
entityA.entity_id.split(".")[0],
|
||||||
const entityB = typeof(b) === "string"
|
entityB.entity_id.split(".")[0]
|
||||||
? hass.states[b]
|
);
|
||||||
: hass.states[b.entity];
|
case "entity_id":
|
||||||
|
return compare(
|
||||||
if(entityA === undefined || entityB === undefined) return 0;
|
entityA.entity_id,
|
||||||
|
entityB.entity_id
|
||||||
const [lt, gt] = method.reverse ? [-1, 1] : [1, -1];
|
);
|
||||||
function compare(_a, _b) {
|
case "friendly_name":
|
||||||
if(method.ignore_case && _a.toLowerCase) _a = _a.toLowerCase();
|
case "name":
|
||||||
if(method.ignore_case && _b.toLowerCase) _b = _b.toLowerCase();
|
return compare(
|
||||||
if(_a === undefined && _b === undefined) return 0;
|
entityA.attributes.friendly_name || entityA.entity_id.split(".")[1],
|
||||||
if(_a === undefined) return lt;
|
entityB.attributes.friendly_name || entityB.entity_id.split(".")[1]
|
||||||
if(_b === undefined) return gt;
|
);
|
||||||
if(_a < _b) return gt;
|
|
||||||
if(_a > _b) return lt;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
switch(method.method) {
|
|
||||||
case "domain":
|
|
||||||
return compare(
|
|
||||||
entityA.entity_id.split(".")[0],
|
|
||||||
entityB.entity_id.split(".")[0]
|
|
||||||
);
|
|
||||||
case "entity_id":
|
|
||||||
return compare(
|
|
||||||
entityA.entity_id,
|
|
||||||
entityB.entity_id
|
|
||||||
);
|
|
||||||
case "friendly_name":
|
|
||||||
case "name":
|
|
||||||
return compare(
|
|
||||||
entityA.attributes.friendly_name || entityA.entity_id.split(".")[1],
|
|
||||||
entityB.attributes.friendly_name || entityB.entity_id.split(".")[1]
|
|
||||||
);
|
|
||||||
case "state":
|
case "state":
|
||||||
return compare(
|
return compare(
|
||||||
entityA.state,
|
entityA.state,
|
||||||
entityB.state
|
entityB.state
|
||||||
);
|
);
|
||||||
case "attribute":
|
case "attribute":
|
||||||
let _a = entityA.attributes;
|
let _a = entityA.attributes;
|
||||||
let _b = entityB.attributes;
|
let _b = entityB.attributes;
|
||||||
let attr = method.attribute;
|
let attr = method.attribute;
|
||||||
while(attr) {
|
while(attr) {
|
||||||
let k;
|
let k;
|
||||||
[k, attr] = attr.split(":");
|
[k, attr] = attr.split(":");
|
||||||
_a = _a[k];
|
_a = _a[k];
|
||||||
_b = _b[k];
|
_b = _b[k];
|
||||||
if(_a === undefined && _b === undefined) return 0;
|
if(_a === undefined && _b === undefined) return 0;
|
||||||
if(_a === undefined) return lt;
|
if(_a === undefined) return lt;
|
||||||
if(_b === undefined) return gt;
|
if(_b === undefined) return gt;
|
||||||
}
|
}
|
||||||
return compare(_a, _b);
|
return compare(_a, _b);
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user