• Stars
    star
    247
  • Rank 164,117 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🔹A collection of tools for other lovelace plugins to use

card-tools version 2

hacs_badge

This is a collection of tools to simplify creating custom cards for Home Assistant

Installation instructions

If you see "Can't find card-tools. [...]" in your Home Assistant UI, follow these instructions.

To install card-tools follow this guide.

The recommended type of this plugin is: module

resources:
  url: /local/card-tools.js
  type: module

User instructions

That's all. You don't need to do anything else.




Card Developer Instructions

There are two ways you can get access to the card-tools functions.

  1. If you are using npm and a packager:

Add the package as a dependency

> npm install thomasloven/lovelace-card-tools

And then import the parts you want

import { LitElement } from "card-tools/src/lit-element";
  1. Have your users add card-tools.js to their lovelace resources and access the functions through the card-tools customElement:
customElements.whenDefined('card-tools').then(() => {
  var cardTools = customElements.get('card-tools');
  // YOUR CODE GOES IN HERE

  class MyPlugin extends cardTools.LitElement {
    setConfig(config) {
      this.name = config.name;
    }

    render() {
      return cardTools.LitHtml`
        ${this.name}
      `;
    }
  }

  customElements.define("my-plugin", MyPlugin);
}); // END OF .then(() => {

setTimeout(() => {
  if(customElements.get('card-tools')) return;
  customElements.define('my-plugin', class extends HTMLElement{
    setConfig() { throw new Error("Can't find card-tools. See https://github.com/thomasloven/lovelace-card-tools");}
  });
}, 2000);

The setTimeout block at the end will make your element display an error message if card-tools is not found. Make sure the element name is the same in both customElements.define() calls.

Functions

card-tools/src/deviceID

deviceID

This can be used to uniquely identify the device connected to Lovelace. Or actually, the device-browser combination.

It generates a random number, and stores it in the browsers local storage. That means it will stay around for quite a while.

It's kind of hard to explain, but as an example I use this to identify the browser for browser_mod.

I'm sure this can have lots of more uses.

The device ID is stored in localstorage with a key called lovelace-player-device-id (for historical reasons).

card-tools/src/event

fireEvent(ev, detail) / `cardTools.fireEvent(...)

This is mainly used as a helper for some other functions of cardTools, but it could be useful to fire a lovelace event sometime, such as "config-refresh" perhaps? Explore!

card-tools/src/hass

hass() / cardTools.hass

This is provided for plugins that aren't cards, elements or entity rows. For those three kinds, the hass object is kindly provided to you by the whatever loads your element, but if you wish to write something that doesn't have a representation in the DOM, this can give you access to all of Home Assistants power anyway.

  ...
  greeting.innerHTML = `Hi there, ${cardTools.hass.user.name}`;
  cardTools.hass.connection.subscribeEvents((event) => {console.log("A service was called in Home Assistant")}, 'call-service');

Note that this is called as a function if imported, but is a direct property of the cardTools element.

lovelace() / cardTools.lovelace

This object contains information about the users lovelace configuration. As a bonus lovelace().current_view contains the index of the currently displayed view.
Note that this is called as a function if imported, but is a direct property of the cardTools element.

lovelace_view() / cardTools.lovelace_view()

Return a reference to the lovelace view object.

provideHass(element) / cardTools.provideHass(...)

Will make sure element.hass is set and updated whenever the hass object is updated (i.e. when any entity state changes).

load_lovelace()

Evaluating this function will load in the lovelace interface and all customElements of it, such as ha-card. This is not provided in the card-tools element, because that wouldn't make sense.

card-tools/src/lit-element

LitElement / cardTools.LitElement

The lit-element base class. Using this could save you a few bytes.

html / cardTools.LitHtml

css / cardTools.LitCSS

The html and css functions from lit-element.

card-tools/src/action

bindActionHandler(element, options) / `cardTools.longpress(...)

This binds element to the action-handler of lovelace, which manages different special click/tap actions such as long-press or double click.

Once bound, the element will receive action events whenever something happens.

render() {
  return html`
    <div
      id="my-clickable-div"
      @action=${(ev) => console.log("I was clicked, or something", ev)

    >
      Double-tap me!
    </div>`;
}

firstUpdated() {
  bindActionHandler(this.shadowRoot.querySelector("#my-clickable-div"), {hasHold: true, hasDoubleClick: true});
}

card-tools/src/lovelace-element.js

createCard(config) / cardTools.createCard(...)

createElement(config) / cardTools.createElement(...)

createEntityRow(config) / cardTools.createEntityRow(...)

Currently, custom elements can be used in three places in Lovelace; as cards, as elements in a picture-elements card or as rows in an entities card.

Those functions creates a card, element or row safely and cleanly from a config object. They handle custom elements and automatically picks the most suitable row for an entity. In short, it's mainly based on - and works very similar to - how Lovelace handles those things natively.

const myElement = createElement({
  type: "state-icon",
  entity: "light.bed_light",
  hold_action: {action: "toggle"},
});

card-tools/src/card-maker

Importing this file (or adding card-tools.js the lovelace resources) will define three new customElements, card-maker, element-maker and entity-row-maker which acts as wrappers for cards, elements and entity rows respectively. Very useful for e.g. cards which contain other cards:

render() {
  return html`
  <ha-card>
    <card-maker
      .config=${this.card1_config}
      .hass=${this.hass}
      ></card-maker>
    <card-maker
      .config=${this.card2_config}
      .hass=${this.hass}
      ></card-maker>
  </ha-card>`;
}

card-tools/src/more-info

moreInfo(entity, large=false) / cardTools.moreInfo(...)

Pops open the more-info dialog for entity.

card-tools/src/popup

popUp(title, card, large=false, style=null, fullscreen=false) / cardTools.popUp(...)

Opens up a dialog similar to the more-info dialog, but with the contents replaces by the lovelace card defined by card.

closePopUp() / cardTools.closePopUp()

Closes a popUp or more-info dialog.

card-tools/src/templates

parseTemplate(hass, str, variables) / cardTools.parseTemplate(hass, str, variables)

Renders and returns a jinja2 template in the backend. Only works if the currently logged in user is in the admin group.

subscribeRenderTemplate(conn, onChange, params) / cardTools.subscribeRenderTemplate(...)

Renders the jinja2 templates in parameters.template in the backend and sends the results to onChange whenever anything changes. Returns a function for canceling the subscription.

card-tools/src/old-templates

Relates to my Mod Plugin Templates which are rendered entirely in the frontend

hasOldTemplate(text)

Check if there is a template in text.

parseOldTemplate(text, data) / cardTools.parseTemplate(text, data)

Parses a template and returns the result.


Buy Me A Coffee

More Repositories

1

hass-browser_mod

🔹 A Home Assistant integration to turn your browser into a controllable entity and media player
TypeScript
1,314
star
2

lovelace-auto-entities

🔹Automatically populate the entities-list of lovelace cards
TypeScript
1,271
star
3

lovelace-card-mod

🔹 Add CSS styles to (almost) any lovelace card
TypeScript
1,143
star
4

lovelace-layout-card

🔹 Get more control over the placement of lovelace cards.
TypeScript
1,064
star
5

lovelace-slider-entity-row

🔹 Add sliders to entity cards
TypeScript
818
star
6

lovelace-fold-entity-row

🔹 A foldable row for entities card, containing other rows
TypeScript
595
star
7

lovelace-state-switch

🔹Dynamically replace lovelace cards depending on occasion
TypeScript
400
star
8

hass-fontawesome

🔹 Use icons from fontawesome in home-assistant
Python
284
star
9

lovelace-template-entity-row

🔹 Display whatever you want in an entities card row.
TypeScript
221
star
10

hass-lovelace_gen

🔹 Improve the lovelace yaml parser for Home Assistant
Python
218
star
11

hass-config

My Home Assistant configuration
Python
213
star
12

lovelace-more-info-card

🔹 Display the more-info dialog of any entity as a lovelace card
TypeScript
150
star
13

lovelace-hui-element

🔹 Use built-in elements in the wrong place
TypeScript
109
star
14

hass-favicon

🔹 Change the favicon of your Home Assistant instance
Python
104
star
15

lovelace-flower-card

JavaScript
102
star
16

lovelace-card-modder

JavaScript
101
star
17

lovelace-popup-card

JavaScript
94
star
18

hass-plejd

🔹 Plejd BLE integration for Home Assistant
Python
90
star
19

round-slider

JavaScript
85
star
20

lovelace-badge-card

🔹 Place badges anywhere in the lovelace layout
JavaScript
81
star
21

lovelace-toggle-lock-entity-row

JavaScript
62
star
22

lovelace-fullykiosk

Lovelace plugin for using Fully Kiosk Browser features in home-assistant
JavaScript
61
star
23

homeassistant-lovelace-gen

A lovelace configuration file generator for home assistant
Python
57
star
24

lovelace-player

Lets any browser currently viewing your lovelace interface act as an audio receiver with a media_player interface
JavaScript
50
star
25

mittos64

C
47
star
26

lovelace-theme-maker

JavaScript
42
star
27

lovelace-browser-commander

JavaScript
33
star
28

lovelace-gap-card

🔹 A lovelace card that does nothing and looks like nothing. Incredibly useful! No, really.
JavaScript
31
star
29

lovelace-useful-markdown-card

JavaScript
30
star
30

hass-custom-devcontainer

A devcontainer for developing and testing custom Home Assistant stuff
Shell
29
star
31

homeassistant-2020-presentation

18
star
32

lovelace-gui-sandbox

🔹 Lets you play around with the GUI editors even if you're using YAML mode
JavaScript
17
star
33

lovelace-markdown-mod

JavaScript
16
star
34

lovelace-color-picker

JavaScript
14
star
35

lovelace-card-loader

JavaScript
13
star
36

plejd2mqtt

JavaScript
12
star
37

lovelace-column-card

A columnizing card for lovelace ui for home-assistant.
JavaScript
12
star
38

card-tools

A collection of tools to help develop lovelace cards
JavaScript
11
star
39

os5

My attempts at developing an operating system kernel.
C
11
star
40

lovelace-dummy-entity-row

🔹 An entity row with only icon and name
JavaScript
10
star
41

lovelace-q-card

JavaScript
9
star
42

lovelace-long-press

JavaScript
9
star
43

lovelace-time-input-row

JavaScript
9
star
44

nextrevo-mod

How I use the E3D Revo system with Prusa Nextruder
8
star
45

dito

A library and some tools for handling disk image files.
C
8
star
46

mittsnap

rsnapshot-like tool that leverages btrfs snapshots
Shell
5
star
47

dotfiles

MATLAB
5
star
48

lovelace-color-glance-card

A glance card with a colorized background
JavaScript
3
star
49

lovelace-icon-headers

WIP
JavaScript
2
star
50

lovelace-wbah

Lovelace with bluejays and herons
TypeScript
2
star
51

pyplejd

Python
2
star
52

os4

C
1
star
53

new-hass-config

Python
1
star
54

z80Monitor

Assembly
1
star
55

vim-tstatus

My status line for vim
Vim Script
1
star
56

mittos64-old

C
1
star
57

hass-plant_helper

🔹 Home Assistant Helper for monitoring plant health using MiFlora devices
Python
1
star