Add support for areas
This commit is contained in:
parent
c37bbe6bf3
commit
3cfd04b6ac
@ -40,6 +40,7 @@ All options are optional, and filters will match any entity matching **ALL** opt
|
|||||||
| name | Match friendly name attribute (e.g. "Kitchen lights", "Front door")
|
| name | Match friendly name attribute (e.g. "Kitchen lights", "Front door")
|
||||||
| group | Match entities in given group
|
| group | Match entities in given group
|
||||||
| attributes | Match attributes. **See below**
|
| attributes | Match attributes. **See below**
|
||||||
|
| area | Entity belongs in given area (Home Assistant 0.87 or later)
|
||||||
| options | Additional options to attach to entities matching this filter (only makes sense in `include`)
|
| options | Additional options to attach to entities matching this filter (only makes sense in `include`)
|
||||||
|
|
||||||
The attributes option takes an object with `attribute: value` combinations and matches any entity which matches all of those attributes.
|
The attributes option takes an object with `attribute: value` combinations and matches any entity which matches all of those attributes.
|
||||||
|
@ -6,11 +6,13 @@ class AutoEntities extends cardTools.litElement() {
|
|||||||
throw new Error("Invalid configuration");
|
throw new Error("Invalid configuration");
|
||||||
|
|
||||||
this._config = config;
|
this._config = config;
|
||||||
|
this.data = {};
|
||||||
|
|
||||||
this.entities = this.get_entities() || [];
|
this.entities = this.get_entities() || [];
|
||||||
this.card = cardTools.createCard({entities: this.entities, ...config.card});
|
this.card = cardTools.createCard({entities: this.entities, ...config.card});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
match(pattern, str){
|
match(pattern, str){
|
||||||
if (typeof(str) === "string" && typeof(pattern) === "string") {
|
if (typeof(str) === "string" && typeof(pattern) === "string") {
|
||||||
if((pattern.startsWith('/') && pattern.endsWith('/')) || pattern.indexOf('*') !== -1) {
|
if((pattern.startsWith('/') && pattern.endsWith('/')) || pattern.indexOf('*') !== -1) {
|
||||||
@ -71,6 +73,26 @@ class AutoEntities extends cardTools.litElement() {
|
|||||||
)
|
)
|
||||||
unmatched = true;
|
unmatched = true;
|
||||||
break;
|
break;
|
||||||
|
case "area":
|
||||||
|
let found = false;
|
||||||
|
this.data.areas.forEach((a) => {
|
||||||
|
if(found) return;
|
||||||
|
if(this.match(value, a.name)) {
|
||||||
|
this.data.devices.forEach((d) => {
|
||||||
|
if(found) return;
|
||||||
|
if(d.area_id && d.area_id === a.area_id) {
|
||||||
|
this.data.entities.forEach((en) => {
|
||||||
|
if(found) return;
|
||||||
|
if(en.device_id === d.id && en.entity_id === e.entity_id) {
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if(!found) unmatched = true;
|
||||||
|
break;
|
||||||
case "group":
|
case "group":
|
||||||
if(!value.startsWith("group.")
|
if(!value.startsWith("group.")
|
||||||
|| !hass.states[value]
|
|| !hass.states[value]
|
||||||
@ -89,6 +111,8 @@ class AutoEntities extends cardTools.litElement() {
|
|||||||
unmatched = true;
|
unmatched = true;
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
unmatched = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if(!unmatched) retval.push(count);
|
if(!unmatched) retval.push(count);
|
||||||
@ -106,7 +130,7 @@ class AutoEntities extends cardTools.litElement() {
|
|||||||
|
|
||||||
if(this._config.filter.include){
|
if(this._config.filter.include){
|
||||||
this._config.filter.include.forEach((f) => {
|
this._config.filter.include.forEach((f) => {
|
||||||
const add = this.match_filter(this._hass, Object.keys(this._hass.states), f)
|
const add = this.match_filter(this._hass, Object.keys(this._hass.states), f);
|
||||||
add.forEach((i) => {
|
add.forEach((i) => {
|
||||||
entities.push({entity: Object.keys(this._hass.states)[i], ...f.options});
|
entities.push({entity: Object.keys(this._hass.states)[i], ...f.options});
|
||||||
});
|
});
|
||||||
@ -134,16 +158,27 @@ class AutoEntities extends cardTools.litElement() {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async get_data(hass) {
|
||||||
|
try {
|
||||||
|
this.data.areas = await hass.callWS({type: "config/area_registry/list"});
|
||||||
|
this.data.devices = await hass.callWS({type: "config/device_registry/list"});
|
||||||
|
this.data.entities = await hass.callWS({type: "config/entity_registry/list"});
|
||||||
|
} catch (err) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
set hass(hass) {
|
set hass(hass) {
|
||||||
this._hass = hass;
|
this._hass = hass;
|
||||||
const oldlen = this.entities.length;
|
this.get_data(hass).then(() => {
|
||||||
this.entities = this.get_entities() || [];
|
const oldlen = this.entities.length;
|
||||||
if(this.card)
|
this.entities = this.get_entities() || [];
|
||||||
{
|
if(this.card)
|
||||||
this.card.hass = this._hass;
|
{
|
||||||
this.card.setConfig({entities: this.entities, ...this._config.card});
|
this.card.hass = this._hass;
|
||||||
}
|
this.card.setConfig({entities: this.entities, ...this._config.card});
|
||||||
if(this.entities.length != oldlen) this.requestUpdate();
|
}
|
||||||
|
if(this.entities.length != oldlen) this.requestUpdate();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user