From e63d82d2913f19cbd0c68cc2628f244dcd19ec2a Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:53:17 -0700 Subject: [PATCH] Fix persistent notification count on server restart (#21405) * Fix persistent notification count on server restart --- src/components/ha-sidebar.ts | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/components/ha-sidebar.ts b/src/components/ha-sidebar.ts index da4b698616..bd705917aa 100644 --- a/src/components/ha-sidebar.ts +++ b/src/components/ha-sidebar.ts @@ -210,6 +210,8 @@ class HaSidebar extends SubscribeMixin(LitElement) { private _editStyleLoaded = false; + private _unsubPersistentNotifications: UnsubscribeFunc | undefined; + @storage({ key: "sidebarPanelOrder", state: true, @@ -283,15 +285,26 @@ class HaSidebar extends SubscribeMixin(LitElement) { hass.localize !== oldHass.localize || hass.locale !== oldHass.locale || hass.states !== oldHass.states || - hass.defaultPanel !== oldHass.defaultPanel + hass.defaultPanel !== oldHass.defaultPanel || + hass.connected !== oldHass.connected ); } protected firstUpdated(changedProps: PropertyValues) { super.firstUpdated(changedProps); - subscribeNotifications(this.hass.connection, (notifications) => { - this._notifications = notifications; - }); + this.subscribePersistentNotifications(); + } + + private subscribePersistentNotifications(): void { + if (this._unsubPersistentNotifications) { + this._unsubPersistentNotifications(); + } + this._unsubPersistentNotifications = subscribeNotifications( + this.hass.connection, + (notifications) => { + this._notifications = notifications; + } + ); } protected updated(changedProps) { @@ -306,6 +319,14 @@ class HaSidebar extends SubscribeMixin(LitElement) { return; } + if ( + this.hass && + changedProps.get("hass")?.connected === false && + this.hass.connected === true + ) { + this.subscribePersistentNotifications(); + } + this._calculateCounts(); if (!SUPPORT_SCROLL_IF_NEEDED) {