diff --git a/src/cards/ha-camera-card.js b/src/cards/ha-camera-card.js index c26eedf563..575a5a3613 100644 --- a/src/cards/ha-camera-card.js +++ b/src/cards/ha-camera-card.js @@ -99,20 +99,19 @@ class HaCameraCard extends LocalizeMixin(EventsMixin(PolymerElement)) { this.fire('hass-more-info', { entityId: this.stateObj.entity_id }); } - updateCameraFeedSrc() { - this.hass.connection.sendMessagePromise({ - type: 'camera_thumbnail', - entity_id: this.stateObj.entity_id, - }).then((resp) => { - if (resp.success) { - this.setProperties({ - imageLoaded: true, - cameraFeedSrc: `data:${resp.result.content_type};base64, ${resp.result.content}`, - }); - } else { - this.imageLoaded = false; - } - }); + async updateCameraFeedSrc() { + try { + const { content_type: contentType, content } = await this.hass.callWS({ + type: 'camera_thumbnail', + entity_id: this.stateObj.entity_id, + }); + this.setProperties({ + imageLoaded: true, + cameraFeedSrc: `data:${contentType};base64, ${content}`, + }); + } catch (err) { + this.imageLoaded = false; + } } _computeStateName(stateObj) { diff --git a/src/cards/ha-media_player-card.js b/src/cards/ha-media_player-card.js index cce9491067..d5e3ae1dfc 100644 --- a/src/cards/ha-media_player-card.js +++ b/src/cards/ha-media_player-card.js @@ -200,27 +200,7 @@ class HaMediaPlayerCard extends LocalizeMixin(EventsMixin(PolymerElement)) { }; } - playerObjChanged(playerObj, oldPlayerObj) { - const picture = playerObj.stateObj.attributes.entity_picture; - const oldPicture = oldPlayerObj && oldPlayerObj.stateObj.attributes.entity_picture; - - if (picture !== oldPicture && !picture) { - this.$.cover.style.backgroundImage = ''; - } else if (picture !== oldPicture) { - // We have a new picture url - this.hass.connection.sendMessagePromise({ - type: 'media_player_thumbnail', - entity_id: playerObj.stateObj.entity_id, - }).then((resp) => { - if (resp.success) { - this.$.cover.style.backgroundImage = `url(data:${resp.result.content_type};base64,${resp.result.content})`; - } else { - this.$.cover.style.backgroundImage = ''; - this.$.cover.parentElement.classList.add('no-cover'); - } - }); - } - + async playerObjChanged(playerObj, oldPlayerObj) { if (playerObj.isPlaying && playerObj.showProgress) { if (!this._positionTracking) { this._positionTracking = setInterval(() => this.updatePlaybackPosition(), 1000); @@ -232,6 +212,28 @@ class HaMediaPlayerCard extends LocalizeMixin(EventsMixin(PolymerElement)) { if (playerObj.showProgress) { this.updatePlaybackPosition(); } + + const picture = playerObj.stateObj.attributes.entity_picture; + const oldPicture = oldPlayerObj && oldPlayerObj.stateObj.attributes.entity_picture; + + if (picture !== oldPicture && !picture) { + this.$.cover.style.backgroundImage = ''; + return; + } else if (picture === oldPicture) { + return; + } + + // We have a new picture url + try { + const { content_type: contentType, content } = await this.hass.callWS({ + type: 'media_player_thumbnail', + entity_id: playerObj.stateObj.entity_id, + }); + this.$.cover.style.backgroundImage = `url(data:${contentType};base64,${content})`; + } catch (err) { + this.$.cover.style.backgroundImage = ''; + this.$.cover.parentElement.classList.add('no-cover'); + } } updatePlaybackPosition() { diff --git a/src/dialogs/ha-more-info-dialog.js b/src/dialogs/ha-more-info-dialog.js index 163c654842..ad21496726 100644 --- a/src/dialogs/ha-more-info-dialog.js +++ b/src/dialogs/ha-more-info-dialog.js @@ -128,7 +128,7 @@ class HaMoreInfoDialog extends DialogMixin(PolymerElement) { return hass.states[hass.moreInfoEntityId] || null; } - _stateObjChanged(newVal, oldVal) { + async _stateObjChanged(newVal, oldVal) { if (!newVal) { this.setProperties({ opened: false, @@ -139,22 +139,26 @@ class HaMoreInfoDialog extends DialogMixin(PolymerElement) { return; } - if (isComponentLoaded(this.hass, 'config.entity_registry') && - (!oldVal || oldVal.entity_id !== newVal.entity_id)) { - this.hass.connection.sendMessagePromise({ - type: 'config/entity_registry/get', - entity_id: newVal.entity_id, - }).then( - (msg) => { this._registryInfo = msg.result; }, - () => { this._registryInfo = false; } - ); - } - requestAnimationFrame(() => requestAnimationFrame(() => { // allow dialog to render content before showing it so it will be // positioned correctly. this.opened = true; })); + + if (!isComponentLoaded(this.hass, 'config.entity_registry') || + (oldVal && oldVal.entity_id === newVal.entity_id)) { + return; + } + + try { + const info = await this.hass.callWS({ + type: 'config/entity_registry/get', + entity_id: newVal.entity_id, + }); + this._registryInfo = info; + } catch (err) { + this._registryInfo = null; + } } _dialogOpenChanged(newVal) { diff --git a/src/dialogs/more-info/more-info-settings.js b/src/dialogs/more-info/more-info-settings.js index 85b5e63c27..7e3f448ffd 100644 --- a/src/dialogs/more-info/more-info-settings.js +++ b/src/dialogs/more-info/more-info-settings.js @@ -94,15 +94,17 @@ class MoreInfoSettings extends LocalizeMixin(EventsMixin(PolymerElement)) { this.fire('more-info-page', { page: null }); } - _save() { - this.hass.connection.sendMessagePromise({ - type: 'config/entity_registry/update', - entity_id: this.stateObj.entity_id, - name: this._name, - }).then( - (msg) => { this.registryInfo = msg.result; }, - () => { alert('save failed!'); } - ); + async _save() { + try { + const info = await this.hass.callWS({ + type: 'config/entity_registry/update', + entity_id: this.stateObj.entity_id, + name: this._name, + }); + this.registryInfo = info; + } catch (err) { + alert('save failed!'); + } } } customElements.define('more-info-settings', MoreInfoSettings); diff --git a/src/entrypoints/app.js b/src/entrypoints/app.js index 2ac312c825..6e13be822a 100644 --- a/src/entrypoints/app.js +++ b/src/entrypoints/app.js @@ -132,7 +132,7 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) { const language = this.hass.selectedLanguage || this.hass.language; - const resp = await this.hass.connection.sendMessagePromise({ + const { resources } = await this.hass.callWS({ type: 'frontend/get_translations', language, }); @@ -140,7 +140,7 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) { // If we've switched selected languages just ignore this response if ((this.hass.selectedLanguage || this.hass.language) !== language) return; - this._updateResources(language, resp.result.resources); + this._updateResources(language, resources); } _updateResources(language, data) { @@ -427,10 +427,10 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) { } async _loadPanels() { - const msg = await this.connection.sendMessagePromise({ + const panels = await this.hass.callWS({ type: 'get_panels' }); - this._updateHass({ panels: msg.result }); + this._updateHass({ panels }); } diff --git a/src/panels/lovelace/components/hui-image.js b/src/panels/lovelace/components/hui-image.js index 5ec4d1accd..e205a711e3 100644 --- a/src/panels/lovelace/components/hui-image.js +++ b/src/panels/lovelace/components/hui-image.js @@ -135,18 +135,17 @@ class HuiImage extends LocalizeMixin(PolymerElement) { this.$.image.style.filter = filter || (isOff && this._imageFallback && DEFAULT_FILTER) || ''; } - _updateCameraImageSrc() { - this.hass.connection.sendMessagePromise({ - type: 'camera_thumbnail', - entity_id: this.cameraImage, - }).then((resp) => { - if (resp.success) { - this._imageSrc = `data:${resp.result.content_type};base64, ${resp.result.content}`; - this._onImageLoad(); - } else { - this._onImageError(); - } - }, () => this._onImageError()); + async _updateCameraImageSrc() { + try { + const { content_type: contentType, content } = await this.hass.callWS({ + type: 'camera_thumbnail', + entity_id: this.cameraImage, + }); + this._imageSrc = `data:${contentType};base64, ${content}`; + this._onImageLoad(); + } catch (err) { + this._onImageError(); + } } } diff --git a/src/panels/lovelace/ha-panel-lovelace.js b/src/panels/lovelace/ha-panel-lovelace.js index a023e92856..c8d10600e2 100644 --- a/src/panels/lovelace/ha-panel-lovelace.js +++ b/src/panels/lovelace/ha-panel-lovelace.js @@ -102,18 +102,19 @@ class Lovelace extends PolymerElement { this._columns = Math.max(1, matchColumns - (!this.narrow && this.showMenu)); } - _fetchConfig() { - this.hass.connection.sendMessagePromise({ type: 'frontend/lovelace_config' }) - .then( - conf => this.setProperties({ - _config: conf.result, - _state: 'loaded', - }), - err => this.setProperties({ - _state: 'error', - _errorMsg: err.message, - }) - ); + async _fetchConfig() { + try { + const conf = await this.hass.callWS({ type: 'frontend/lovelace_config' }); + this.setProperties({ + _config: conf, + _state: 'loaded', + }); + } catch (err) { + this.setProperties({ + _state: 'error', + _errorMsg: err.message, + }); + } } _equal(a, b) {