config-template-card icon indicating copy to clipboard operation
config-template-card copied to clipboard

Allow async evaluation in special mode

Open malmeloo opened this issue 4 months ago • 0 comments

Adds async configuration value which allows using await (and other async constructs) in templates. Hidden behind a config option because the change is not backwards-compatible; see README changes for documentation.

Useful if you want to e.g. make a websocket request, example:

type: custom:config-template-card
entities:
  - ${vars.sonos_id.entity_id}
variables:
  sonos_id: states["media_player.keuken"]
  base_card:
    camera_view: auto
    type: picture-glance
    entities: []
    aspect_ratio: "1:1"
  getFavorites: |
    async () => {
      const favs = await this.hass.callWS({
        "type": "media_player/browse_media",
        "entity_id": vars.sonos_id.entity_id,
        "media_content_id": "object.item.audioItem.audioBroadcast",
        "media_content_type": "favorites_folder"
      })

      return favs.children.filter(
        (fav) => fav.can_play
      );
    }
  getCard: |
    (fav) => {
      return Object.assign({}, vars.base_card, {
        title: fav.title,
        image: fav.thumbnail
      })
    }
async: true
card:
  type: grid
  cards: ${return (await getFavorites()).map(getCard))

malmeloo avatar Oct 18 '24 10:10 malmeloo