Compare commits

...
Author SHA1 Message Date
Franck Nijhof 026c858a0f Add bulk delete to the automations and scripts lists 2026-11-24 17:07:20 +00:00
3 changed files with 188 additions and 62 deletions

No files matched your search

@@ -114,6 +114,7 @@ import {
showConfirmationDialog,
} from "../../../dialogs/generic/show-dialog-box";
import "../../../layouts/hass-tabs-subpage-data-table";
import type { HaTabsSubpageDataTable } from "../../../layouts/hass-tabs-subpage-data-table";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { haStyle } from "../../../resources/styles";
import type { HomeAssistant, Route, ServiceCallResponse } from "../../../types";
@@ -263,6 +264,9 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
@query("#overflow-menu") private _overflowMenu!: HaDropdown;
@query("hass-tabs-subpage-data-table", true)
private _dataTable!: HaTabsSubpageDataTable;
private _sizeController = new ResizeController(this, {
callback: (entries) => entries[0]?.contentRect.width,
});
@@ -762,6 +766,13 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
"ui.panel.config.automation.picker.bulk_actions.disable"
)}
</ha-dropdown-item>
<wa-divider></wa-divider>
<ha-dropdown-item value="delete_selected" variant="danger">
<ha-svg-icon slot="icon" .path=${mdiDelete}></ha-svg-icon>
${this.hass.localize(
"ui.panel.config.automation.picker.bulk_actions.delete_selected.button"
)}
</ha-dropdown-item>
</ha-dropdown>
${
!this.automations.length
@@ -1175,6 +1186,50 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
}
}
private _deleteSelected = () => {
// Only rows with an entity registry entry are selectable, so every selected
// automation has an ID. The backend still refuses the ones that aren't
// stored in automations.yaml, those end up in the failure alert.
const automations = this.automations.filter((automation) =>
this._selected.includes(automation.entity_id)
);
let failedCount = 0;
showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.config.automation.picker.bulk_actions.delete_selected.confirm_title"
),
text: this.hass.localize(
"ui.panel.config.automation.picker.bulk_actions.delete_selected.confirm_text",
{ count: automations.length }
),
confirmText: this.hass.localize("ui.common.delete"),
dismissText: this.hass.localize("ui.common.cancel"),
destructive: true,
action: async () => {
const results = await Promise.allSettled(
automations.map((automation) =>
deleteAutomation(this.hass, automation.attributes.id!)
)
);
failedCount = rejectedItems(results).length;
this._dataTable.clearSelection();
},
}).then((confirmed) => {
if (confirmed && failedCount) {
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.automation.picker.bulk_actions.delete_selected.failed_title"
),
text: this.hass.localize(
"ui.panel.config.automation.picker.bulk_actions.delete_selected.failed",
{ count: failedCount }
),
});
}
});
};
private _duplicate = async (automation: AutomationItem) => {
try {
const config = await fetchAutomationFileConfig(
@@ -1546,6 +1601,10 @@ ${rejected
this._handleBulkDisable();
return;
}
if (value === "delete_selected") {
this._deleteSelected();
return;
}
if (value.startsWith("category_")) {
if (value === "category_create") {
+112 -61
View File
@@ -19,7 +19,7 @@ import {
import type { UnsubscribeFunc } from "home-assistant-js-websocket";
import type { CSSResultGroup, PropertyValues, TemplateResult } from "lit";
import { LitElement, css, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { customElement, property, query, state } from "lit/decorators";
import { styleMap } from "lit/directives/style-map";
import memoizeOne from "memoize-one";
import { isComponentLoaded } from "../../../common/config/is_component_loaded";
@@ -105,6 +105,7 @@ import {
} from "../../../dialogs/generic/show-dialog-box";
import { showMoreInfoDialog } from "../../../dialogs/more-info/show-ha-more-info-dialog";
import "../../../layouts/hass-tabs-subpage-data-table";
import type { HaTabsSubpageDataTable } from "../../../layouts/hass-tabs-subpage-data-table";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { haStyle } from "../../../resources/styles";
import type { HomeAssistant, Route } from "../../../types";
@@ -157,6 +158,9 @@ class HaScriptPicker extends SubscribeMixin(LitElement) {
@state() private _selected: string[] = [];
@query("hass-tabs-subpage-data-table", true)
private _dataTable!: HaTabsSubpageDataTable;
@state() private _activeFilters?: string[];
@state() private _filteredEntityIds?: string[] | null;
@@ -628,66 +632,66 @@ class HaScriptPicker extends SubscribeMixin(LitElement) {
}`
: nothing
}
${
this.narrow || areasInOverflow
? html` <ha-dropdown
slot="selection-bar"
@wa-select=${this._handleBulkAction}
>
${
this.narrow
? html`<ha-assist-chip
.label=${this.hass.localize(
"ui.panel.config.automation.picker.bulk_action"
)}
slot="trigger"
>
<ha-svg-icon
slot="trailing-icon"
.path=${mdiMenuDown}
></ha-svg-icon>
</ha-assist-chip>`
: html`<ha-icon-button
.path=${mdiDotsVertical}
.label=${this.hass.localize(
"ui.panel.config.automation.picker.bulk_action"
)}
slot="trigger"
></ha-icon-button>`
}
${
this.narrow
? html`<ha-dropdown-item>
${this.hass.localize(
"ui.panel.config.automation.picker.bulk_actions.move_category"
)}
${this._renderCategoryItems("submenu")}
</ha-dropdown-item>`
: nothing
}
${
this.narrow || labelsInOverflow
? html`<ha-dropdown-item>
${this.hass.localize(
"ui.panel.config.automation.picker.bulk_actions.add_label"
)}
${this._renderLabelItems("submenu")}
</ha-dropdown-item>`
: nothing
}
${
this.narrow || areasInOverflow
? html`<ha-dropdown-item>
${this.hass.localize(
"ui.panel.config.devices.picker.bulk_actions.move_area"
)}
${this._renderAreaItems("submenu")}
</ha-dropdown-item>`
: nothing
}
</ha-dropdown>`
: nothing
}
<ha-dropdown slot="selection-bar" @wa-select=${this._handleBulkAction}>
${
this.narrow
? html`<ha-assist-chip
.label=${this.hass.localize(
"ui.panel.config.automation.picker.bulk_action"
)}
slot="trigger"
>
<ha-svg-icon
slot="trailing-icon"
.path=${mdiMenuDown}
></ha-svg-icon>
</ha-assist-chip>`
: html`<ha-icon-button
.path=${mdiDotsVertical}
.label=${this.hass.localize(
"ui.panel.config.automation.picker.bulk_action"
)}
slot="trigger"
></ha-icon-button>`
}
${
this.narrow
? html`<ha-dropdown-item>
${this.hass.localize(
"ui.panel.config.automation.picker.bulk_actions.move_category"
)}
${this._renderCategoryItems("submenu")}
</ha-dropdown-item>`
: nothing
}
${
this.narrow || labelsInOverflow
? html`<ha-dropdown-item>
${this.hass.localize(
"ui.panel.config.automation.picker.bulk_actions.add_label"
)}
${this._renderLabelItems("submenu")}
</ha-dropdown-item>`
: nothing
}
${
this.narrow || areasInOverflow
? html`<ha-dropdown-item>
${this.hass.localize(
"ui.panel.config.devices.picker.bulk_actions.move_area"
)}
${this._renderAreaItems("submenu")}
</ha-dropdown-item>
<wa-divider></wa-divider>`
: nothing
}
<ha-dropdown-item value="delete_selected" variant="danger">
<ha-svg-icon slot="icon" .path=${mdiDelete}></ha-svg-icon>
${this.hass.localize(
"ui.panel.config.script.picker.bulk_actions.delete_selected.button"
)}
</ha-dropdown-item>
</ha-dropdown>
${
!this.scripts.length
? html` <div class="empty" slot="empty">
@@ -1141,6 +1145,48 @@ ${rejected
}
}
private _deleteSelected = () => {
// Only rows with an entity registry entry are selectable, so every selected
// script has a unique ID. The backend still refuses the ones that aren't
// stored in scripts.yaml, those end up in the failure alert.
const entries = this._entityReg.filter((entry) =>
this._selected.includes(entry.entity_id)
);
let failedCount = 0;
showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.config.script.picker.bulk_actions.delete_selected.confirm_title"
),
text: this.hass.localize(
"ui.panel.config.script.picker.bulk_actions.delete_selected.confirm_text",
{ count: entries.length }
),
confirmText: this.hass.localize("ui.common.delete"),
dismissText: this.hass.localize("ui.common.cancel"),
destructive: true,
action: async () => {
const results = await Promise.allSettled(
entries.map((entry) => deleteScript(this.hass, entry.unique_id))
);
failedCount = rejectedItems(results).length;
this._dataTable.clearSelection();
},
}).then((confirmed) => {
if (confirmed && failedCount) {
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.script.picker.bulk_actions.delete_selected.failed_title"
),
text: this.hass.localize(
"ui.panel.config.script.picker.bulk_actions.delete_selected.failed",
{ count: failedCount }
),
});
}
});
};
private _bulkCreateCategory = () => {
showCategoryRegistryDetailDialog(this, {
scope: "script",
@@ -1307,6 +1353,11 @@ ${rejected
return;
}
if (value === "delete_selected") {
this._deleteSelected();
return;
}
if (value.startsWith("category_")) {
if (value === "category_create") {
this._bulkCreateCategory();
+17 -1
View File
@@ -5914,7 +5914,14 @@
"no_category": "No category",
"add_label": "Add label",
"enable": "Enable",
"disable": "Disable"
"disable": "Disable",
"delete_selected": {
"button": "[%key:ui::panel::config::entities::picker::delete_selected::button%]",
"confirm_title": "Delete selected automations?",
"confirm_text": "{count, plural,\n one {The selected automation will be permanently deleted.}\n other {The {count} selected automations will be permanently deleted.}\n}",
"failed_title": "[%key:ui::panel::config::devices::picker::bulk_actions::delete_selected::partial_failure_title%]",
"failed": "{count, plural,\n one {1 automation could not be deleted.}\n other {{count} automations could not be deleted.}\n} Only automations stored in automations.yaml can be deleted."
}
},
"empty_header": "Start automating",
"empty_text_1": "Automations make Home Assistant automatically respond to things happening in and around your home.",
@@ -7099,6 +7106,15 @@
"no_category_entity_reg": "To assign a category to a script it needs to have a unique ID.",
"delete": "[%key:ui::common::delete%]",
"duplicate": "[%key:ui::common::duplicate%]",
"bulk_actions": {
"delete_selected": {
"button": "[%key:ui::panel::config::entities::picker::delete_selected::button%]",
"confirm_title": "Delete selected scripts?",
"confirm_text": "{count, plural,\n one {The selected script will be permanently deleted.}\n other {The {count} selected scripts will be permanently deleted.}\n}",
"failed_title": "[%key:ui::panel::config::devices::picker::bulk_actions::delete_selected::partial_failure_title%]",
"failed": "{count, plural,\n one {1 script could not be deleted.}\n other {{count} scripts could not be deleted.}\n} Only scripts stored in scripts.yaml can be deleted."
}
},
"empty_header": "Create your first script",
"empty_text": "A script is a sequence of actions that can be run from a dashboard, an automation, or be triggered by voice. For example, a ''Wake-up routine'' script that gradually turns on the light in the bedroom and opens the blinds after a delay.",
"search": "Search {number} {number, plural,\n one {script}\n other {scripts}\n}",