Streamline Card

repository·main·Indexed 42 days ago

https://github.com/brunosabot/streamline-card

A Home Assistant Lovelace card (v0.2.2) that provides a powerful templating system for creating reusable card blueprints. It allows users to define card structures with variables and JavaScript logic to reduce configuration duplication, supporting dynamic styling and the generation of card grids based on entity attributes.

Tokens
8.7K
Snippets
28
Records
32
Agent score
55%

What's inside Streamline Card

  1. How templates and variables work in Streamline Card

    main

    A template acts as a blueprint for your cards. It defines a structure but leaves specific parts empty using variables. When you use the template in a dashboard, you provide values for these variables to customize each instance.

    Key Concepts:

    • Variables: Defined in the template using double brackets, e.g., [[variable_name]].
    • Default Values: You can define a default section in your template to provide fallback values if a variable isn't specified during usage.
    • Template Structure: Every template consists of a Template Name, an optional Default Values list, and a Card Configuration (using the card key for standard cards or element for picture-elements).
    my_light_template:
      default:
        - light_icon: mdi:ceiling-light
      card:
        type: custom:bubble-card
        name: "[[room_name]] Light"
        icon: "[[light_icon]]"
        entity: "[[light_entity]]"
  2. Configure Streamline Card templates

    main

    Templates are defined in the Home Assistant UI raw configuration editor or in your configuration files under the streamline_templates key. A template consists of a default section (optional) for variable defaults and a card section which defines the actual card configuration. Inside the card configuration, you use double brackets [[variable_name]] to create placeholders that will be filled by variables when the card is used.

    streamline_templates:
      light_template:
        default:
          - icon: mdi:ceiling-light
        card:
          type: custom:bubble-card
          card_type: button
          button_type: "[[type]]"
          entity: "[[entity]]"
          name: "[[name]]"
          icon: "[[icon]]"
          tap_action:
            action: toggle
          hold_action:
            action: more-info
  3. Split template configuration with !include

    main

    To keep your streamline_templates.yaml organized, you can use the standard YAML !include tag to reference external files. Paths are resolved relative to the file containing the !include tag.

    Example structure:

    streamline_templates.yaml:

    light_template: !include templates/light_template.yaml
    weather_template: !include templates/weather_template.yaml

    templates/light_template.yaml:

    default:
      - light_icon: mdi:ceiling-light
    card:
      type: custom:bubble-card
      name: "[[room_name]] Light"
      icon: "[[light_icon]]"
      entity: "[[light_entity]]"
    light_template: !include templates/light_template.yaml
  4. Set up templates using the UI (Raw Editor)

    main

    For beginners or quick setups, you can define templates directly in the dashboard's Raw Configuration Editor.

    1. Open your dashboard.
    2. Click the menu icon (⋮) and select Raw configuration editor.
    3. At the very top of the file, add your streamline_templates block.
    streamline_templates:
      my_light_template:
        default:
          - light_icon: mdi:ceiling-light
        card:
          type: custom:bubble-card
          name: "[[room_name]] Light"
          icon: "[[light_icon]]"
          entity: "[[light_entity]]"
  5. Set up a development environment for Streamline Card

    main

    To develop or contribute to Streamline Card, follow these steps:

    1. Prerequisites: Install Node.js (v18+) and pnpm.
    2. Clone the repo:
      git clone https://github.com/brunosabot/streamline-card.git
      cd streamline-card
    3. Install dependencies:
      pnpm install
    4. Configure Output (Optional): Create a .env.development.local file to specify where built files should go:
      TARGET_DIRECTORY=/path/to/your/homeassistant/www/streamline-card
    5. Run Development Mode:
      pnpm dev
      This watches for changes and rebuilds automatically. If TARGET_DIRECTORY is set, it builds directly into your Home Assistant directory for live testing.
    pnpm install
    pnpm dev
  6. Manual Installation of Streamline Card

    main

    If you cannot use HACS, follow these steps to install manually:

    1. Download the file: Download streamline-card.js from the repository.
    2. Save to Home Assistant: Place the file in your <config>/www folder.
    3. Add to Resources:
      • Open your dashboard and click the menu icon (⋮).
      • Select Edit dashboard.
      • Click the menu icon (⋮) again and select Manage resources.
      • Click Add resource.
      • URL: /local/streamline-card.js?v=1 (increment the version number v=X whenever you update the file to bypass browser cache).
      • Type: JavaScript Module.
      • Click Create.
    4. Add to Dashboard: Refresh your browser, enter Edit dashboard mode, and add the Streamline Card.
    /local/streamline-card.js?v=1
  7. Install Streamline Card via HACS (Recommended)

    main

    The easiest way to install Streamline Card is through the Home Assistant Community Store (HACS).

    1. Install HACS if you haven't already (follow instructions at hacs.xyz).
    2. In your Home Assistant sidebar, go to HACS.
    3. Search for Streamline Card.
    4. Click on the card and click the Download button.
    5. To use it, go to your dashboard, enter Edit dashboard mode, and search for Streamline Card in the Add card menu.
  8. Set up templates using YAML Configuration

    main

    For managing many templates, use YAML files. This is the recommended method for advanced users.

    1. Create a directory: Create /config/streamline_templates/ in your Home Assistant configuration directory.
    2. Create template files: Add YAML files (e.g., light_template.yaml) inside that folder.
    3. Register in Dashboard: In your dashboard's YAML configuration, include the directory using the !include_dir_named tag:
    streamline_templates: !include_dir_named ../streamline_templates/

    Automatic Loading Fallbacks: If you don't use !include_dir_named, the card automatically looks for a single streamline_templates.yaml file in these locations (in order):

    1. /config/www/community/streamline-card/streamline_templates.yaml (HACS default)
    2. /config/www/streamline-card/streamline_templates.yaml (Manual default)
    streamline_templates: !include_dir_named ../streamline_templates/
  9. Listen for template updates via streamline-templates-updated event

    main

    The templateLoader performs background revalidation to check for changes in the remote YAML file. If the new templates differ from the currently loaded ones, a custom DOM event is dispatched. You can listen for this event to react to template changes (e.g., to re-render components).

    Event Name: streamline-templates-updated
    Detail: Contains the url of the template file that was updated.

    window.addEventListener('streamline-templates-updated', (event) => {
      console.log('Templates updated from:', event.detail.url);
      // Trigger UI updates here
    });
  10. Understand Streamline Card template variables

    main

    Streamline Card uses a specific syntax to define variables within a template. Variables are identified by wrapping a name in double square brackets: [[variable_name]].

    When the editor parses a template, it extracts these names and generates a configuration form. The editor uses the variable name to determine the appropriate input type in the UI:

    • Entity Selectors: If the variable name contains entity (case-insensitive), the editor provides an entity picker.
    • Icon Selectors: If the variable name contains icon (case-insensitive), the editor provides an icon picker.
    • Boolean Selectors: If the variable name contains bool (case-insensitive), the editor provides a boolean toggle.
    • Text/Default: All other variables default to a standard text input.

    Note: The visual editor currently does not support variables that resolve to objects or arrays. If a variable contains a non-primitive value, an error message will be displayed: Object and array variables are not supported in the visual editor.

    [[entity]]
    [[icon]]
    [[is_enabled_bool]]
    [[label_text]]
  11. Configure the Streamline Card via the UI Editor

    main

    The streamline-card-editor provides a visual interface for configuring cards. The resulting configuration object follows this structure:

    • type: Always streamline-card.
    • template: The name of the selected template from streamline_templates.
    • variables: A dictionary of key-value pairs corresponding to the [[variable_name]] placeholders in the template.
    • grid_options: (Optional) Layout settings for the dashboard grid.
    • visibility: (Optional) Visibility settings for the card.

    When you change a template in the editor, the existing variables are cleared and reset to defaults to prevent configuration mismatch.

    {
      "type": "streamline-card",
      "template": "example_tile",
      "variables": {
        "entity": "light.living_room",
        "icon": "mdi:lightbulb"
      },
      "grid_options": {
        "columns": 2
      }
    }
  12. How Streamline Card templates work

    main

    Streamline Card acts as a wrapper that resolves a template and then renders the underlying component.

    1. Template Resolution: The card looks up the template name provided in the config within the streamline_templates object (found in the Lovelace configuration or loaded remotely).
    2. Configuration Evaluation: The card uses evaluateConfig to merge the template's configuration with the variables provided in the card's local config. It can also access the hass object and use Javascript if the template contains the _javascript key.
    3. Component Creation:
      • If the template defines a card, the card uses HELPERS.createCardElement(config) to instantiate it.
      • If the template defines an element, it uses HELPERS.createHuiElement(config) to instantiate a Home Assistant UI element.
    4. Lifecycle: The card manages updates to hass, editMode, and config using a requestAnimationFrame-based queue to ensure efficient re-renders.