From cc0c96b8b4b545cb8157226b30543aab614d4ad7 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 4 May 2022 13:09:54 +0200 Subject: [PATCH] Make update notification better, add progress (#12581) --- src/data/update.ts | 29 +++++++++++++++---- .../config/dashboard/ha-config-updates.ts | 26 ++++++++++++++++- src/translations/en.json | 2 +- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/data/update.ts b/src/data/update.ts index e76a8947a3..f888823d21 100644 --- a/src/data/update.ts +++ b/src/data/update.ts @@ -2,8 +2,10 @@ import type { HassEntities, HassEntityAttributeBase, HassEntityBase, + HassEvent, } from "home-assistant-js-websocket"; import { BINARY_STATE_ON } from "../common/const"; +import { computeDomain } from "../common/entity/compute_domain"; import { computeStateDomain } from "../common/entity/compute_state_domain"; import { supportsFeature } from "../common/entity/supports-feature"; import { caseInsensitiveStringCompare } from "../common/string/compare"; @@ -110,15 +112,32 @@ export const checkForEntityUpdates = async ( return; } + let updated = 0; + + const unsubscribeEvents = await hass.connection.subscribeEvents( + (event) => { + if (computeDomain(event.data.entity_id) === "update") { + updated++; + showToast(element, { + message: hass.localize("ui.panel.config.updates.updates_refreshed", { + count: updated, + }), + }); + } + }, + "state_changed" + ); + await hass.callService("homeassistant", "update_entity", { entity_id: entities, }); - if (filterUpdateEntitiesWithInstall(hass.states).length) { - showToast(element, { - message: hass.localize("ui.panel.config.updates.updates_refreshed"), - }); - } else { + // there is no reliable way to know if all the updates are done updating, so we just wait a bit for now... + await new Promise((r) => setTimeout(r, 10000)); + + unsubscribeEvents(); + + if (updated === 0) { showToast(element, { message: hass.localize("ui.panel.config.updates.no_new_updates"), }); diff --git a/src/panels/config/dashboard/ha-config-updates.ts b/src/panels/config/dashboard/ha-config-updates.ts index 416b0d9d8d..dcb02c4de4 100644 --- a/src/panels/config/dashboard/ha-config-updates.ts +++ b/src/panels/config/dashboard/ha-config-updates.ts @@ -9,6 +9,7 @@ import "../../../components/ha-alert"; import "../../../components/ha-icon-next"; import type { UpdateEntity } from "../../../data/update"; import type { HomeAssistant } from "../../../types"; +import "../../../components/ha-circular-progress"; @customElement("ha-config-updates") class HaConfigUpdates extends LitElement { @@ -51,7 +52,18 @@ class HaConfigUpdates extends LitElement { .title=${entity.attributes.title || entity.attributes.friendly_name} .stateObj=${entity} + class=${this.narrow && entity.attributes.in_progress + ? "updating" + : ""} > + ${this.narrow && entity.attributes.in_progress + ? html`` + : ""} ${entity.attributes.title || entity.attributes.friendly_name} ${!this.narrow - ? html`` + ? entity.attributes.in_progress + ? html`` + : html`` : ""} ` @@ -121,6 +139,12 @@ class HaConfigUpdates extends LitElement { cursor: pointer; font-size: 16px; } + ha-circular-progress.absolute { + position: absolute; + } + state-badge.updating { + opacity: 0.5; + } `, ]; } diff --git a/src/translations/en.json b/src/translations/en.json index 8b0a865f77..649aea8f90 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -1183,7 +1183,7 @@ }, "check_updates": "Check for updates", "no_new_updates": "No new updates found", - "updates_refreshed": "Updates refreshed", + "updates_refreshed": "{count} {count, plural,\n one {update}\n other {updates}\n} refreshed", "title": "{count} {count, plural,\n one {update}\n other {updates}\n}", "unable_to_fetch": "Unable to load updates", "version_available": "Version {version_available} is available",