Small Polymer 2 tweaks
parent
512b07963b
commit
17519cce30
|
@ -98,9 +98,9 @@ Polymer({
|
||||||
loadIcons: function () {
|
loadIcons: function () {
|
||||||
// If the import fails, we'll try to import again, must be a server glitch
|
// If the import fails, we'll try to import again, must be a server glitch
|
||||||
// Since HTML imports only resolve once, we import another url.
|
// Since HTML imports only resolve once, we import another url.
|
||||||
var success = function () {
|
const success = () => {
|
||||||
this.iconsLoaded = true;
|
this.iconsLoaded = true;
|
||||||
}.bind(this);
|
};
|
||||||
|
|
||||||
this.importHref('/static/mdi-' + this.icons + '.html',
|
this.importHref('/static/mdi-' + this.icons + '.html',
|
||||||
success,
|
success,
|
||||||
|
@ -119,7 +119,6 @@ Polymer({
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var notifications = this.$.notifications;
|
var notifications = this.$.notifications;
|
||||||
var el = this;
|
|
||||||
this.hass = Object.assign({
|
this.hass = Object.assign({
|
||||||
connection: conn,
|
connection: conn,
|
||||||
connected: true,
|
connected: true,
|
||||||
|
@ -128,14 +127,14 @@ Polymer({
|
||||||
themes: null,
|
themes: null,
|
||||||
dockedSidebar: false,
|
dockedSidebar: false,
|
||||||
moreInfoEntityId: null,
|
moreInfoEntityId: null,
|
||||||
callService: function (domain, service, serviceData) {
|
callService: (domain, service, serviceData) =>
|
||||||
return conn.callService(domain, service, serviceData || {})
|
conn.callService(domain, service, serviceData || {})
|
||||||
.then(function () {
|
.then(() => {
|
||||||
var message;
|
var message;
|
||||||
var name;
|
var name;
|
||||||
if (serviceData.entity_id && el.hass.states &&
|
if (serviceData.entity_id && this.hass.states &&
|
||||||
el.hass.states[serviceData.entity_id]) {
|
this.hass.states[serviceData.entity_id]) {
|
||||||
name = window.hassUtil.computeStateName(el.hass.states[serviceData.entity_id]);
|
name = window.hassUtil.computeStateName(this.hass.states[serviceData.entity_id]);
|
||||||
}
|
}
|
||||||
if (service === 'turn_on' && serviceData.entity_id) {
|
if (service === 'turn_on' && serviceData.entity_id) {
|
||||||
message = 'Turned on ' + (name || serviceData.entity_id) + '.';
|
message = 'Turned on ' + (name || serviceData.entity_id) + '.';
|
||||||
|
@ -150,52 +149,51 @@ Polymer({
|
||||||
notifications.showNotification(
|
notifications.showNotification(
|
||||||
'Failed to call service ' + domain + '/' + service);
|
'Failed to call service ' + domain + '/' + service);
|
||||||
return Promise.reject();
|
return Promise.reject();
|
||||||
});
|
}),
|
||||||
},
|
callApi: (method, path, parameters) => {
|
||||||
callApi: function (method, path, parameters) {
|
|
||||||
var host = window.location.protocol + '//' + window.location.host;
|
var host = window.location.protocol + '//' + window.location.host;
|
||||||
var auth = conn.options.authToken ? conn.options : {};
|
var auth = conn.options.authToken ? conn.options : {};
|
||||||
return window.hassCallApi(host, auth, method, path, parameters);
|
return window.hassCallApi(host, auth, method, path, parameters);
|
||||||
},
|
},
|
||||||
}, this.$.storage.getStoredState());
|
}, this.$.storage.getStoredState());
|
||||||
|
|
||||||
var reconnected = function () {
|
var reconnected = () => {
|
||||||
this.hass = Object.assign({}, this.hass, { connected: true });
|
this.hass = Object.assign({}, this.hass, { connected: true });
|
||||||
}.bind(this);
|
};
|
||||||
|
|
||||||
conn.addEventListener('ready', reconnected);
|
conn.addEventListener('ready', reconnected);
|
||||||
|
|
||||||
var disconnected = function () {
|
var disconnected = () => {
|
||||||
this.hass = Object.assign({}, this.hass, { connected: false });
|
this.hass = Object.assign({}, this.hass, { connected: false });
|
||||||
}.bind(this);
|
};
|
||||||
|
|
||||||
conn.addEventListener('disconnected', disconnected);
|
conn.addEventListener('disconnected', disconnected);
|
||||||
|
|
||||||
var unsubEntities;
|
var unsubEntities;
|
||||||
|
|
||||||
window.HAWS.subscribeEntities(conn, function (states) {
|
window.HAWS.subscribeEntities(conn, (states) => {
|
||||||
this.hass = Object.assign({}, this.hass, { states: states });
|
this.hass = Object.assign({}, this.hass, { states: states });
|
||||||
}.bind(this)).then(function (unsub) {
|
}).then(function (unsub) {
|
||||||
unsubEntities = unsub;
|
unsubEntities = unsub;
|
||||||
});
|
});
|
||||||
|
|
||||||
var unsubConfig;
|
var unsubConfig;
|
||||||
|
|
||||||
window.HAWS.subscribeConfig(conn, function (config) {
|
window.HAWS.subscribeConfig(conn, (config) => {
|
||||||
this.hass = Object.assign({}, this.hass, { config: config });
|
this.hass = Object.assign({}, this.hass, { config: config });
|
||||||
}.bind(this)).then(function (unsub) {
|
}).then(function (unsub) {
|
||||||
unsubConfig = unsub;
|
unsubConfig = unsub;
|
||||||
});
|
});
|
||||||
|
|
||||||
var unsubThemes;
|
var unsubThemes;
|
||||||
|
|
||||||
this.hass.callApi('get', 'themes').then(function (themes) {
|
this.hass.callApi('get', 'themes').then((themes) => {
|
||||||
el.hass.themes = themes;
|
this.hass.themes = themes;
|
||||||
window.hassUtil.applyThemesOnElement(el, themes);
|
window.hassUtil.applyThemesOnElement(this, themes);
|
||||||
});
|
});
|
||||||
conn.subscribeEvents(function (event) {
|
conn.subscribeEvents((event) => {
|
||||||
el.hass.themes = event.data;
|
this.hass.themes = event.data;
|
||||||
window.hassUtil.applyThemesOnElement(el, event.data);
|
window.hassUtil.applyThemesOnElement(this, event.data);
|
||||||
}, 'themes_updated').then(function (unsub) {
|
}, 'themes_updated').then(function (unsub) {
|
||||||
unsubThemes = unsub;
|
unsubThemes = unsub;
|
||||||
});
|
});
|
||||||
|
@ -212,12 +210,10 @@ Polymer({
|
||||||
handleConnectionPromise: function (prom) {
|
handleConnectionPromise: function (prom) {
|
||||||
if (!prom) return;
|
if (!prom) return;
|
||||||
|
|
||||||
var el = this;
|
prom.then((conn) => {
|
||||||
|
this.connection = conn;
|
||||||
prom.then(function (conn) {
|
}, () => {
|
||||||
el.connection = conn;
|
this.connectionPromise = null;
|
||||||
}, function () {
|
|
||||||
el.connectionPromise = null;
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -256,10 +252,6 @@ Polymer({
|
||||||
|
|
||||||
ready: function () {
|
ready: function () {
|
||||||
this.loadIcons();
|
this.loadIcons();
|
||||||
|
|
||||||
if (this.connectionPromise !== null) {
|
|
||||||
this.handleConnectionPromise(this.connectionPromise);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -226,7 +226,7 @@ Polymer({
|
||||||
},
|
},
|
||||||
|
|
||||||
areTabsHidden: function (views, showTabs) {
|
areTabsHidden: function (views, showTabs) {
|
||||||
return !views.length || !showTabs;
|
return !views || !views.length || !showTabs;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue