Use callWS helper (#1465)

This commit is contained in:
Paulus Schoutsen 2018-07-18 17:13:53 +02:00 committed by GitHub
parent 4c3e039423
commit 9ab4158e0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 84 deletions

View File

@ -99,20 +99,19 @@ class HaCameraCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
this.fire('hass-more-info', { entityId: this.stateObj.entity_id }); this.fire('hass-more-info', { entityId: this.stateObj.entity_id });
} }
updateCameraFeedSrc() { async updateCameraFeedSrc() {
this.hass.connection.sendMessagePromise({ try {
type: 'camera_thumbnail', const { content_type: contentType, content } = await this.hass.callWS({
entity_id: this.stateObj.entity_id, type: 'camera_thumbnail',
}).then((resp) => { entity_id: this.stateObj.entity_id,
if (resp.success) { });
this.setProperties({ this.setProperties({
imageLoaded: true, imageLoaded: true,
cameraFeedSrc: `data:${resp.result.content_type};base64, ${resp.result.content}`, cameraFeedSrc: `data:${contentType};base64, ${content}`,
}); });
} else { } catch (err) {
this.imageLoaded = false; this.imageLoaded = false;
} }
});
} }
_computeStateName(stateObj) { _computeStateName(stateObj) {

View File

@ -200,27 +200,7 @@ class HaMediaPlayerCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
}; };
} }
playerObjChanged(playerObj, oldPlayerObj) { async 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');
}
});
}
if (playerObj.isPlaying && playerObj.showProgress) { if (playerObj.isPlaying && playerObj.showProgress) {
if (!this._positionTracking) { if (!this._positionTracking) {
this._positionTracking = setInterval(() => this.updatePlaybackPosition(), 1000); this._positionTracking = setInterval(() => this.updatePlaybackPosition(), 1000);
@ -232,6 +212,28 @@ class HaMediaPlayerCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
if (playerObj.showProgress) { if (playerObj.showProgress) {
this.updatePlaybackPosition(); 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() { updatePlaybackPosition() {

View File

@ -128,7 +128,7 @@ class HaMoreInfoDialog extends DialogMixin(PolymerElement) {
return hass.states[hass.moreInfoEntityId] || null; return hass.states[hass.moreInfoEntityId] || null;
} }
_stateObjChanged(newVal, oldVal) { async _stateObjChanged(newVal, oldVal) {
if (!newVal) { if (!newVal) {
this.setProperties({ this.setProperties({
opened: false, opened: false,
@ -139,22 +139,26 @@ class HaMoreInfoDialog extends DialogMixin(PolymerElement) {
return; 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(() => { requestAnimationFrame(() => requestAnimationFrame(() => {
// allow dialog to render content before showing it so it will be // allow dialog to render content before showing it so it will be
// positioned correctly. // positioned correctly.
this.opened = true; 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) { _dialogOpenChanged(newVal) {

View File

@ -94,15 +94,17 @@ class MoreInfoSettings extends LocalizeMixin(EventsMixin(PolymerElement)) {
this.fire('more-info-page', { page: null }); this.fire('more-info-page', { page: null });
} }
_save() { async _save() {
this.hass.connection.sendMessagePromise({ try {
type: 'config/entity_registry/update', const info = await this.hass.callWS({
entity_id: this.stateObj.entity_id, type: 'config/entity_registry/update',
name: this._name, entity_id: this.stateObj.entity_id,
}).then( name: this._name,
(msg) => { this.registryInfo = msg.result; }, });
() => { alert('save failed!'); } this.registryInfo = info;
); } catch (err) {
alert('save failed!');
}
} }
} }
customElements.define('more-info-settings', MoreInfoSettings); customElements.define('more-info-settings', MoreInfoSettings);

View File

@ -132,7 +132,7 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
const language = this.hass.selectedLanguage || this.hass.language; 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', type: 'frontend/get_translations',
language, language,
}); });
@ -140,7 +140,7 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
// If we've switched selected languages just ignore this response // If we've switched selected languages just ignore this response
if ((this.hass.selectedLanguage || this.hass.language) !== language) return; if ((this.hass.selectedLanguage || this.hass.language) !== language) return;
this._updateResources(language, resp.result.resources); this._updateResources(language, resources);
} }
_updateResources(language, data) { _updateResources(language, data) {
@ -427,10 +427,10 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
} }
async _loadPanels() { async _loadPanels() {
const msg = await this.connection.sendMessagePromise({ const panels = await this.hass.callWS({
type: 'get_panels' type: 'get_panels'
}); });
this._updateHass({ panels: msg.result }); this._updateHass({ panels });
} }

View File

@ -135,18 +135,17 @@ class HuiImage extends LocalizeMixin(PolymerElement) {
this.$.image.style.filter = filter || (isOff && this._imageFallback && DEFAULT_FILTER) || ''; this.$.image.style.filter = filter || (isOff && this._imageFallback && DEFAULT_FILTER) || '';
} }
_updateCameraImageSrc() { async _updateCameraImageSrc() {
this.hass.connection.sendMessagePromise({ try {
type: 'camera_thumbnail', const { content_type: contentType, content } = await this.hass.callWS({
entity_id: this.cameraImage, type: 'camera_thumbnail',
}).then((resp) => { entity_id: this.cameraImage,
if (resp.success) { });
this._imageSrc = `data:${resp.result.content_type};base64, ${resp.result.content}`; this._imageSrc = `data:${contentType};base64, ${content}`;
this._onImageLoad(); this._onImageLoad();
} else { } catch (err) {
this._onImageError(); this._onImageError();
} }
}, () => this._onImageError());
} }
} }

View File

@ -102,18 +102,19 @@ class Lovelace extends PolymerElement {
this._columns = Math.max(1, matchColumns - (!this.narrow && this.showMenu)); this._columns = Math.max(1, matchColumns - (!this.narrow && this.showMenu));
} }
_fetchConfig() { async _fetchConfig() {
this.hass.connection.sendMessagePromise({ type: 'frontend/lovelace_config' }) try {
.then( const conf = await this.hass.callWS({ type: 'frontend/lovelace_config' });
conf => this.setProperties({ this.setProperties({
_config: conf.result, _config: conf,
_state: 'loaded', _state: 'loaded',
}), });
err => this.setProperties({ } catch (err) {
_state: 'error', this.setProperties({
_errorMsg: err.message, _state: 'error',
}) _errorMsg: err.message,
); });
}
} }
_equal(a, b) { _equal(a, b) {