kalkih / mini-graph-card

Minimalistic graph card for Home Assistant Lovelace UI
MIT License
2.87k stars 231 forks source link

Add support of attribute tree when available. #995

Closed onlinux closed 11 months ago

onlinux commented 11 months ago

When devices have attributes defined as attr1.attr2.attr3 etc... you can define the attribute as attr1.attr2.attr3 etc

Ex: for RPI monitor :

type: custom:mini-graph-card entities:

This will prevent the need to create numerous virtual sensors for each sub-attribute you wish to access.

onlinux commented 11 months ago

screenshot

akloeckner commented 11 months ago

Thanks for the suggestion! I like the feature and it seems to be a quite local change.

However, I feel, there is still a bit of redundancy in the code that should be reduced:

How about adding a function like this and then just re-use it:

  function getObjectAttr (obj, path) {
      let res = obj;

      path.split('.').forEach(function (key) {
         res = typeof res === 'object' && res ? res[key] : undefined;
      });

      return res;
   }

We might have to adjust the style to the mini-graph-card habits a bit.

Also, there is some housekeeping to be done:

What do you think?

ildar170975 commented 11 months ago

This functionality is very useful. Are these usecases possible?

  1. Attribute is a dictionary. A sub attribute is called as “dictattr[“subattr”] - in addition to “dictattr.subattr”.
  2. Attribute is a list. An element is called is “listattr[2]”.

Can be tested with a weather entity.

akloeckner commented 11 months ago

Are these usecases possible?

I would keep it simple for a start:

I suggest to get this merged as simple as possible first. And then extend.