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 reconnected = () => this.hassReconnected();
const disconnected = () => this._updateHass({ connected: false });
const disconnected = () => this.hassDisconnected();
const reconnectError = async (_conn, err) => {
if (err !== ERR_INVALID_AUTH) return;
disconnected();
while (this.unsubFuncs.length) {
this.unsubFuncs.pop()();
}
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);
@ -191,6 +193,11 @@ export default superClass =>
this._loadPanels();
}
hassDisconnected() {
super.hassDisconnected();
this._updateHass({ connected: false });
}
async _loadPanels() {
const panels = await this.hass.callWS({
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
hassConnected() {}
hassReconnected() {}
hassDisconnected() {}
panelUrlChanged(newPanelUrl) {}
hassChanged(hass, oldHass) {
this.__provideHass.forEach((el) => {

View File

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

View File

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