Add sequence support to dashboard action

This commit is contained in:
Paul Bottein 2024-10-09 18:53:41 +02:00
parent 23a33b10a1
commit cc07d51613
No known key found for this signature in database
6 changed files with 63 additions and 3 deletions

View File

@ -174,6 +174,7 @@ export class HaServiceControl extends LitElement {
if (this._value && serviceData) { if (this._value && serviceData) {
const loadDefaults = this.value && !("data" in this.value); const loadDefaults = this.value && !("data" in this.value);
// Set mandatory bools without a default value to false // Set mandatory bools without a default value to false
this._value = { ...this._value };
if (!this._value.data) { if (!this._value.data) {
this._value.data = {}; this._value.data = {};
} }

View File

@ -1,4 +1,5 @@
import type { HassServiceTarget } from "home-assistant-js-websocket"; import type { HassServiceTarget } from "home-assistant-js-websocket";
import { Action } from "../../script";
export interface ToggleActionConfig extends BaseActionConfig { export interface ToggleActionConfig extends BaseActionConfig {
action: "toggle"; action: "toggle";
@ -31,6 +32,11 @@ export interface MoreInfoActionConfig extends BaseActionConfig {
entity_id?: string; entity_id?: string;
} }
export interface SequenceActionConfig extends BaseActionConfig {
action: "sequence";
actions?: Action[];
}
export interface AssistActionConfig extends BaseActionConfig { export interface AssistActionConfig extends BaseActionConfig {
action: "assist"; action: "assist";
pipeline_id?: string; pipeline_id?: string;
@ -67,4 +73,5 @@ export type ActionConfig =
| MoreInfoActionConfig | MoreInfoActionConfig
| AssistActionConfig | AssistActionConfig
| NoActionConfig | NoActionConfig
| CustomActionConfig; | CustomActionConfig
| SequenceActionConfig;

View File

@ -3,6 +3,7 @@ import { navigate } from "../../../common/navigate";
import { forwardHaptic } from "../../../data/haptics"; import { forwardHaptic } from "../../../data/haptics";
import { domainToName } from "../../../data/integration"; import { domainToName } from "../../../data/integration";
import { ActionConfig } from "../../../data/lovelace/config/action"; import { ActionConfig } from "../../../data/lovelace/config/action";
import { callExecuteScript } from "../../../data/service";
import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box"; import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box";
import { showVoiceCommandDialog } from "../../../dialogs/voice-command-dialog/show-ha-voice-command-dialog"; import { showVoiceCommandDialog } from "../../../dialogs/voice-command-dialog/show-ha-voice-command-dialog";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
@ -177,6 +178,13 @@ export const handleAction = async (
}); });
break; break;
} }
case "sequence": {
if (!actionConfig.actions) {
return;
}
callExecuteScript(hass, actionConfig.actions);
break;
}
case "fire-dom-event": { case "fire-dom-event": {
fireEvent(node, "ll-custom", actionConfig); fireEvent(node, "ll-custom", actionConfig);
} }

View File

@ -34,6 +34,7 @@ const DEFAULT_ACTIONS: UiAction[] = [
"navigate", "navigate",
"url", "url",
"perform-action", "perform-action",
"sequence",
"assist", "assist",
"none", "none",
]; ];
@ -70,6 +71,15 @@ const ASSIST_SCHEMA = [
}, },
] as const satisfies readonly HaFormSchema[]; ] as const satisfies readonly HaFormSchema[];
const SEQUENCE_SCHEMA = [
{
name: "actions",
selector: {
action: {},
},
},
] as const satisfies readonly HaFormSchema[];
@customElement("hui-action-editor") @customElement("hui-action-editor")
export class HuiActionEditor extends LitElement { export class HuiActionEditor extends LitElement {
@property({ attribute: false }) public config?: ActionConfig; @property({ attribute: false }) public config?: ActionConfig;
@ -120,6 +130,10 @@ export class HuiActionEditor extends LitElement {
} }
} }
protected firstUpdated(_changedProperties: PropertyValues): void {
this.hass!.loadFragmentTranslation("config");
}
protected render() { protected render() {
if (!this.hass) { if (!this.hass) {
return nothing; return nothing;
@ -218,6 +232,17 @@ export class HuiActionEditor extends LitElement {
</ha-form> </ha-form>
` `
: nothing} : nothing}
${this.config?.action === "sequence"
? html`
<ha-form
.hass=${this.hass}
.schema=${SEQUENCE_SCHEMA}
.data=${this.config}
.computeLabel=${this._computeFormLabel}
@value-changed=${this._formValueChanged}
></ha-form>
`
: nothing}
`; `;
} }
@ -289,7 +314,15 @@ export class HuiActionEditor extends LitElement {
}); });
} }
private _computeFormLabel(schema: SchemaUnion<typeof ASSIST_SCHEMA>) { private _computeFormLabel(
schema:
| SchemaUnion<typeof ASSIST_SCHEMA>
| SchemaUnion<typeof NAVIGATE_SCHEMA>
| SchemaUnion<typeof SEQUENCE_SCHEMA>
) {
if (schema.name === "actions") {
return "";
}
return this.hass?.localize( return this.hass?.localize(
`ui.panel.lovelace.editor.action-editor.${schema.name}` `ui.panel.lovelace.editor.action-editor.${schema.name}`
); );

View File

@ -48,6 +48,12 @@ const actionConfigStructService = object({
confirmation: optional(actionConfigStructConfirmation), confirmation: optional(actionConfigStructConfirmation),
}); });
const actionConfigStructSequence = object({
action: literal("sequence"),
actions: optional(array(object())),
confirmation: optional(actionConfigStructConfirmation),
});
const actionConfigStructNavigate = object({ const actionConfigStructNavigate = object({
action: literal("navigate"), action: literal("navigate"),
navigation_path: string(), navigation_path: string(),
@ -101,6 +107,9 @@ export const actionConfigStruct = dynamic<any>((value) => {
case "more-info": { case "more-info": {
return actionConfigStructMoreInfo; return actionConfigStructMoreInfo;
} }
case "sequence": {
return actionConfigStructSequence;
}
} }
} }

View File

@ -5752,10 +5752,12 @@
"more-info": "More info", "more-info": "More info",
"toggle": "Toggle", "toggle": "Toggle",
"navigate": "Navigate", "navigate": "Navigate",
"sequence": "Sequence",
"assist": "Assist", "assist": "Assist",
"url": "URL", "url": "URL",
"none": "Nothing" "none": "Nothing"
} },
"sequence_actions": "Actions"
}, },
"condition-editor": { "condition-editor": {
"explanation": "The card will be shown when ALL conditions below are fulfilled.", "explanation": "The card will be shown when ALL conditions below are fulfilled.",