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 });
}
updateCameraFeedSrc() {
this.hass.connection.sendMessagePromise({
async updateCameraFeedSrc() {
try {
const { content_type: contentType, content } = await this.hass.callWS({
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}`,
cameraFeedSrc: `data:${contentType};base64, ${content}`,
});
} else {
} catch (err) {
this.imageLoaded = false;
}
});
}
_computeStateName(stateObj) {

View File

@ -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() {

View File

@ -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) {

View File

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

View File

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

View File

@ -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,
async _fetchConfig() {
try {
const conf = await this.hass.callWS({ type: 'frontend/lovelace_config' });
this.setProperties({
_config: conf,
_state: 'loaded',
}),
err => this.setProperties({
});
} catch (err) {
this.setProperties({
_state: 'error',
_errorMsg: err.message,
})
);
});
}
}
_equal(a, b) {