From 66d580e21e784076282ecf2b7b67c10af5ccd0b9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 16:03:24 -1000 Subject: [PATCH 01/14] Show which integrations are being setup at startup --- src/state/disconnect-toast-mixin.ts | 82 ++++++++++++++++++++++++++++- src/translations/en.json | 2 + 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 1bf402ce8ff3..1f691bc082f5 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -9,6 +9,10 @@ import { HassBaseEl } from "./hass-base-mixin"; export default >(superClass: T) => class extends superClass { + private _subscribed?: Promise<() => Promise>; + + private _dismissed = false; + protected firstUpdated(changedProps) { super.firstUpdated(changedProps); // Need to load in advance because when disconnected, can't dynamically load code. @@ -35,15 +39,19 @@ export default >(superClass: T) => action: { text: this.hass!.localize("ui.notification_toast.dismiss") || "Dismiss", - action: () => {}, + action: () => { + this._dismissed = true; + }, }, }); + this._subscribeBootstrapIntergrations(); } else if ( oldHass?.config && oldHass.config.state === STATE_NOT_RUNNING && (this.hass!.config.state === STATE_STARTING || this.hass!.config.state === STATE_RUNNING) ) { + this._unsubscribeBootstrapIntergrations(); showToast(this, { message: this.hass!.localize("ui.notification_toast.started"), duration: 5000, @@ -61,11 +69,81 @@ export default >(superClass: T) => protected hassDisconnected() { super.hassDisconnected(); - showToast(this, { message: this.hass!.localize("ui.notification_toast.connection_lost"), duration: 0, dismissable: false, }); } + + private _handleMessage(message: any): void { + if (this._dismissed || this.hass!.config.state !== STATE_NOT_RUNNING) { + return; + } + + if (Object.keys(message).length === 0) { + showToast(this, { + message: + this.hass!.localize("ui.notification_toast.wrapping_up_startup") || + `Wrapping up startup, not everything will be available until it is finished.`, + duration: 0, + dismissable: false, + action: { + text: + this.hass!.localize("ui.notification_toast.dismiss") || "Dismiss", + action: () => { + this._dismissed = true; + }, + }, + }); + return; + } + + // Show the integration that has been starting for the longest time + const max = Object.keys(message).reduce( + (a, v) => Math.max(a, message[v]), + -Infinity + ); + const integration = Object.keys(message).filter( + (v) => message[v] === max + ); + + showToast(this, { + message: + this.hass!.localize( + "ui.notification_toast.intergration_starting", + "integration", + integration + ) || + `Starting ${integration}, not everything will be available until it is finished.`, + duration: 0, + dismissable: false, + action: { + text: + this.hass!.localize("ui.notification_toast.dismiss") || "Dismiss", + action: () => { + this._dismissed = true; + }, + }, + }); + } + + private _unsubscribeBootstrapIntergrations() { + if (this._subscribed) { + this._subscribed.then((unsub) => unsub()); + this._subscribed = undefined; + } + } + + private _subscribeBootstrapIntergrations() { + if (!this.hass) { + return; + } + this._subscribed = this.hass.connection.subscribeMessage( + (message) => this._handleMessage(message), + { + type: "subscribe_bootstrap_integrations", + } + ); + } }; diff --git a/src/translations/en.json b/src/translations/en.json index e187f9656d1c..ff1defa46d57 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -850,6 +850,8 @@ "connection_lost": "Connection lost. Reconnecting…", "started": "Home Assistant has started!", "starting": "Home Assistant is starting, not everything will be available until it is finished.", + "wrapping_up_startup": "Wrapping up startup, not everything will be available until it is finished.", + "intergration_starting": "Starting {integration}, not everything will be available until it is finished.", "triggered": "Triggered {name}", "dismiss": "Dismiss" }, From 7f4be0bd5080b85883192d603b7a36a39bed7952 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 22:55:04 -1000 Subject: [PATCH 02/14] Update src/state/disconnect-toast-mixin.ts Co-authored-by: Bram Kragten --- src/state/disconnect-toast-mixin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 1f691bc082f5..55d318767bfb 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -113,7 +113,7 @@ export default >(superClass: T) => this.hass!.localize( "ui.notification_toast.intergration_starting", "integration", - integration + domainToName(this.hass.localize, integration) ) || `Starting ${integration}, not everything will be available until it is finished.`, duration: 0, From b6e747c85b53de550eec0b4d91e9869cb27b985c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 22:55:35 -1000 Subject: [PATCH 03/14] Update src/state/disconnect-toast-mixin.ts Co-authored-by: Bram Kragten --- src/state/disconnect-toast-mixin.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 55d318767bfb..99050cf20efb 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -100,13 +100,7 @@ export default >(superClass: T) => } // Show the integration that has been starting for the longest time - const max = Object.keys(message).reduce( - (a, v) => Math.max(a, message[v]), - -Infinity - ); - const integration = Object.keys(message).filter( - (v) => message[v] === max - ); + const [integration, max] = Object.entries(message).sort(([,a],[,b]) => b-a)[0]; showToast(this, { message: From 96f20b06a2fe1870e432be7fa8ceef9097c420fc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 23:12:01 -1000 Subject: [PATCH 04/14] review adjustments --- src/state/disconnect-toast-mixin.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 99050cf20efb..4ab4c8871ae9 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -6,6 +6,7 @@ import { import { Constructor } from "../types"; import { showToast } from "../util/toast"; import { HassBaseEl } from "./hass-base-mixin"; +import { domainToName } from "../data/integration"; export default >(superClass: T) => class extends superClass { @@ -77,7 +78,7 @@ export default >(superClass: T) => } private _handleMessage(message: any): void { - if (this._dismissed || this.hass!.config.state !== STATE_NOT_RUNNING) { + if (this.hass!.config.state !== STATE_NOT_RUNNING) { return; } @@ -92,7 +93,7 @@ export default >(superClass: T) => text: this.hass!.localize("ui.notification_toast.dismiss") || "Dismiss", action: () => { - this._dismissed = true; + this._unsubscribeBootstrapIntergrations(); }, }, }); @@ -100,14 +101,16 @@ export default >(superClass: T) => } // Show the integration that has been starting for the longest time - const [integration, max] = Object.entries(message).sort(([,a],[,b]) => b-a)[0]; + const integration = Object.entries(message).sort( + ([, a], [, b]) => b - a + )[0][0]; showToast(this, { message: this.hass!.localize( "ui.notification_toast.intergration_starting", "integration", - domainToName(this.hass.localize, integration) + domainToName(this.hass!.localize, integration) ) || `Starting ${integration}, not everything will be available until it is finished.`, duration: 0, @@ -116,7 +119,7 @@ export default >(superClass: T) => text: this.hass!.localize("ui.notification_toast.dismiss") || "Dismiss", action: () => { - this._dismissed = true; + this._unsubscribeBootstrapIntergrations(); }, }, }); From 8a75a5c9b1ce10326cc3f11b340c0427cc2a2b3f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 23:20:12 -1000 Subject: [PATCH 05/14] review adjustments --- src/state/disconnect-toast-mixin.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 4ab4c8871ae9..98b1f9bece49 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -10,9 +10,7 @@ import { domainToName } from "../data/integration"; export default >(superClass: T) => class extends superClass { - private _subscribed?: Promise<() => Promise>; - - private _dismissed = false; + private _subscribedBootstrapIntegrations?: Promise<() => Promise>; protected firstUpdated(changedProps) { super.firstUpdated(changedProps); @@ -41,7 +39,7 @@ export default >(superClass: T) => text: this.hass!.localize("ui.notification_toast.dismiss") || "Dismiss", action: () => { - this._dismissed = true; + this._subscribeBootstrapIntergrations(); }, }, }); @@ -126,9 +124,9 @@ export default >(superClass: T) => } private _unsubscribeBootstrapIntergrations() { - if (this._subscribed) { - this._subscribed.then((unsub) => unsub()); - this._subscribed = undefined; + if (this._subscribedBootstrapIntegrations) { + this._subscribedBootstrapIntegrations.then((unsub) => unsub()); + this._subscribedBootstrapIntegrations = undefined; } } @@ -136,7 +134,7 @@ export default >(superClass: T) => if (!this.hass) { return; } - this._subscribed = this.hass.connection.subscribeMessage( + this._subscribedBootstrapIntegrations = this.hass.connection.subscribeMessage( (message) => this._handleMessage(message), { type: "subscribe_bootstrap_integrations", From 8a15c740deb8fa8f637b2d1a3201e88a9eaf19ea Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 23:23:17 -1000 Subject: [PATCH 06/14] remove impossible --- src/state/disconnect-toast-mixin.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 98b1f9bece49..25eb26058a5b 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -39,7 +39,7 @@ export default >(superClass: T) => text: this.hass!.localize("ui.notification_toast.dismiss") || "Dismiss", action: () => { - this._subscribeBootstrapIntergrations(); + this._unsubscribeBootstrapIntergrations(); }, }, }); @@ -90,9 +90,7 @@ export default >(superClass: T) => action: { text: this.hass!.localize("ui.notification_toast.dismiss") || "Dismiss", - action: () => { - this._unsubscribeBootstrapIntergrations(); - }, + action: () => {}, }, }); return; From abdf8c52128e23bb80eeb7ac9d58e7156d1da446 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 23:34:25 -1000 Subject: [PATCH 07/14] type --- src/state/disconnect-toast-mixin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 25eb26058a5b..59fcd6b09868 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -75,7 +75,7 @@ export default >(superClass: T) => }); } - private _handleMessage(message: any): void { + private _handleMessage(message: { string: number }): void { if (this.hass!.config.state !== STATE_NOT_RUNNING) { return; } From 6a9f42482ff6d1814ae2c7364e58807df1b71f74 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 23:47:29 -1000 Subject: [PATCH 08/14] Update src/state/disconnect-toast-mixin.ts Co-authored-by: Bram Kragten --- src/state/disconnect-toast-mixin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 59fcd6b09868..ea580ab255f5 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -75,7 +75,7 @@ export default >(superClass: T) => }); } - private _handleMessage(message: { string: number }): void { + private _handleMessage(message: { [key: string]: number }): void { if (this.hass!.config.state !== STATE_NOT_RUNNING) { return; } From 7611d4ef408406835131cf5bb57c9349ed930b95 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 23:47:38 -1000 Subject: [PATCH 09/14] Update src/state/disconnect-toast-mixin.ts Co-authored-by: Bram Kragten --- src/state/disconnect-toast-mixin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index ea580ab255f5..8109f96d06d1 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -132,7 +132,7 @@ export default >(superClass: T) => if (!this.hass) { return; } - this._subscribedBootstrapIntegrations = this.hass.connection.subscribeMessage( + this._subscribedBootstrapIntegrations = this.hass.connection.subscribeMessage<{[key: string]: number}>( (message) => this._handleMessage(message), { type: "subscribe_bootstrap_integrations", From 2e07f3fead7f52f7ca7cdf7d70c0d4186b4d3ff4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 23:56:44 -1000 Subject: [PATCH 10/14] move to data --- src/data/bootstrap_integrations.ts | 16 ++++++++++++++++ src/state/disconnect-toast-mixin.ts | 9 +++++---- 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 src/data/bootstrap_integrations.ts diff --git a/src/data/bootstrap_integrations.ts b/src/data/bootstrap_integrations.ts new file mode 100644 index 000000000000..9cf950d8fe05 --- /dev/null +++ b/src/data/bootstrap_integrations.ts @@ -0,0 +1,16 @@ +import { HomeAssistant } from "../types"; + +type BootstrapIntegrationsTiming = { [key: string]: number }; + +export const subscribeBootstrapIntegrations = ( + hass: HomeAssistant, + callback: (message: BootstrapIntegrationsTiming) => void +) => { + const unsubProm = hass.connection.subscribeMessage< + BootstrapIntegrationsTiming + >((message) => callback(message), { + type: "subscribe_bootstrap_integrations", + }); + + return unsubProm; +}; diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 8109f96d06d1..a9452b5161c0 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -7,6 +7,7 @@ import { Constructor } from "../types"; import { showToast } from "../util/toast"; import { HassBaseEl } from "./hass-base-mixin"; import { domainToName } from "../data/integration"; +import { subscribeBootstrapIntegrations } from "../data/bootstrap_integrations"; export default >(superClass: T) => class extends superClass { @@ -132,10 +133,10 @@ export default >(superClass: T) => if (!this.hass) { return; } - this._subscribedBootstrapIntegrations = this.hass.connection.subscribeMessage<{[key: string]: number}>( - (message) => this._handleMessage(message), - { - type: "subscribe_bootstrap_integrations", + this._subscribedBootstrapIntegrations = subscribeBootstrapIntegrations( + this.hass!, + (message) => { + this._handleMessage(message); } ); } From 8477f2dd50068a025f95f1cf83bbbbae012804db Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 6 Apr 2021 23:59:06 -1000 Subject: [PATCH 11/14] move to data --- src/state/disconnect-toast-mixin.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index a9452b5161c0..3bce553ecf16 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -7,7 +7,10 @@ import { Constructor } from "../types"; import { showToast } from "../util/toast"; import { HassBaseEl } from "./hass-base-mixin"; import { domainToName } from "../data/integration"; -import { subscribeBootstrapIntegrations } from "../data/bootstrap_integrations"; +import { + subscribeBootstrapIntegrations, + BootstrapIntegrationsTiming, +} from "../data/bootstrap_integrations"; export default >(superClass: T) => class extends superClass { @@ -76,7 +79,7 @@ export default >(superClass: T) => }); } - private _handleMessage(message: { [key: string]: number }): void { + private _handleMessage(message: BootstrapIntegrationsTiming): void { if (this.hass!.config.state !== STATE_NOT_RUNNING) { return; } From 95ca2198d0039d46b3afce2e2877a2fcb20d9508 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Apr 2021 00:03:14 -1000 Subject: [PATCH 12/14] move to data --- src/data/bootstrap_integrations.ts | 6 +++--- src/state/disconnect-toast-mixin.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/data/bootstrap_integrations.ts b/src/data/bootstrap_integrations.ts index 9cf950d8fe05..864cc2efef89 100644 --- a/src/data/bootstrap_integrations.ts +++ b/src/data/bootstrap_integrations.ts @@ -1,13 +1,13 @@ import { HomeAssistant } from "../types"; -type BootstrapIntegrationsTiming = { [key: string]: number }; +export type BootstrapIntegrationsTimings = { [key: string]: number }; export const subscribeBootstrapIntegrations = ( hass: HomeAssistant, - callback: (message: BootstrapIntegrationsTiming) => void + callback: (message: BootstrapIntegrationsTimings) => void ) => { const unsubProm = hass.connection.subscribeMessage< - BootstrapIntegrationsTiming + BootstrapIntegrationsTimings >((message) => callback(message), { type: "subscribe_bootstrap_integrations", }); diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 3bce553ecf16..10c0f32cf93b 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -9,7 +9,7 @@ import { HassBaseEl } from "./hass-base-mixin"; import { domainToName } from "../data/integration"; import { subscribeBootstrapIntegrations, - BootstrapIntegrationsTiming, + BootstrapIntegrationsTimings, } from "../data/bootstrap_integrations"; export default >(superClass: T) => @@ -79,7 +79,7 @@ export default >(superClass: T) => }); } - private _handleMessage(message: BootstrapIntegrationsTiming): void { + private _handleMessage(message: BootstrapIntegrationsTimings): void { if (this.hass!.config.state !== STATE_NOT_RUNNING) { return; } From 1d52286d42476fac3b3379c23eaec40c4f90ff03 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Apr 2021 06:11:47 -1000 Subject: [PATCH 13/14] Update src/state/disconnect-toast-mixin.ts Co-authored-by: Bram Kragten --- src/state/disconnect-toast-mixin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index 10c0f32cf93b..ad6cc2c68b88 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -14,7 +14,7 @@ import { export default >(superClass: T) => class extends superClass { - private _subscribedBootstrapIntegrations?: Promise<() => Promise>; + private _subscribedBootstrapIntegrations?: Promise; protected firstUpdated(changedProps) { super.firstUpdated(changedProps); From ebe9cfe9d26a367456783809f3dafffb47d2fbd1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Apr 2021 06:17:34 -1000 Subject: [PATCH 14/14] import --- src/state/disconnect-toast-mixin.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index ad6cc2c68b88..09efcc3f893a 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -2,6 +2,7 @@ import { STATE_NOT_RUNNING, STATE_RUNNING, STATE_STARTING, + UnsubscribeFunc, } from "home-assistant-js-websocket"; import { Constructor } from "../types"; import { showToast } from "../util/toast"; @@ -72,6 +73,7 @@ export default >(superClass: T) => protected hassDisconnected() { super.hassDisconnected(); + showToast(this, { message: this.hass!.localize("ui.notification_toast.connection_lost"), duration: 0,