Bump min version for latest to use async/await (#1312)

* Bump min version for latest to use async/await

* Use async functions

* Load async support for es5

* Lint
This commit is contained in:
Paulus Schoutsen 2018-06-21 13:57:01 -04:00 committed by GitHub
parent 18b52b53cb
commit 75502bac6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 78 deletions

View File

@ -50,6 +50,7 @@
"no-multi-assign": 0, "no-multi-assign": 0,
"radix": 0, "radix": 0,
"no-alert": 0, "no-alert": 0,
"no-return-await": 0,
"prefer-destructuring": 0, "prefer-destructuring": 0,
"no-restricted-globals": [2, "event"], "no-restricted-globals": [2, "event"],
"prefer-promise-reject-errors": 0, "prefer-promise-reject-errors": 0,

View File

@ -77,6 +77,7 @@
"preact": "^8.2.9", "preact": "^8.2.9",
"preact-compat": "^3.18.0", "preact-compat": "^3.18.0",
"react-big-calendar": "^0.19.1", "react-big-calendar": "^0.19.1",
"regenerator-runtime": "^0.11.1",
"unfetch": "^3.0.0", "unfetch": "^3.0.0",
"web-animations-js": "^2.3.1", "web-animations-js": "^2.3.1",
"xss": "^1.0.3" "xss": "^1.0.3"

View File

@ -3,13 +3,13 @@ import os
from user_agents import parse from user_agents import parse
FAMILY_MIN_VERSION = { FAMILY_MIN_VERSION = {
'Chrome': 54, # Object.values 'Chrome': 55, # Async/await
'Chrome Mobile': 54, 'Chrome Mobile': 55,
'Firefox': 47, # Object.values 'Firefox': 52, # Async/await
'Firefox Mobile': 47, 'Firefox Mobile': 52,
'Opera': 41, # Object.values 'Opera': 42, # Async/await
'Edge': 14, # Array.prototype.includes added in 14 'Edge': 15, # Async/await
'Safari': 10, # Many features not supported by 9 'Safari': 10.1, # Async/await
} }

View File

@ -122,27 +122,25 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
|| (hass && hass.connection && (!hass.states || !hass.config))); || (hass && hass.connection && (!hass.states || !hass.config)));
} }
loadResources(fragment) { async loadResources(fragment) {
getTranslation(fragment).then((result) => { const result = await getTranslation(fragment);
this._updateResources(result.language, result.data); this._updateResources(result.language, result.data);
});
} }
loadBackendTranslations() { async loadBackendTranslations() {
if (!this.hass.language) return; if (!this.hass.language) return;
const language = this.hass.selectedLanguage || this.hass.language; const language = this.hass.selectedLanguage || this.hass.language;
this.hass.connection.sendMessagePromise({ const resp = await this.hass.connection.sendMessagePromise({
type: 'frontend/get_translations', type: 'frontend/get_translations',
language, language,
}) });
.then((resp) => {
// 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); // 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);
} }
_updateResources(language, data) { _updateResources(language, data) {
@ -195,55 +193,55 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
translationMetadata: translationMetadata, translationMetadata: translationMetadata,
dockedSidebar: false, dockedSidebar: false,
moreInfoEntityId: null, moreInfoEntityId: null,
callService: (domain, service, serviceData) => callService: async (domain, service, serviceData) => {
conn.callService(domain, service, serviceData || {}) try {
.then( await conn.callService(domain, service, serviceData || {});
() => {
let message; let message;
let name; let name;
if (serviceData.entity_id && this.hass.states && if (serviceData.entity_id && this.hass.states &&
this.hass.states[serviceData.entity_id]) { this.hass.states[serviceData.entity_id]) {
name = computeStateName(this.hass.states[serviceData.entity_id]); name = computeStateName(this.hass.states[serviceData.entity_id]);
} }
if (service === 'turn_on' && serviceData.entity_id) { if (service === 'turn_on' && serviceData.entity_id) {
message = this.localize( message = this.localize(
'ui.notification_toast.entity_turned_on', 'ui.notification_toast.entity_turned_on',
'entity', name || serviceData.entity_id 'entity', name || serviceData.entity_id
); );
} else if (service === 'turn_off' && serviceData.entity_id) { } else if (service === 'turn_off' && serviceData.entity_id) {
message = this.localize( message = this.localize(
'ui.notification_toast.entity_turned_off', 'ui.notification_toast.entity_turned_off',
'entity', name || serviceData.entity_id 'entity', name || serviceData.entity_id
); );
} else { } else {
message = this.localize( message = this.localize(
'ui.notification_toast.service_called', 'ui.notification_toast.service_called',
'service', `${domain}/${service}` 'service', `${domain}/${service}`
); );
} }
notifications.showNotification(message); notifications.showNotification(message);
}, } catch (err) {
function () { const msg = this.localize(
const msg = this.localize( 'ui.notification_toast.service_call_failed',
'ui.notification_toast.service_call_failed', 'service', `${domain}/${service}`
'service', `${domain}/${service}` );
); notifications.showNotification(msg);
notifications.showNotification(msg); throw err;
return Promise.reject(); }
} },
), callApi: async (method, path, parameters) => {
callApi: (method, path, parameters) => {
const host = window.location.protocol + '//' + window.location.host; const host = window.location.protocol + '//' + window.location.host;
const auth = conn.options; const auth = conn.options;
return hassCallApi(host, auth, method, path, parameters).catch((err) => { try {
return await hassCallApi(host, auth, method, path, parameters);
} catch (err) {
if (!err || err.status_code !== 401 || !auth.accessToken) throw err; if (!err || err.status_code !== 401 || !auth.accessToken) throw err;
// If we connect with access token and get 401, refresh token and try again // If we connect with access token and get 401, refresh token and try again
return window.refreshToken().then((accessToken) => { const accessToken = await window.refreshToken();
conn.options.accessToken = accessToken; conn.options.accessToken = accessToken;
return hassCallApi(host, auth, method, path, parameters); return await hassCallApi(host, auth, method, path, parameters);
}); }
});
}, },
}, this.$.storage.getStoredState()); }, this.$.storage.getStoredState());
@ -261,16 +259,16 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
// If we reconnect after losing connection and access token is no longer // If we reconnect after losing connection and access token is no longer
// valid. // valid.
conn.addEventListener('reconnect-error', (_conn, err) => { conn.addEventListener('reconnect-error', async (_conn, err) => {
if (err !== ERR_INVALID_AUTH) return; if (err !== ERR_INVALID_AUTH) return;
disconnected(); disconnected();
this.unsubConnection(); this.unsubConnection();
window.refreshToken().then(accessToken => const accessToken = await window.refreshToken();
this.handleConnectionPromise(window.createHassConnection(null, accessToken))); this.handleConnectionPromise(window.createHassConnection(null, accessToken));
}); });
conn.addEventListener('disconnected', disconnected); conn.addEventListener('disconnected', disconnected);
var unsubEntities; let unsubEntities;
subscribeEntities(conn, (states) => { subscribeEntities(conn, (states) => {
this._updateHass({ states: states }); this._updateHass({ states: states });
@ -278,7 +276,7 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
unsubEntities = unsub; unsubEntities = unsub;
}); });
var unsubConfig; let unsubConfig;
subscribeConfig(conn, (config) => { subscribeConfig(conn, (config) => {
this._updateHass({ config: config }); this._updateHass({ config: config });
@ -288,8 +286,7 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
this._loadPanels(); this._loadPanels();
var unsubThemes; let unsubThemes;
this.hass.connection.sendMessagePromise({ this.hass.connection.sendMessagePromise({
type: 'frontend/get_themes', type: 'frontend/get_themes',
@ -335,14 +332,14 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
this.loadTranslationFragment(newPanelUrl); this.loadTranslationFragment(newPanelUrl);
} }
handleConnectionPromise(prom) { async handleConnectionPromise(prom) {
if (!prom) return; if (!prom) return;
prom.then((conn) => { try {
this.connection = conn; this.connection = await prom;
}, () => { } catch (err) {
this.connectionPromise = null; this.connectionPromise = null;
}); }
} }
handleMoreInfo(ev) { handleMoreInfo(ev) {
@ -392,10 +389,11 @@ class HomeAssistant extends LocalizeMixin(PolymerElement) {
} }
} }
_loadPanels() { async _loadPanels() {
this.connection.sendMessagePromise({ const msg = await this.connection.sendMessagePromise({
type: 'get_panels' type: 'get_panels'
}).then(msg => this._updateHass({ panels: msg.result })); });
this._updateHass({ panels: msg.result });
} }

View File

@ -1,5 +1,6 @@
import 'mdn-polyfills/Array.prototype.includes'; import 'mdn-polyfills/Array.prototype.includes';
import 'unfetch/polyfill'; import 'unfetch/polyfill';
import 'regenerator-runtime/runtime';
import objAssign from 'es6-object-assign'; import objAssign from 'es6-object-assign';
objAssign.polyfill(); objAssign.polyfill();