From d19fe2266f9b53d2f1c5b01697049662cf0cb0d5 Mon Sep 17 00:00:00 2001 From: luixal Date: Tue, 27 Feb 2024 19:34:52 +0100 Subject: [PATCH] Fixes problem with non-templated image urls. Adds beta-state support for video. --- media-source-image-card.js | 40 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/media-source-image-card.js b/media-source-image-card.js index 2b44952..df9298b 100644 --- a/media-source-image-card.js +++ b/media-source-image-card.js @@ -21,6 +21,9 @@ class MediaSourceImageCard extends HTMLElement { .card-content { padding: 0px; + display: flex; + align-content: center; + justify-content: center; } img { @@ -111,31 +114,38 @@ class MediaSourceImageCard extends HTMLElement { return hasChanged; } - set hass(hass) { - this._hass = hass; - // render base html: - if (!this.content) this.renderBase(); - // resolve image from media source and render it: - // if (!this.image || this.image != this.config.image) { - // this.image = this.config.image; - if (this.watchEntities(this.config.image, hass)) { - this.getImageUrl(this.config.image) + renderContent() { + this.getImageUrl(this.config.image) .then(imageUrl => { - hass.callWS({ + this._hass.callWS({ type: "media_source/resolve_media", media_content_id: imageUrl }).then(response => { if (this.image != response.url) { this.image = response.url; - this.content.innerHTML = ``; + if (response.url.indexOf('mp4') != -1 || response.url.indexOf('ogg') != -1 || response.url.indexOf('webm') != -1) { + this.content.innerHTML = ``; + } else { + this.content.innerHTML = ``; + } } }).catch(error => { this.content.innerHTML = `Error loading image: ${error.message} `; console.error({config: this.config, error}); }); }) + } + + set hass(hass) { + this._hass = hass; + // render base html and initial content: + if (!this.content) { + this.renderBase(); + this.renderContent(); } - // apply grayscale: + // when a related entity changes, refresh content: + if (this.watchEntities(this.config.image, hass)) this.renderContent(); + // apply grayscale according to entity state: if (this.config.entity_id) { const newState = hass.states[this.config.entity_id].state; if (this.entity_state != newState) { @@ -148,9 +158,9 @@ class MediaSourceImageCard extends HTMLElement { onEntityStateChange(state) { if (this.config.apply_grayscale) { if (state == 'off') { - this.content.querySelector("img").setAttribute("class", "off"); + this.content.querySelector("img")?.setAttribute("class", "off"); } else { - this.content.querySelector("img").removeAttribute("class", "off"); + this.content.querySelector("img")?.removeAttribute("class", "off"); } } } @@ -191,7 +201,7 @@ window.customCards.push({ }); console.info( - `%c MEDIA SOURCE IMAGE CARD %c Version 0.2.0 `, + `%c MEDIA SOURCE IMAGE CARD %c Version 0.2.2 `, 'color: orange; font-weight: bold; background: black', 'color: white; font-weight: bold; background: dimgray', );