From 67517643ef6361262e469b5cad9e4c6a55e8603c Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 24 Oct 2023 21:08:23 +0200 Subject: [PATCH] Add safe mode theme and alert dialog --- src/state/disconnect-toast-mixin.ts | 83 ++++++++++++++++++----------- src/state/themes-mixin.ts | 15 +++++- src/translations/en.json | 8 ++- 3 files changed, 72 insertions(+), 34 deletions(-) diff --git a/src/state/disconnect-toast-mixin.ts b/src/state/disconnect-toast-mixin.ts index c74773c91325..f3c8059c5484 100644 --- a/src/state/disconnect-toast-mixin.ts +++ b/src/state/disconnect-toast-mixin.ts @@ -12,6 +12,7 @@ import { domainToName } from "../data/integration"; import { Constructor } from "../types"; import { showToast } from "../util/toast"; import { HassBaseEl } from "./hass-base-mixin"; +import { navigate } from "../common/navigate"; export default >(superClass: T) => class extends superClass { @@ -28,40 +29,62 @@ export default >(superClass: T) => updated(changedProperties) { super.updated(changedProperties); const oldHass = changedProperties.get("hass"); - if ( - !changedProperties.has("hass") || - !this.hass!.config || - oldHass?.config?.state === this.hass!.config.state - ) { + if (!changedProperties.has("hass") || !this.hass!.config) { return; } - if (this.hass!.config.state === STATE_NOT_RUNNING) { - showToast(this, { - message: - this.hass!.localize("ui.notification_toast.starting") || - "Home Assistant is starting, 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._unsubscribeBootstrapIntegrations(); + if (oldHass?.config?.state !== this.hass!.config.state) { + if (this.hass!.config.state === STATE_NOT_RUNNING) { + showToast(this, { + message: + this.hass!.localize("ui.notification_toast.starting") || + "Home Assistant is starting, 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._unsubscribeBootstrapIntegrations(); + }, }, - }, - }); - this._subscribeBootstrapIntegrations(); - } else if ( - oldHass?.config && - oldHass.config.state === STATE_NOT_RUNNING && - (this.hass!.config.state === STATE_STARTING || - this.hass!.config.state === STATE_RUNNING) + }); + this._subscribeBootstrapIntegrations(); + } else if ( + oldHass?.config && + oldHass.config.state === STATE_NOT_RUNNING && + (this.hass!.config.state === STATE_STARTING || + this.hass!.config.state === STATE_RUNNING) + ) { + this._unsubscribeBootstrapIntegrations(); + showToast(this, { + message: this.hass!.localize("ui.notification_toast.started"), + duration: 5000, + }); + } + } + if ( + this.hass!.config.safe_mode && + oldHass?.config?.safe_mode !== this.hass!.config.safe_mode ) { - this._unsubscribeBootstrapIntegrations(); - showToast(this, { - message: this.hass!.localize("ui.notification_toast.started"), - duration: 5000, - }); + import("../dialogs/generic/show-dialog-box").then( + ({ showAlertDialog }) => { + showAlertDialog(this, { + title: + this.hass!.localize("ui.dialogs.safe_mode.title") || + "Safe mode", + text: + this.hass!.localize("ui.dialogs.safe_mode.text") || + "Home Assistant is running in safe mode, custom integrations and modules are not available. Restart Home Assistant to exit safe mode.", + }); + } + ); + } + if ( + this.hass!.config.recovery_mode && + oldHass?.config?.recovery_mode !== this.hass!.config.recovery_mode + ) { + navigate("/"); } } diff --git a/src/state/themes-mixin.ts b/src/state/themes-mixin.ts index 9869f51156c3..034a3eb9aca8 100644 --- a/src/state/themes-mixin.ts +++ b/src/state/themes-mixin.ts @@ -71,7 +71,18 @@ export default >(superClass: T) => } let themeSettings: Partial = - this.hass.selectedTheme; + this.hass.config.recovery_mode || this.hass.config.safe_mode + ? { + ...this.hass.selectedTheme, + theme: "default", + primaryColor: this.hass.config.recovery_mode + ? "#db4437" + : "#e48629", + accentColor: this.hass.config.recovery_mode + ? "#ffca28" + : "#db4437", + } + : this.hass.selectedTheme; const themeName = themeSettings?.theme || @@ -90,7 +101,7 @@ export default >(superClass: T) => darkMode = false; } - themeSettings = { ...this.hass.selectedTheme, dark: darkMode }; + themeSettings = { ...themeSettings, dark: darkMode }; this._updateHass({ themes: { ...this.hass.themes!, theme: themeName }, }); diff --git a/src/translations/en.json b/src/translations/en.json index 2752f93c70e9..cc1ef6e6b15f 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -786,6 +786,10 @@ } }, "dialogs": { + "safe_mode": { + "title": "Safe mode", + "text": "Home Assistant is running in safe mode, custom integrations and frontend modules are not available. Restart Home Assistant to exit safe mode." + }, "quick-bar": { "commands": { "reload": { @@ -1236,9 +1240,9 @@ }, "restart-safe-mode": { "title": "Restart Home Assistant in safe mode", - "description": "Restart Home Assistant without loading any custom integrations and modules.", + "description": "Restart Home Assistant without loading any custom integrations and frontend modules.", "confirm_title": "Restart Home Assistant in safe mode?", - "confirm_description": "This will restart Home Assistant without loading any custom integrations and modules.", + "confirm_description": "This will restart Home Assistant without loading any custom integrations and frontend modules.", "confirm_action": "Restart", "failed": "Failed to restart Home Assistant" }