diff --git a/auto-entities.js b/auto-entities.js index 151173c..699afa5 100644 --- a/auto-entities.js +++ b/auto-entities.js @@ -1,4 +1,4 @@ -!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;const n="custom:";function o(t,e){const i=document.createElement("hui-error-card");return i.setConfig({type:"error",error:t,config:e}),i}function a(t,e){if(!e||"object"!=typeof e||!e.type)return o(`No ${t} type configured`,e);let i=e.type;if(i=i.startsWith(n)?i.substr(n.length):`hui-${i}-${t}`,customElements.get(i))return function(t,e){const i=document.createElement(t);try{i.setConfig(e)}catch(t){return o(t,e)}return i}(i,e);const s=o(`Custom element doesn't exist: ${i}.`,e);s.style.display="None";const r=setTimeout(()=>{s.style.display=""},2e3);return customElements.whenDefined(i).then(()=>{clearTimeout(r),function(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("app-drawer-layout partial-panel-resolver"))&&s.shadowRoot||s)&&s.querySelector("ha-panel-lovelace"))&&s.shadowRoot)&&s.querySelector("hui-root"))&&s.shadowRoot)&&s.querySelector("ha-app-layout #view"))&&s.firstElementChild)&&s.dispatchEvent(t)}}("ll-rebuild",{},s)}),s}function c(){return document.querySelector("home-assistant").hass}class u extends s{static get properties(){return{hass:{},config:{},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 r`${this.el}`}}if(!customElements.get("card-maker")){class t extends u{create(t){return function(t){return a("card",t)}(t)}getCardSize(){return this.firstElementChild&&this.firstElementChild.getCardSize?this.firstElementChild.getCardSize():1}}customElements.define("card-maker",t)}if(!customElements.get("element-maker")){class t extends u{create(t){return function(t){return a("element",t)}(t)}}customElements.define("element-maker",t)}if(!customElements.get("entity-row-maker")){class t extends u{create(t){return function(t){const e=new Set(["call-service","divider","section","weblink"]);if(!t)return o("Invalid configuration given.",t);if("string"==typeof t&&(t={entity:t}),"object"!=typeof t||!t.entity&&!t.type)return o("Invalid configuration given.",t);const i=t.type||"default";if(e.has(i)||i.startsWith(n))return a("row",t);const s=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"}[s]||"text"}),a("entity-row",t)}(t)}}customElements.define("entity-row-maker",t)}const l=c().callWS({type:"config/area_registry/list"}),f=c().callWS({type:"config/device_registry/list"}),d=c().callWS({type:"config/entity_registry/list"});async function h(){return window.cardToolsData=window.cardToolsData||{areas:await l,devices:await f,entities:await d},window.cardToolsData}function g(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 p(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 y(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)"))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 m(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(!y(n,s.entity_id.split(".")[0]))return!1;break;case"entity_id":if(!y(n,s.entity_id))return!1;break;case"state":if(!y(n,s.state))return!1;break;case"name":if(!s.attributes.friendly_name||!y(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&&!y(e,r))return!1}break;case"not":if(m(t,n)(i))return!1;break;case"device":let e=!1;for(const t of window.cardToolsData.devices)y(n,t.name)&&p(t).includes(s.entity_id)&&(e=!0);if(!e)return!1;break;case"area":let r=!1;for(const t of window.cardToolsData.areas)y(n,t.name)&&g(t).flatMap(p).includes(s.entity_id)&&(r=!0);if(!r)return!1;break;default:return!1}return!0}}function _(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:ti?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}}}h();customElements.define("auto-entities",class extends s{static get properties(){return{hass:{}}}async setConfig(t){if(!t||!t.card)throw new Error("Invalid configuration");this._config?(this._config=t,this.hass=this.hass):(this._config=t,this.cardConfig={entities:[],...t.card},this.entities=[])}async _getEntities(){let t=[];if(this._config.entities&&(t=t.concat(this._config.entities).map(t=>"string"==typeof t?{entity:t}:t)),!this.hass||!this._config.filter)return t;if(this._config.filter.include){const e=Object.keys(this.hass.states).map(t=>new Object({entity:t}));for(const i of this._config.filter.include){if(void 0!==i.type){t.push(i);continue}(i.device||i.area)&&await h();let s=e.filter(m(this.hass,i)).map(t=>new Object({...t,...i.options}));void 0!==i.sort&&(s=s.sort(_(this.hass,i.sort))),t=t.concat(s)}}if(this._config.filter.exclude)for(const e of this._config.filter.exclude)t=t.filter(t=>"string"!=typeof t&&void 0===t.entity||!m(this.hass,e)(t));if(this._config.sort&&(t=t.sort(_(this.hass,this._config.sort))),this._config.unique){function e(t,i){return typeof t==typeof i&&("object"!=typeof t?t===i:!Object.keys(t).some(t=>!Object.keys(i).includes(t))&&Object.keys(t).every(s=>e(t[s],i[s])))}let i=[];for(const s of t)i.some(t=>e(t,s))||i.push(s);t=i}return t}set entities(t){(function(t,e){if(t===e)return!0;if(null==t||null==e)return!1;if(t.length!=e.length)return!1;for(var i=0;ithis._getEntities().then(t=>this.entities=t),0)}createRenderRoot(){return this}render(){return r` +!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="))return parseFloat(e)>=parseFloat(t.substr(2));if(t.startsWith("<"))return parseFloat(e)"))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 j(t,e){return function(r){const i="string"==typeof r?t.states[r]:t.states[r.entity];if(!r)return!1;for(const[s,n]of Object.entries(e))switch(s.split(" ")[0]){case"options":case"sort":break;case"domain":if(!O(n,i.entity_id.split(".")[0]))return!1;break;case"entity_id":if(!O(n,i.entity_id))return!1;break;case"state":if(!O(n,i.state))return!1;break;case"name":if(!i.attributes.friendly_name||!O(n,i.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(i.entity_id)))return!1;break;case"attributes":for(const[t,e]of Object.entries(n)){let r=t.split(" ")[0],s=i.attributes;for(;r&&s;){let t;[t,r]=r.split(":"),s=s[t]}if(void 0===s||e&&!O(e,s))return!1}break;case"not":if(j(t,n)(r))return!1;break;case"device":let e=!1;for(const t of window.cardToolsData.devices)O(n,t.name)&&w(t).includes(i.entity_id)&&(e=!0);if(!e)return!1;break;case"area":let s=!1;for(const t of window.cardToolsData.areas)O(n,t.name)&&v(t).flatMap(w).includes(i.entity_id)&&(s=!0);if(!s)return!1;break;default:return!1}return!0}}function k(t,e){return"string"==typeof e&&(e={method:e}),function(r,i){const s="string"==typeof r?t.states[r]:t.states[r.entity],n="string"==typeof i?t.states[i]:t.states[i.entity];if(void 0===s||void 0===n)return 0;const[o,a]=e.reverse?[-1,1]:[1,-1];function c(t,r){return e.ignore_case&&t.toLowerCase&&(t=t.toLowerCase()),e.ignore_case&&r.toLowerCase&&(r=r.toLowerCase()),void 0===t&&void 0===r?0:void 0===t?o:void 0===r?a:tr?o:0}switch(e.method){case"domain":return c(s.entity_id.split(".")[0],n.entity_id.split(".")[0]);case"entity_id":return c(s.entity_id,n.entity_id);case"friendly_name":case"name":return c(s.attributes.friendly_name||s.entity_id.split(".")[1],n.attributes.friendly_name||n.entity_id.split(".")[1]);case"state":return c(s.state,n.state);case"attribute":let t=s.attributes,r=n.attributes,i=e.attribute;for(;i;){let e;if([e,i]=i.split(":"),t=t[e],r=r[e],void 0===t&&void 0===r)return 0;if(void 0===t)return o;if(void 0===r)return a}return c(t,r);default:return 0}}}_();customElements.define("auto-entities",class extends i{static get properties(){return{hass:{}}}setConfig(t){if(!t||!t.card)throw new Error("Invalid configuration");this._config?(this._config=t,this.hass=this.hass):(this._config=t,this.cardConfig={entities:[],...t.card},this.entities=[])}async _getEntities(){let t=[];if(this._config.entities&&(t=t.concat(this._config.entities).map(t=>"string"==typeof t?{entity:t}:t)),!this.hass||!this._config.filter)return t;if(this._config.filter.include){const e=Object.keys(this.hass.states).map(t=>new Object({entity:t}));for(const r of this._config.filter.include){if(void 0!==r.type){t.push(r);continue}(r.device||r.area)&&await _();let i=e.filter(j(this.hass,r)).map(t=>new Object({...t,...r.options}));void 0!==r.sort&&(i=i.sort(k(this.hass,r.sort))),t=t.concat(i)}}if(this._config.filter.exclude)for(const e of this._config.filter.exclude)t=t.filter(t=>"string"!=typeof t&&void 0===t.entity||!j(this.hass,e)(t));if(this._config.sort&&(t=t.sort(k(this.hass,this._config.sort))),this._config.unique){function e(t,r){return typeof t==typeof r&&("object"!=typeof t?t===r:!Object.keys(t).some(t=>!Object.keys(r).includes(t))&&Object.keys(t).every(i=>e(t[i],r[i])))}let r=[];for(const i of t)r.some(t=>e(t,i))||r.push(i);t=r}return t}set entities(t){(function(t,e){if(t===e)return!0;if(null==t||null==e)return!1;if(t.length!=e.length)return!1;for(var r=0;rthis._getEntities().then(t=>this.entities=t),0)}createRenderRoot(){return this}render(){return s` `}getCardSize(){return this.querySelector("card-maker")&&this.querySelector("card-maker").getCardSize?this.querySelector("card-maker").getCardSize():this.entities.length?this.entities.length:this._config.filter&&this._config.filter.include?Object.keys(this._config.filter.include).length:1}})}]); \ No newline at end of file + >`}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",{})}]); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index bdb4490..6ba115a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -627,7 +627,7 @@ "dev": true }, "card-tools": { - "version": "github:thomasloven/lovelace-card-tools#4bcbb48158dc6ee1cfa767c25125fe84c0ea32df", + "version": "github:thomasloven/lovelace-card-tools#605caa906c46340599614aaf9d4e934aabc19b15", "from": "github:thomasloven/lovelace-card-tools" }, "chalk": { diff --git a/package.json b/package.json index b8f8f6e..15d69ef 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "webpack", "watch": "webpack --watch --mode=development", - "test": "echo \"Error: no test specified\" && exit 1" + "update-card-tools": "npm uninstall card-tools && npm install thomasloven/lovelace-card-tools" }, "author": "Thomas Lovén", "license": "MIT", diff --git a/src/main.js b/src/main.js index 88d9e8f..c5730d7 100644 --- a/src/main.js +++ b/src/main.js @@ -3,6 +3,7 @@ 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"; class AutoEntities extends LitElement { @@ -11,11 +12,10 @@ class AutoEntities extends LitElement { hass: {}, }; } - async setConfig(config) { + setConfig(config) { if(!config || !config.card) { throw new Error("Invalid configuration"); } - if(!this._config) { this._config = config; this.cardConfig = {entities: [], ...config.card}; @@ -160,14 +160,16 @@ class AutoEntities extends LitElement { } getCardSize() { + let len = 0; if(this.querySelector("card-maker") && this.querySelector("card-maker").getCardSize) - return this.querySelector("card-maker").getCardSize(); - if(this.entities.length) - return this.entities.length; - if(this._config.filter && this._config.filter.include) + 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 1 + return len || 1; } } -customElements.define('auto-entities', AutoEntities) \ No newline at end of file +customElements.define('auto-entities', AutoEntities); +fireEvent('ll-rebuild', {}); \ No newline at end of file