AmoebeLabs / swiss-army-knife-card

The versatile custom Swiss Army Knife card for Home Assistant allows you to create your unique visualization using several graphical tools, styling options and animations.
232 stars 19 forks source link

Add support for disabling a tool in a layout template #134

Closed AmoebeLabs closed 2 years ago

AmoebeLabs commented 2 years ago

The Problem To Be Solved

Layout templates are a great thing to re-use layouts. A template can use variables to 'configure' the template. However, the layout is still more or less fixed: one could use display: none to disable a tool run-time, but it would be nice if that could be done during configuration setup for tools that won't be needed.

Take for instance a layout with a battery icon and warning: if the sensor doesn't have a battery entity, that tool is not needed. By disabling this tool, this layout can be used for both a layout with a battery indicator (wireless sensor) and without (wired sensor).

Without this support, one would need two different - but almost identical - layout templates!

By explicitly enabling and disabling tools, one could even overlay certain tools in a single layout, and get different layouts depending on a certain variable flag.

Additional background:

By disabling a tool from the configuration, this tool is not created: ie no runtime overhead!

Related Issues (if any)

(Optional): Suggested Solution

A simple flag disabled: true/false would be enough for starters...

For the more complex use, ie enabling and disabling other tools with a single variable flag, some JavaScript is required as YAML does not allow for logical operators, ie not true or !true...

(Optional): Alternative Solutions

For the more complex use, there is a weird alternative by using two explicit settings: disabled and enabled flags.

Say we have two tools. One is enabled by a single flag, the other one disabled, and vice versa:

Flag = true, tool1 shown (default disabled, but enabled through flag), tool2 not (default enabled, but disabled by flag)

  tool1:
    default_disabled: true
    enabled: '[[flag]]' # true

  tool2:
    disabled: '[[flag]]' # true
    default_enabled: true

Flag = false, tool1 is hidden (default disabled, and not enabled through flag), tool2 is shown (default enabled and not disabled by flag)

  tool1:
    default_disabled: true
    enabled: '[[flag]]' # false

  tool2:
    disabled: '[[flag]]' # false
    default_enabled: true

This solves the simple true/false flag variable without any use of JavaScript, but what if that variable holds another value, a setting that involves more than one tool or group of tools?

Furthermore, this combined setting of flag might be confusing for users 😃, where a simple yes/no flag is not.

AmoebeLabs commented 2 years ago

The current parser assumes that we have a live, instantiated tool (with state, etc).

For configuration stuff, we just have the configuration, and we need to be able to distinguish these things.

Solution implemented:

The configuration parser replaces the JavaScript evaluations with the result.