Fix disconnected notification (#1569)

This commit is contained in:
Paulus Schoutsen 2018-08-13 21:22:23 +02:00 committed by GitHub
parent 310299367b
commit 283668ef18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 44 deletions

View File

@ -153,15 +153,17 @@ export default superClass =>
const conn = this.hass.connection; const conn = this.hass.connection;
const reconnected = () => this.hassReconnected(); const reconnected = () => this.hassReconnected();
const disconnected = () => this._updateHass({ connected: false }); const disconnected = () => this.hassDisconnected();
const reconnectError = async (_conn, err) => { const reconnectError = async (_conn, err) => {
if (err !== ERR_INVALID_AUTH) return; if (err !== ERR_INVALID_AUTH) return;
disconnected();
while (this.unsubFuncs.length) { while (this.unsubFuncs.length) {
this.unsubFuncs.pop()(); this.unsubFuncs.pop()();
} }
const accessToken = await refreshToken(); const accessToken = await refreshToken();
this._handleNewConnProm(window.createHassConnection(null, accessToken)); const newConn = window.createHassConnection(null, accessToken);
newConn.then(() => this.hassReconnected());
this._handleNewConnProm(newConn);
}; };
conn.addEventListener('ready', reconnected); conn.addEventListener('ready', reconnected);
@ -191,6 +193,11 @@ export default superClass =>
this._loadPanels(); this._loadPanels();
} }
hassDisconnected() {
super.hassDisconnected();
this._updateHass({ connected: false });
}
async _loadPanels() { async _loadPanels() {
const panels = await this.hass.callWS({ const panels = await this.hass.callWS({
type: 'get_panels' type: 'get_panels'

View File

@ -0,0 +1,27 @@
import LocalizeMixin from '../../mixins/localize-mixin.js';
export default superClass =>
class extends LocalizeMixin(superClass) {
hassConnected() {
super.hassConnected();
// Need to load in advance because when disconnected, can't dynamically load code.
import(/* webpackChunkName: "ha-toast" */ '../../components/ha-toast.js');
}
hassReconnected() {
super.hassReconnected();
this.__discToast.opened = false;
}
hassDisconnected() {
super.hassDisconnected();
if (!this.__discToast) {
const el = document.createElement('ha-toast');
el.duration = 0;
el.text = this.localize('ui.notification_toast.connection_lost');
this.__discToast = el;
this.shadowRoot.appendChild(el);
}
this.__discToast.opened = true;
}
};

View File

@ -9,6 +9,7 @@ export default superClass => class extends superClass {
// Exists so all methods can safely call super method // Exists so all methods can safely call super method
hassConnected() {} hassConnected() {}
hassReconnected() {} hassReconnected() {}
hassDisconnected() {}
panelUrlChanged(newPanelUrl) {} panelUrlChanged(newPanelUrl) {}
hassChanged(hass, oldHass) { hassChanged(hass, oldHass) {
this.__provideHass.forEach((el) => { this.__provideHass.forEach((el) => {

View File

@ -18,6 +18,7 @@ import SidebarMixin from './sidebar-mixin.js';
import DialogManagerMixin from './dialog-manager-mixin.js'; import DialogManagerMixin from './dialog-manager-mixin.js';
import ConnectionMixin from './connection-mixin.js'; import ConnectionMixin from './connection-mixin.js';
import NotificationMixin from './notification-mixin.js'; import NotificationMixin from './notification-mixin.js';
import DisconnectToastMixin from './disconnect-toast-mixin.js';
import(/* webpackChunkName: "login-form" */ '../../layouts/login-form.js'); import(/* webpackChunkName: "login-form" */ '../../layouts/login-form.js');
@ -29,6 +30,7 @@ class HomeAssistant extends ext(PolymerElement, [
TranslationsMixin, TranslationsMixin,
MoreInfoMixin, MoreInfoMixin,
SidebarMixin, SidebarMixin,
DisconnectToastMixin,
ConnectionMixin, ConnectionMixin,
NotificationMixin, NotificationMixin,
DialogManagerMixin, DialogManagerMixin,

View File

@ -3,8 +3,6 @@ import { PolymerElement } from '@polymer/polymer/polymer-element.js';
import LocalizeMixin from '../mixins/localize-mixin.js'; import LocalizeMixin from '../mixins/localize-mixin.js';
import '../components/ha-toast.js';
class NotificationManager extends LocalizeMixin(PolymerElement) { class NotificationManager extends LocalizeMixin(PolymerElement) {
static get template() { static get template() {
return html` return html`
@ -18,58 +16,23 @@ class NotificationManager extends LocalizeMixin(PolymerElement) {
id="toast" id="toast"
no-cancel-on-outside-click="[[_cancelOnOutsideClick]]" no-cancel-on-outside-click="[[_cancelOnOutsideClick]]"
></ha-toast> ></ha-toast>
<ha-toast
id="connToast"
duration="0"
text="[[localize('ui.notification_toast.connection_lost')]]"
opened="[[connectionLost]]"
></ha-toast>
`; `;
} }
static get properties() { static get properties() {
return { return {
hass: { hass: Object,
type: Object,
observer: 'hassChanged',
},
wasConnected: {
type: Boolean,
value: false,
},
connectionLost: {
type: Boolean,
computed: 'computeConnectionLost(wasConnected, hass)',
},
_cancelOnOutsideClick: { _cancelOnOutsideClick: {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
toastClass: {
type: String,
value: '',
},
}; };
} }
hassChanged(hass) { ready() {
if (hass && hass.connected) { super.ready();
// Once the connetion is established, set wasConnected to true import(/* webpackChunkName: "ha-toast" */ '../components/ha-toast.js');
this.wasConnected = true;
}
if (!hass || !hass.connection) {
// If the users logs out, reset wasConnected
this.wasConnected = false;
}
}
computeConnectionLost(wasConnected, hass) {
return wasConnected && hass && !hass.connected;
} }
showDialog({ message }) { showDialog({ message }) {