Add option to remove duplicates. Fix #26
This commit is contained in:
parent
65490abff1
commit
6d3329b925
@ -33,6 +33,7 @@ filter:
|
|||||||
- <filter>
|
- <filter>
|
||||||
|
|
||||||
show_empty: <show_empty>
|
show_empty: <show_empty>
|
||||||
|
unique: <unique>
|
||||||
sort: <sort_method>
|
sort: <sort_method>
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -44,6 +45,7 @@ sort: <sort_method>
|
|||||||
- `include:` **Required.** A list of filters specifying which entities to add to the card
|
- `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
|
- `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`.
|
- `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`.
|
- `sort:` How to sort the entities of the card. Default: `none`.
|
||||||
|
|
||||||
### Filters
|
### Filters
|
||||||
|
File diff suppressed because one or more lines are too long
@ -1,5 +1,5 @@
|
|||||||
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) {
|
||||||
@ -39,7 +39,7 @@ export function entity_filter(hass, filter) {
|
|||||||
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;
|
||||||
@ -85,12 +85,12 @@ export function entity_filter(hass, filter) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "not":
|
case "not":
|
||||||
if(entity_filter(hass,value)(e))
|
if(entity_filter(hass,value)(e))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "device":
|
case "device":
|
||||||
let _deviceMatch = false;
|
let _deviceMatch = false;
|
||||||
for(const d of window.cardToolsData.devices) {
|
for(const d of window.cardToolsData.devices) {
|
||||||
@ -101,7 +101,7 @@ export function entity_filter(hass, filter) {
|
|||||||
}
|
}
|
||||||
if(!_deviceMatch) return false;
|
if(!_deviceMatch) return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "area":
|
case "area":
|
||||||
let _areaMatch = false;
|
let _areaMatch = false;
|
||||||
for (const a of window.cardToolsData.areas) {
|
for (const a of window.cardToolsData.areas) {
|
||||||
|
19
src/main.js
19
src/main.js
@ -17,7 +17,7 @@ class AutoEntities extends LitElement {
|
|||||||
if(!config || !config.card) {
|
if(!config || !config.card) {
|
||||||
throw new Error("Invalid configuration");
|
throw new Error("Invalid configuration");
|
||||||
}
|
}
|
||||||
|
|
||||||
this._config = config;
|
this._config = config;
|
||||||
this.entities = [];
|
this.entities = [];
|
||||||
this.cardConfig = {entities: this.entities, ...config.card};
|
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));
|
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;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +110,7 @@ class AutoEntities extends LitElement {
|
|||||||
const newEntities = await this._getEntities();
|
const newEntities = await this._getEntities();
|
||||||
if(!compare(oldEntities, newEntities)) {
|
if(!compare(oldEntities, newEntities)) {
|
||||||
this.entities = newEntities;
|
this.entities = newEntities;
|
||||||
this.cardConfig = {
|
this.cardConfig = {
|
||||||
...this.cardConfig,
|
...this.cardConfig,
|
||||||
entities: newEntities,
|
entities: newEntities,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user