Skip to content

Commit

Permalink
Change emergency kit format (#23428)
Browse files Browse the repository at this point in the history
* Change emergency kit format

* Update src/panels/config/backup/ha-config-backup-settings.ts

* review
  • Loading branch information
bramkragten authored Dec 24, 2024
1 parent 657bfc8 commit 637fe37
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 32 deletions.
44 changes: 44 additions & 0 deletions src/data/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import type { LocalizeFunc } from "../common/translations/localize";
import type { HomeAssistant } from "../types";
import { domainToName } from "./integration";
import type { FrontendLocaleData } from "./translation";
import {
formatDateTime,
formatDateTimeNumeric,
} from "../common/datetime/format_date_time";
import { fileDownload } from "../util/file_download";

export const enum BackupScheduleState {
NEVER = "never",
Expand Down Expand Up @@ -288,6 +293,45 @@ export const generateEncryptionKey = () => {
return result;
};

export const generateEmergencyKit = (
hass: HomeAssistant,
encryptionKey: string
) =>
"data:text/plain;charset=utf-8," +
encodeURIComponent(`Home Assistant Backup Emergency Kit
This emergency kit contains your backup encryption key. You need this key
to be able to restore your Home Assistant backups.
Date: ${formatDateTime(new Date(), hass.locale, hass.config)}
Instance:
${hass.config.location_name}
URL:
${hass.auth.data.hassUrl}
Encryption key:
${encryptionKey}
For more information visit: https://www.home-assistant.io/more-info/backup-emergency-kit`);

export const geneateEmergencyKitFileName = (
hass: HomeAssistant,
append?: string
) =>
`home_assistant_backup_emergency_kit_${append ? `${append}_` : ""}${formatDateTimeNumeric(new Date(), hass.locale, hass.config).replace(",", "").replace(" ", "_")}.txt`;

export const downloadEmergencyKit = (
hass: HomeAssistant,
key: string,
appendFileName?: string
) =>
fileDownload(
generateEmergencyKit(hass, key),
geneateEmergencyKitFileName(hass, appendFileName)
);

export const getFormattedBackupTime = memoizeOne(
(locale: FrontendLocaleData, config: HassConfig) => {
const date = setMinutes(setHours(new Date(), 4), 45);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import "../../../../../components/ha-md-list";
import "../../../../../components/ha-md-list-item";
import type { HomeAssistant } from "../../../../../types";
import { showChangeBackupEncryptionKeyDialog } from "../../dialogs/show-dialog-change-backup-encryption-key";
import { fileDownload } from "../../../../../util/file_download";
import { showSetBackupEncryptionKeyDialog } from "../../dialogs/show-dialog-set-backup-encryption-key";

import { downloadEmergencyKit } from "../../../../../data/backup";

@customElement("ha-backup-config-encryption-key")
class HaBackupConfigEncryptionKey extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
Expand Down Expand Up @@ -64,10 +65,7 @@ class HaBackupConfigEncryptionKey extends LitElement {
if (!this._value) {
return;
}
fileDownload(
"data:text/plain;charset=utf-8," + encodeURIComponent(this._value),
"emergency_kit.txt"
);
downloadEmergencyKit(this.hass, this._value);
}

private _change() {
Expand Down
8 changes: 3 additions & 5 deletions src/panels/config/backup/dialogs/dialog-backup-onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import {
BackupScheduleState,
CLOUD_AGENT,
CORE_LOCAL_AGENT,
downloadEmergencyKit,
generateEncryptionKey,
HASSIO_LOCAL_AGENT,
updateBackupConfig,
} from "../../../../data/backup";
import type { HassDialog } from "../../../../dialogs/make-dialog-manager";
import { haStyle, haStyleDialog } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import { fileDownload } from "../../../../util/file_download";
import { showToast } from "../../../../util/toast";
import "../components/config/ha-backup-config-agents";
import "../components/config/ha-backup-config-data";
Expand Down Expand Up @@ -392,10 +392,7 @@ class DialogBackupOnboarding extends LitElement implements HassDialog {
if (!key) {
return;
}
fileDownload(
"data:text/plain;charset=utf-8," + encodeURIComponent(key),
"emergency_kit.txt"
);
downloadEmergencyKit(this.hass, key);
}

private _copyKeyToClipboard() {
Expand Down Expand Up @@ -465,6 +462,7 @@ class DialogBackupOnboarding extends LitElement implements HassDialog {
width: 90vw;
max-width: 560px;
--dialog-content-padding: 8px 24px;
max-height: min(605px, 100% - 48px);
}
ha-md-list {
background: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import type { HaMdDialog } from "../../../../components/ha-md-dialog";
import "../../../../components/ha-md-list";
import "../../../../components/ha-md-list-item";
import "../../../../components/ha-password-field";
import { generateEncryptionKey } from "../../../../data/backup";
import {
downloadEmergencyKit,
generateEncryptionKey,
} from "../../../../data/backup";
import type { HassDialog } from "../../../../dialogs/make-dialog-manager";
import { haStyle, haStyleDialog } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import { fileDownload } from "../../../../util/file_download";
import { showToast } from "../../../../util/toast";
import type { ChangeBackupEncryptionKeyDialogParams } from "./show-dialog-change-backup-encryption-key";

Expand Down Expand Up @@ -224,22 +226,14 @@ class DialogChangeBackupEncryptionKey extends LitElement implements HassDialog {
if (!this._params?.currentKey) {
return;
}
fileDownload(
"data:text/plain;charset=utf-8," +
encodeURIComponent(this._params.currentKey),
"emergency_kit_old.txt"
);
downloadEmergencyKit(this.hass, this._params.currentKey, "old");
}

private _downloadNew() {
if (!this._newEncryptionKey) {
return;
}
fileDownload(
"data:text/plain;charset=utf-8," +
encodeURIComponent(this._newEncryptionKey),
"emergency_kit.txt"
);
downloadEmergencyKit(this.hass, this._newEncryptionKey);
}

private async _submit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import type { HaMdDialog } from "../../../../components/ha-md-dialog";
import "../../../../components/ha-md-list";
import "../../../../components/ha-md-list-item";
import "../../../../components/ha-password-field";
import { generateEncryptionKey } from "../../../../data/backup";
import {
downloadEmergencyKit,
generateEncryptionKey,
} from "../../../../data/backup";
import type { HassDialog } from "../../../../dialogs/make-dialog-manager";
import { haStyle, haStyleDialog } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import { fileDownload } from "../../../../util/file_download";
import type { SetBackupEncryptionKeyDialogParams } from "./show-dialog-set-backup-encryption-key";

const STEPS = ["new", "save"] as const;
Expand Down Expand Up @@ -162,11 +164,7 @@ class DialogSetBackupEncryptionKey extends LitElement implements HassDialog {
if (!this._newEncryptionKey) {
return;
}
fileDownload(
"data:text/plain;charset=utf-8," +
encodeURIComponent(this._newEncryptionKey),
"emergency_kit.txt"
);
downloadEmergencyKit(this.hass, this._newEncryptionKey);
}

private _encryptionKeyChanged(ev) {
Expand Down
6 changes: 3 additions & 3 deletions src/panels/config/backup/ha-config-backup-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ class HaConfigBackupSettings extends LitElement {
<div class="card-content">
<p>
Keep this encryption key in a safe place, as you will need it to
access your backup, allowing it to be restored. Either record
the characters below or download them as an emergency kit file.
Encryption keeps your backups private and secure.
access your backup, allowing it to be restored. Download them as
an emergency kit file and store it somewhere safe. Encryption
keeps your backups private and secure.
</p>
<ha-backup-config-encryption-key
.hass=${this.hass}
Expand Down

0 comments on commit 637fe37

Please sign in to comment.