Skip to content
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

Allow to change encryption for each backup location #23861

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { mdiHarddisk, mdiNas } from "@mdi/js";
import { mdiCogOutline, mdiHarddisk, mdiNas } from "@mdi/js";
import type { PropertyValues } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../../../../../common/dom/fire_event";
import { computeDomain } from "../../../../../common/entity/compute_domain";
import "../../../../../components/ha-icon-button";
import "../../../../../components/ha-md-list";
import "../../../../../components/ha-md-list-item";
import "../../../../../components/ha-svg-icon";
Expand All @@ -19,6 +20,7 @@ import {
import type { CloudStatus } from "../../../../../data/cloud";
import type { HomeAssistant } from "../../../../../types";
import { brandsUrl } from "../../../../../util/brands-url";
import { navigate } from "../../../../../common/navigate";

const DEFAULT_AGENTS = [];

Expand All @@ -28,6 +30,9 @@ class HaBackupConfigAgents extends LitElement {

@property({ attribute: false }) public cloudStatus!: CloudStatus;

@property({ type: Boolean, attribute: "show-settings" }) public showSettings =
false;

@state() private _agentIds: string[] = [];

@state() private value?: string[];
Expand Down Expand Up @@ -117,6 +122,13 @@ class HaBackupConfigAgents extends LitElement {
${description
? html`<div slot="supporting-text">${description}</div>`
: nothing}

<ha-icon-button
id=${agentId}
slot="end"
path=${mdiCogOutline}
@click=${this._showAgentSettings}
></ha-icon-button>
<ha-switch
slot="end"
id=${agentId}
Expand All @@ -136,6 +148,11 @@ class HaBackupConfigAgents extends LitElement {
`;
}

private _showAgentSettings(ev): void {
const agentId = ev.currentTarget.id;
navigate(`/config/backup/location/${agentId}`);
}

private _agentToggled(ev) {
ev.stopPropagation();
const value = ev.currentTarget.checked;
Expand Down
291 changes: 291 additions & 0 deletions src/panels/config/backup/ha-config-backup-location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import "../../../components/ha-alert";
import "../../../components/ha-button";
import "../../../components/ha-switch";
import "../../../components/ha-button-menu";
import "../../../components/ha-card";
import "../../../components/ha-circular-progress";
import "../../../components/ha-icon-button";
import "../../../components/ha-list-item";
import "../../../components/ha-md-list";
import "../../../components/ha-md-list-item";
import type { BackupAgent, BackupConfig } from "../../../data/backup";
import {
CLOUD_AGENT,
computeBackupAgentName,
fetchBackupAgentsInfo,
} from "../../../data/backup";
import "../../../layouts/hass-subpage";
import type { HomeAssistant } from "../../../types";
import "./components/ha-backup-data-picker";
import { showConfirmationDialog } from "../../lovelace/custom-card-helpers";

@customElement("ha-config-backup-location")
class HaConfigBackupDetails extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ type: Boolean }) public narrow = false;

@property({ attribute: "agent-id" }) public agentId!: string;

@property({ attribute: false }) public config?: BackupConfig;

@state() private _agent?: BackupAgent | null;

@state() private _agentIds?: string[];

@state() private _error?: string;

// Todo update with api call
@state() private _encrypted = true;

protected firstUpdated(changedProps) {
super.firstUpdated(changedProps);

if (this.agentId) {
this._fetchAgent();
} else {
this._error = "Agent id not defined";
}
}

protected render() {
if (!this.hass) {
return nothing;
}

const encrypted = this._encrypted;

return html`
<hass-subpage
back-path="/config/backup/settings"
.hass=${this.hass}
.narrow=${this.narrow}
.header=${(this._agent &&
computeBackupAgentName(
this.hass.localize,
this.agentId,
this._agentIds
)) ||
this.hass.localize("ui.panel.config.backup.location.header")}
>
<div class="content">
${this._error &&
html`<ha-alert alert-type="error">${this._error}</ha-alert>`}
${this._agent === null
? html`
<ha-alert
alert-type="warning"
.title=${this.hass.localize(
"ui.panel.config.backup.location.not_found"
)}
>
${this.hass.localize(
"ui.panel.config.backup.location.not_found_description",
{ agentId: this.agentId }
)}
</ha-alert>
`
: !this.agentId
? html`<ha-circular-progress active></ha-circular-progress>`
: html`
<ha-card>
<div class="card-header">
${this.hass.localize(
"ui.panel.config.backup.location.encryption.title"
)}
</div>
<div class="card-content">
<ha-md-list>
${CLOUD_AGENT === this.agentId
? html`
<ha-md-list-item>
<span slot="headline">
${this.hass.localize(
"ui.panel.config.backup.location.encryption.encryption"
)}
</span>
<span slot="supporting-text">
${this.hass.localize(
`ui.panel.config.backup.location.encryption.cloud_forced_encryption`
)}
</span>
<ha-switch
slot="end"
checked
disabled
></ha-switch>
</ha-md-list-item>
`
: html`
<ha-md-list-item>
<span slot="headline">
${this.hass.localize(
"ui.panel.config.backup.location.encryption.encryption"
)}
</span>
<span slot="supporting-text">
${this.hass.localize(
`ui.panel.config.backup.location.encryption.encryption_description_${encrypted ? "on" : "off"}`
)}
</span>
${encrypted
? html`
<ha-button
slot="end"
@click=${this._turnOffEncryption}
destructive
>
${this.hass.localize(
"ui.panel.config.backup.location.encryption.encryption_turn_off"
)}
</ha-button>
`
: html`
<ha-button
slot="end"
@click=${this._turnOnEncryption}
>
${this.hass.localize(
"ui.panel.config.backup.location.encryption.encryption_turn_on"
)}
</ha-button>
`}
</ha-md-list-item>
`}
</ha-md-list>
</div>
</ha-card>
`}
</div>
</hass-subpage>
`;
}

private async _fetchAgent() {
try {
// Todo fetch agent details
const { agents } = await fetchBackupAgentsInfo(this.hass);
const agent = agents.find((a) => a.agent_id === this.agentId);
if (!agent) {
throw new Error("Agent not found");
}
this._agent = agent;
this._agentIds = agents.map((a) => a.agent_id);
} catch (err: any) {
this._error =
err?.message ||
this.hass.localize("ui.panel.config.backup.details.error");
}
}

private _turnOnEncryption() {
this._encrypted = true;
// Todo call api
}

private async _turnOffEncryption() {
const response = await showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.config.backup.location.encryption.encryption_turn_off_confirm_title"
),
text: this.hass.localize(
"ui.panel.config.backup.location.encryption.encryption_turn_off_confirm_text"
),
confirmText: this.hass.localize(
"ui.panel.config.backup.location.encryption.encryption_turn_off_confirm_action"
),
dismissText: this.hass.localize("ui.common.cancel"),
destructive: true,
});
if (response) {
this._encrypted = false;
// Todo call api
}
}

static styles = css`
.content {
padding: 28px 20px 0;
max-width: 690px;
margin: 0 auto;
gap: 24px;
display: grid;
margin-bottom: 24px;
}
.card-content {
padding: 0 20px;
}
.card-actions {
display: flex;
justify-content: flex-end;
}
ha-md-list {
background: none;
padding: 0;
}
ha-md-list-item {
--md-list-item-leading-space: 0;
--md-list-item-trailing-space: 0;
--md-list-item-two-line-container-height: 64px;
}
ha-md-list-item img {
width: 48px;
}
ha-md-list-item ha-svg-icon[slot="start"] {
--mdc-icon-size: 48px;
color: var(--primary-text-color);
}
ha-md-list.summary ha-md-list-item {
--md-list-item-supporting-text-size: 1rem;
--md-list-item-label-text-size: 0.875rem;

--md-list-item-label-text-color: var(--secondary-text-color);
--md-list-item-supporting-text-color: var(--primary-text-color);
}
.warning {
color: var(--error-color);
}
.warning ha-svg-icon {
color: var(--error-color);
}
ha-button.danger {
--mdc-theme-primary: var(--error-color);
}
ha-backup-data-picker {
display: block;
}
ha-md-list-item [slot="supporting-text"] {
display: flex;
align-items: center;
flex-direction: row;
gap: 8px;
line-height: normal;
}
.dot {
display: block;
position: relative;
width: 8px;
height: 8px;
background-color: var(--disabled-color);
border-radius: 50%;
flex: none;
}
.dot.success {
background-color: var(--success-color);
}
.dot.error {
background-color: var(--error-color);
}
.card-header {
padding-bottom: 8px;
}
`;
}

declare global {
interface HTMLElementTagNameMap {
"ha-config-backup-location": HaConfigBackupDetails;
}
}
1 change: 1 addition & 0 deletions src/panels/config/backup/ha-config-backup-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class HaConfigBackupSettings extends LitElement {
.value=${this._config.create_backup.agent_ids}
.cloudStatus=${this.cloudStatus}
@value-changed=${this._agentsConfigChanged}
show-settings
></ha-backup-config-agents>
${!this._config.create_backup.agent_ids.length
? html`
Expand Down
18 changes: 13 additions & 5 deletions src/panels/config/backup/ha-config-backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class HaConfigBackup extends SubscribeMixin(HassRouterPage) {
tag: "ha-config-backup-settings",
load: () => import("./ha-config-backup-settings"),
},
location: {
tag: "ha-config-backup-location",
load: () => import("./ha-config-backup-location"),
},
},
};

Expand All @@ -119,11 +123,15 @@ class HaConfigBackup extends SubscribeMixin(HassRouterPage) {
pageEl.config = this._config;
pageEl.fetching = this._fetching;

if (
(!changedProps || changedProps.has("route")) &&
this._currentPage === "details"
) {
pageEl.backupId = this.routeTail.path.substr(1);
if (!changedProps || changedProps.has("route")) {
switch (this._currentPage) {
case "details":
pageEl.backupId = this.routeTail.path.substr(1);
break;
case "location":
pageEl.agentId = this.routeTail.path.substr(1);
break;
}
}
}

Expand Down
Loading
Loading