Add option to remove duplicates. Fix #26

This commit is contained in:
Thomas Lovén 2019-11-08 09:26:08 +01:00
parent 65490abff1
commit 6d3329b925
4 changed files with 25 additions and 8 deletions

View File

@ -33,6 +33,7 @@ filter:
- <filter>
show_empty: <show_empty>
unique: <unique>
sort: <sort_method>
```
@ -44,6 +45,7 @@ sort: <sort_method>
- `include:` **Required.** A list of filters specifying which entities to add to the card
- `exclude:` A list of filters specifying which entities to remove from the card
- `show_empty:` Whether to display the card if it has no entities. Default: `true`.
- `unique:` Whether to remove duplicate values after filtering and sorting. Default: `false`.
- `sort:` How to sort the entities of the card. Default: `none`.
### Filters

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
import { areaByName, areaDevices, deviceByName, deviceEntities } from "card-tools/src/devices";
function match(pattern, value) {
if(typeof(value) === "string" && typeof(pattern) === "string") {
if((pattern.startsWith('/') && pattern.endsWith('/')) || pattern.indexOf('*') !== -1) {
@ -39,7 +39,7 @@ export function entity_filter(hass, filter) {
case "sort":
break;
case "domain":
case "domain":
if(!match(value, entity.entity_id.split('.')[0]))
return false;
break;
@ -85,12 +85,12 @@ export function entity_filter(hass, filter) {
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) {
@ -101,7 +101,7 @@ export function entity_filter(hass, filter) {
}
if(!_deviceMatch) return false;
break;
case "area":
let _areaMatch = false;
for (const a of window.cardToolsData.areas) {

View File

@ -17,7 +17,7 @@ class AutoEntities extends LitElement {
if(!config || !config.card) {
throw new Error("Invalid configuration");
}
this._config = config;
this.entities = [];
this.cardConfig = {entities: this.entities, ...config.card};
@ -76,6 +76,21 @@ class AutoEntities extends LitElement {
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;
}
@ -95,7 +110,7 @@ class AutoEntities extends LitElement {
const newEntities = await this._getEntities();
if(!compare(oldEntities, newEntities)) {
this.entities = newEntities;
this.cardConfig = {
this.cardConfig = {
...this.cardConfig,
entities: newEntities,
};