diff --git a/media-source-image-card.js b/media-source-image-card.js
index df9298b..d2f6090 100644
--- a/media-source-image-card.js
+++ b/media-source-image-card.js
@@ -73,16 +73,26 @@ class MediaSourceImageCard extends HTMLElement {
return new Function('hass', 'states', 'user', 'config', `'use strict'; ${_template}`).call(this, this._hass, this._hass.states, this._hass.user, this.config);
}
- getImageUrl(image) {
+ getMediaUrl(url) {
return new Promise(
resolve => {
- if (this.config.image.indexOf('{{') > -1) return resolve(this.renderTemplate(image));
- if (this.config.image.indexOf('[[[') > -1) return resolve(this.renderJsTemplate(image));
- return resolve(image);
+ if (this.config.image.indexOf('media-source://') == -1) return resolve({url});
+ return resolve(this._hass.callWS({
+ type: "media_source/resolve_media",
+ media_content_id: url
+ }));
}
);
}
+ async getImageUrl(image) {
+ // if template, resolve rendered template:
+ if (this.config.image.indexOf('{{') > -1) return this.getMediaUrl(await this.renderTemplate(image));
+ if (this.config.image.indexOf('[[[') > -1) return this.getMediaUrl(await this.renderJsTemplate(image));
+ // else, call HA service to get media source url:
+ return this.getMediaUrl(image);
+ }
+
setConfig(config) {
if (!config.image) {
throw new Error('You have to provide an url for a media source image');
@@ -116,24 +126,16 @@ class MediaSourceImageCard extends HTMLElement {
renderContent() {
this.getImageUrl(this.config.image)
- .then(imageUrl => {
- this._hass.callWS({
- type: "media_source/resolve_media",
- media_content_id: imageUrl
- }).then(response => {
- if (this.image != response.url) {
- this.image = response.url;
- if (response.url.indexOf('mp4') != -1 || response.url.indexOf('ogg') != -1 || response.url.indexOf('webm') != -1) {
- this.content.innerHTML = ``;
- } else {
- this.content.innerHTML = `
`;
- }
+ .then(response => {
+ if (this.image != response.url) {
+ this.image = response.url;
+ 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) {
@@ -201,7 +203,7 @@ window.customCards.push({
});
console.info(
- `%c MEDIA SOURCE IMAGE CARD %c Version 0.2.2 `,
+ `%c MEDIA SOURCE IMAGE CARD %c Version 0.2.3 `,
'color: orange; font-weight: bold; background: black',
'color: white; font-weight: bold; background: dimgray',
);