-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add panel to Backup integration #11671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
215fde4
Add panel to Backup integration
ludeeus 885508b
Remove name
ludeeus f658538
lint
ludeeus c15c39b
confirm remove
ludeeus 39d7a5c
Merge branch 'dev' of github.com:home-assistant/frontend into backup_…
ludeeus f16827c
Merge branch 'dev' of github.com:home-assistant/frontend into backup_…
ludeeus 56ca6dc
disable when backing up
ludeeus 8b363e0
Apply suggestions from code review
balloob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { HomeAssistant } from "../types"; | ||
|
|
||
| export interface BackupContent { | ||
| slug: string; | ||
| date: string; | ||
| name: string; | ||
| size: number; | ||
| path: string; | ||
| } | ||
|
|
||
| export interface BackupData { | ||
| backing_up: boolean; | ||
| backups: BackupContent[]; | ||
| } | ||
|
|
||
| export const getBackupDownloadUrl = (slug: string) => | ||
| `/api/backup/download/${slug}`; | ||
|
|
||
| export const fetchBackupInfo = (hass: HomeAssistant): Promise<BackupData> => | ||
| hass.callWS({ | ||
| type: "backup/info", | ||
| }); | ||
|
|
||
| export const removeBackup = ( | ||
| hass: HomeAssistant, | ||
| slug: string | ||
| ): Promise<void> => | ||
| hass.callWS({ | ||
| type: "backup/remove", | ||
| slug, | ||
| }); | ||
|
|
||
| export const generateBackup = (hass: HomeAssistant): Promise<BackupContent> => | ||
| hass.callWS({ | ||
| type: "backup/generate", | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| import { mdiDelete, mdiDownload, mdiPlus } from "@mdi/js"; | ||
| import "@polymer/paper-tooltip/paper-tooltip"; | ||
| import { | ||
| css, | ||
| CSSResultGroup, | ||
| html, | ||
| LitElement, | ||
| PropertyValues, | ||
| TemplateResult, | ||
| } from "lit"; | ||
| import { customElement, property, state } from "lit/decorators"; | ||
| import memoize from "memoize-one"; | ||
| import { relativeTime } from "../../../common/datetime/relative_time"; | ||
| import { DataTableColumnContainer } from "../../../components/data-table/ha-data-table"; | ||
| import "../../../components/ha-circular-progress"; | ||
| import "../../../components/ha-fab"; | ||
| import "../../../components/ha-icon"; | ||
| import "../../../components/ha-icon-overflow-menu"; | ||
| import "../../../components/ha-svg-icon"; | ||
| import { getSignedPath } from "../../../data/auth"; | ||
| import { | ||
| BackupContent, | ||
| BackupData, | ||
| fetchBackupInfo, | ||
| generateBackup, | ||
| getBackupDownloadUrl, | ||
| removeBackup, | ||
| } from "../../../data/backup"; | ||
| import { | ||
| showAlertDialog, | ||
| showConfirmationDialog, | ||
| } from "../../../dialogs/generic/show-dialog-box"; | ||
| import "../../../layouts/hass-loading-screen"; | ||
| import "../../../layouts/hass-tabs-subpage-data-table"; | ||
| import { HomeAssistant, Route } from "../../../types"; | ||
| import { fileDownload } from "../../../util/file_download"; | ||
| import { configSections } from "../ha-panel-config"; | ||
|
|
||
| @customElement("ha-config-backup") | ||
| class HaConfigBackup extends LitElement { | ||
| @property({ attribute: false }) public hass!: HomeAssistant; | ||
|
|
||
| @property({ type: Boolean }) public isWide!: boolean; | ||
|
|
||
| @property({ type: Boolean }) public narrow!: boolean; | ||
|
|
||
| @property({ attribute: false }) public route!: Route; | ||
|
|
||
| @state() private _backupData?: BackupData; | ||
|
|
||
| private _columns = memoize( | ||
| (narrow, _language): DataTableColumnContainer => ({ | ||
| name: { | ||
| title: this.hass.localize("ui.panel.config.backup.name"), | ||
| sortable: true, | ||
| filterable: true, | ||
| grows: true, | ||
| template: (entry: string, backup: BackupContent) => | ||
| html`${entry} | ||
| <div class="secondary">${backup.path}</div>`, | ||
| }, | ||
| size: { | ||
| title: this.hass.localize("ui.panel.config.backup.size"), | ||
| width: "15%", | ||
| hidden: narrow, | ||
| filterable: true, | ||
| sortable: true, | ||
| template: (entry: number) => Math.ceil(entry * 10) / 10 + " MB", | ||
| }, | ||
| date: { | ||
| title: this.hass.localize("ui.panel.config.backup.created"), | ||
| width: "15%", | ||
| direction: "desc", | ||
| hidden: narrow, | ||
| filterable: true, | ||
| sortable: true, | ||
| template: (entry: string) => | ||
| relativeTime(new Date(entry), this.hass.locale), | ||
| }, | ||
|
|
||
| actions: { | ||
| title: "", | ||
| width: "15%", | ||
| template: (_: string, backup: BackupContent) => | ||
| html`<ha-icon-overflow-menu | ||
| .hass=${this.hass} | ||
| .narrow=${this.narrow} | ||
| .items=${[ | ||
| // Download Button | ||
| { | ||
| path: mdiDownload, | ||
| label: this.hass.localize( | ||
| "ui.panel.config.backup.download_backup" | ||
| ), | ||
| action: () => this._downloadBackup(backup), | ||
| }, | ||
| // Delete button | ||
| { | ||
| path: mdiDelete, | ||
| label: this.hass.localize( | ||
| "ui.panel.config.backup.remove_backup" | ||
| ), | ||
| action: () => this._removeBackup(backup), | ||
| }, | ||
| ]} | ||
| style="color: var(--secondary-text-color)" | ||
| > | ||
| </ha-icon-overflow-menu>`, | ||
| }, | ||
| }) | ||
| ); | ||
|
|
||
| private _getItems = memoize((backupItems: BackupContent[]) => | ||
| backupItems.map((backup) => ({ | ||
| name: backup.name, | ||
| slug: backup.slug, | ||
| date: backup.date, | ||
| size: backup.size, | ||
| path: backup.path, | ||
| })) | ||
| ); | ||
|
|
||
| protected render(): TemplateResult { | ||
| if (!this.hass || this._backupData === undefined) { | ||
| return html`<hass-loading-screen></hass-loading-screen>`; | ||
| } | ||
|
|
||
| return html` | ||
| <hass-tabs-subpage-data-table | ||
| .hass=${this.hass} | ||
| .narrow=${this.narrow} | ||
| back-path="/config" | ||
| .route=${this.route} | ||
| .tabs=${configSections.backup} | ||
| .columns=${this._columns(this.narrow, this.hass.language)} | ||
| .data=${this._getItems(this._backupData.backups)} | ||
| .noDataText=${this.hass.localize("ui.panel.config.backup.no_bakcups")} | ||
| > | ||
| <ha-fab | ||
| slot="fab" | ||
| ?disabled=${this._backupData.backing_up} | ||
| .label=${this._backupData.backing_up | ||
| ? this.hass.localize("ui.panel.config.backup.creating_backup") | ||
| : this.hass.localize("ui.panel.config.backup.create_backup")} | ||
| extended | ||
| @click=${this._generateBackup} | ||
| > | ||
| ${this._backupData.backing_up | ||
| ? html`<ha-circular-progress | ||
| slot="icon" | ||
| active | ||
| ></ha-circular-progress>` | ||
| : html`<ha-svg-icon slot="icon" .path=${mdiPlus}></ha-svg-icon>`} | ||
| </ha-fab> | ||
| </hass-tabs-subpage-data-table> | ||
| `; | ||
| } | ||
|
|
||
| protected firstUpdated(changedProps: PropertyValues) { | ||
| super.firstUpdated(changedProps); | ||
| this._getBackups(); | ||
| } | ||
|
|
||
| private async _getBackups(): Promise<void> { | ||
| this._backupData = await fetchBackupInfo(this.hass); | ||
| } | ||
|
|
||
| private async _downloadBackup(backup: BackupContent): Promise<void> { | ||
| const signedUrl = await getSignedPath( | ||
| this.hass, | ||
| getBackupDownloadUrl(backup.slug) | ||
| ); | ||
| fileDownload(signedUrl.path); | ||
| } | ||
|
|
||
| private async _generateBackup(): Promise<void> { | ||
| const confirm = await showConfirmationDialog(this, { | ||
| title: this.hass.localize("ui.panel.config.backup.create.title"), | ||
| text: this.hass.localize("ui.panel.config.backup.create.description"), | ||
| confirmText: this.hass.localize("ui.panel.config.backup.create.confirm"), | ||
| }); | ||
| if (!confirm) { | ||
| return; | ||
| } | ||
|
|
||
| generateBackup(this.hass) | ||
| .then(() => this._getBackups()) | ||
| .catch((err) => showAlertDialog(this, { text: (err as Error).message })); | ||
|
|
||
| await this._getBackups(); | ||
| } | ||
|
|
||
| private async _removeBackup(backup: BackupContent): Promise<void> { | ||
| const confirm = await showConfirmationDialog(this, { | ||
| title: this.hass.localize("ui.panel.config.backup.remove.title"), | ||
| text: this.hass.localize("ui.panel.config.backup.remove.description", { | ||
| name: backup.name, | ||
| }), | ||
| confirmText: this.hass.localize("ui.panel.config.backup.remove.confirm"), | ||
| }); | ||
| if (!confirm) { | ||
| return; | ||
| } | ||
|
|
||
| await removeBackup(this.hass, backup.slug); | ||
| await this._getBackups(); | ||
| } | ||
|
|
||
| static get styles(): CSSResultGroup { | ||
| return [ | ||
| css` | ||
| ha-fab[disabled] { | ||
| --mdc-theme-secondary: var(--disabled-text-color) !important; | ||
| } | ||
| `, | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| "ha-config-backup": HaConfigBackup; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we disable the fab and change the text while a backup is being generated? Otherwise the user will not know a backup is in progress.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, a user should always be able to click and fail even if we know it will never work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not disable plus change text that were making a backup ?