feat: Add support of attribute tree when available (#996)

* Add support of attribute tree when available
* Update README file

Authored-by: Eric Vandecasteele <eric@onlinux.fr>
This commit is contained in:
Eric Vandecasteele
2023-08-11 01:48:05 +02:00
committed by GitHub
parent edc6b9085d
commit 9c1c31eaef
2 changed files with 7 additions and 3 deletions

View File

@@ -124,7 +124,7 @@ properties of the Entity object detailed in the following table (as per `sensor.
| Name | Type | Default | Description |
|------|:----:|:-------:|-------------|
| entity ***(required)*** | string | | Entity id of the sensor.
| attribute | string | | Retrieves an attribute instead of the state
| attribute | string | | Retrieves an attribute or sub-attribute (attr1.attr2...) instead of the state
| name | string | | Set a custom display name, defaults to entity's friendly_name.
| color | string | | Set a custom color, overrides all other color options including thresholds.
| unit | string | | Set a custom unit of measurement, overrides `unit` set in base config.

View File

@@ -270,12 +270,16 @@ class MiniGraphCard extends LitElement {
`;
}
getObjectAttr(obj, path) {
return path.split('.').reduce((res, key) => res && res[key], obj);
}
getEntityState(id) {
const entityConfig = this.config.entities[id];
if (this.config.show.state === 'last') {
return this.points[id][this.points[id].length - 1][V];
} else if (entityConfig.attribute) {
return this.entity[id].attributes[entityConfig.attribute];
return this.getObjectAttr(this.entity[id].attributes, entityConfig.attribute);
} else {
return this.entity[id].state;
}
@@ -907,7 +911,7 @@ class MiniGraphCard extends LitElement {
newStateHistory[0].forEach((item) => {
if (this.config.entities[index].attribute) {
// eslint-disable-next-line no-param-reassign
item.state = item.attributes[this.config.entities[index].attribute];
item.state = this.getObjectAttr(item.attributes, this.config.entities[index].attribute);
// eslint-disable-next-line no-param-reassign
delete item.attributes;
}