dnguyen800 / air-visual-card

A Lovelace card showing air quality data from airvisual.com. Requires the AirVisual component.
MIT License
100 stars 35 forks source link

⚠️ Information: Future card configuration changes. #17

Closed thomasloven closed 4 years ago

thomasloven commented 4 years ago

Hi.

A change in how lovelace cards are set up in Home Assistant 0.105 increases performance, but enables cards to accidentally modify their own configuration in the loaded lovelace configuration.

I'm going through the cards in the HACS default repository, and noticed that your card may be susceptible to this. Looking through your code it seems you may modify parts of the config object passed to your card in setConfig.

The result could be that your card does not work well with the GUI editors or that parts of the configuration start showing up multiple times.

At some point in the future, it is likely that the configuration will be frozen before being passed to setConfig. At this point, your card will fail entirely when it tries to modify the configuration object.

There are several ways to fix/protect agains this problem.

The best is to restructure setConfig such that the configuration is never modified. Other alternatives are to make a copy of the configuration and work on that instead.

setConfig(config) {
  config = { ...config }; // This works for simple configurations not containing arrays or objects
...
import { deepClone } from "deep-clone-simple";
// https://github.com/balloob/deep-clone-simple

setConfig(config) {
  config = deepClone(config); // This is a safe and fast method
...

or

setConfig(config) {
  config = JSON.parse(JSON.stringify(config)); // This uses built-in functions, but may be slower than deepClone
  ...

Please note that I have not tested your card agains Home Assistant 0.105 or later, but just quickly looked through the code. If I'm mistaken in my assessment, I appologize for taking your time.

See thomasloven/hass-config#6 for more info.

dnguyen800 commented 4 years ago

thanks for the advice. i will look into the changes when I have a chance.

thomasloven commented 4 years ago

Just move this down a bit https://github.com/dnguyen800/air-visual-card/blob/master/dist/air-visual-card.js#L29-L31 and do it to cardConfig instead, and you should be ok.

SeLLeRoNe commented 4 years ago

I guess this is what need to be done to fix the card with HA 0.106.0? Because since the update there is an error (Cannot add property icons, object is not extensible) and the card doesn't load anymore ;)

dnguyen800 commented 4 years ago

Hopefully this is fixed in release 0.0.10. I couldn't test properly, but the code that modifies the config object isn't needed, so I deleted it.

SeLLeRoNe commented 4 years ago

I am afraid it didn't fix the issue

SeLLeRoNe commented 4 years ago

Nevermind, now it works, it must have been a cache issue even if I cleaned it multiple times and added a comment with the version on top of the file (which my be a good suggestion to check what version is the browser using ;) )