custom-cards / flex-table-card

Highly Flexible Lovelace Card - arbitrary contents/columns/rows, regex matched, perfect to show appdaemon created content and anything breaking out of the entity_id + attributes concept
GNU General Public License v3.0
198 stars 23 forks source link

Patch for auto-entities support #25

Closed SBado closed 4 years ago

SBado commented 4 years ago

Here's a small patch file to add support for auto-entities. I don't know if it will break something (probably it will), consider this just as a starting point:

--- flex-table-card.js      2020-02-28 11:54:48.465137124 +0100
+++ flex-table-card.js.auto_entities    2020-02-28 12:24:57.992151258 +0100
@@ -186,7 +186,19 @@
             return new RegExp(`^${merged}$`, 'gi');
     }

-    _getEntities(hass, incl, excl) {
+    _getEntities(hass, entities, incl, excl) {
+        const format_entities = (e) => {
+            if(!e) return null;
+            if(typeof(e) === "string")
+                return {entity: e.trim()}
+            return e;
+        }
+
+        if (!incl && ! excl && entities) {
+                       entities = entities.map(format_entities);
+            return entities.map(e => hass.states[e.entity]);
+        }
+
         // apply inclusion regex
         const incl_re = listify(incl).map(pat => this._getRegEx([pat])); 
         // make sure to respect the incl-implied order: no (incl-)regex-stiching, collect 
@@ -309,7 +321,7 @@
         const root = this.shadowRoot;

         // get "data sources / origins" i.e, entities
-        let entities = this._getEntities(hass, config.entities.include, config.entities.exclude);
+        let entities = this._getEntities(hass, config.entities, config.entities.include, config.entities.exclude);

         // `raw_rows` to be filled with data here, due to 'attr_as_list' it is possible to have
         // multiple data `raw_rows` acquired into one cell(.raw_data), so re-iterate all rows
@@ -343,3 +355,4 @@
 }

 customElements.define('flex-table-card', FlexTableCard);
+
daringer commented 4 years ago

mmh, 1st impression looks legit, not tried the patch yet...

Am I understanding correctly: auto-entities just wraps any card and injects config.entities? would be fine for me and not seeing anything that might break here on the 1st glance...

Overall if neither inc nor exc nor auto-entities are present, the resulting entities var should end up in a valid state (empty Array) and not being undefined or null.

Feel free to push it into a PR and we can do some more extensive testing...

SBado commented 4 years ago

Am I understanding correctly: auto-entities just wraps any card and injects config.entities? would be fine for me and not seeing anything that might break here on the 1st glance...

Correct.

Feel free to push it into a PR and we can do some more extensive testing...

Done!