mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-08 09:56:36 +00:00
Improve ensureArray and use it in tracing (#8785)
* Improve ensureArray and use it in tracing * Fix typing Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
401064d3c8
commit
deca6f03ba
@ -1,6 +1,8 @@
|
|||||||
export const ensureArray = (value?: any) => {
|
export function ensureArray(value: undefined): undefined;
|
||||||
if (!value || Array.isArray(value)) {
|
export function ensureArray<T>(value: T | T[]): T[];
|
||||||
|
export function ensureArray(value) {
|
||||||
|
if (value === undefined || Array.isArray(value)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
return [value];
|
return [value];
|
||||||
};
|
}
|
||||||
|
@ -125,35 +125,41 @@ export class HaTargetPicker extends SubscribeMixin(LitElement) {
|
|||||||
return html``;
|
return html``;
|
||||||
}
|
}
|
||||||
return html`<div class="mdc-chip-set items">
|
return html`<div class="mdc-chip-set items">
|
||||||
${ensureArray(this.value?.area_id)?.map((area_id) => {
|
${this.value?.area_id
|
||||||
const area = this._areas![area_id];
|
? ensureArray(this.value.area_id).map((area_id) => {
|
||||||
return this._renderChip(
|
const area = this._areas![area_id];
|
||||||
"area_id",
|
return this._renderChip(
|
||||||
area_id,
|
"area_id",
|
||||||
area?.name || area_id,
|
area_id,
|
||||||
undefined,
|
area?.name || area_id,
|
||||||
mdiSofa
|
undefined,
|
||||||
);
|
mdiSofa
|
||||||
})}
|
);
|
||||||
${ensureArray(this.value?.device_id)?.map((device_id) => {
|
})
|
||||||
const device = this._devices![device_id];
|
: ""}
|
||||||
return this._renderChip(
|
${this.value?.device_id
|
||||||
"device_id",
|
? ensureArray(this.value.device_id).map((device_id) => {
|
||||||
device_id,
|
const device = this._devices![device_id];
|
||||||
device ? computeDeviceName(device, this.hass) : device_id,
|
return this._renderChip(
|
||||||
undefined,
|
"device_id",
|
||||||
mdiDevices
|
device_id,
|
||||||
);
|
device ? computeDeviceName(device, this.hass) : device_id,
|
||||||
})}
|
undefined,
|
||||||
${ensureArray(this.value?.entity_id)?.map((entity_id) => {
|
mdiDevices
|
||||||
const entity = this.hass.states[entity_id];
|
);
|
||||||
return this._renderChip(
|
})
|
||||||
"entity_id",
|
: ""}
|
||||||
entity_id,
|
${this.value?.entity_id
|
||||||
entity ? computeStateName(entity) : entity_id,
|
? ensureArray(this.value.entity_id).map((entity_id) => {
|
||||||
entity ? stateIcon(entity) : undefined
|
const entity = this.hass.states[entity_id];
|
||||||
);
|
return this._renderChip(
|
||||||
})}
|
"entity_id",
|
||||||
|
entity_id,
|
||||||
|
entity ? computeStateName(entity) : entity_id,
|
||||||
|
entity ? stateIcon(entity) : undefined
|
||||||
|
);
|
||||||
|
})
|
||||||
|
: ""}
|
||||||
</div>
|
</div>
|
||||||
${this._renderPicker()}
|
${this._renderPicker()}
|
||||||
<div class="mdc-chip-set">
|
<div class="mdc-chip-set">
|
||||||
|
@ -48,6 +48,7 @@ import {
|
|||||||
WaitAction,
|
WaitAction,
|
||||||
WaitForTriggerAction,
|
WaitForTriggerAction,
|
||||||
} from "../../data/script";
|
} from "../../data/script";
|
||||||
|
import { ensureArray } from "../../common/ensure-array";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface HASSDomEvents {
|
interface HASSDomEvents {
|
||||||
@ -412,16 +413,14 @@ class HatScriptGraph extends LitElement {
|
|||||||
|
|
||||||
const manual_triggered = this.trace && "trigger" in this.trace.trace;
|
const manual_triggered = this.trace && "trigger" in this.trace.trace;
|
||||||
let track_path = manual_triggered ? undefined : [0];
|
let track_path = manual_triggered ? undefined : [0];
|
||||||
const trigger_nodes = (Array.isArray(this.trace.config.trigger)
|
const trigger_nodes = ensureArray(this.trace.config.trigger).map(
|
||||||
? this.trace.config.trigger
|
(trigger, i) => {
|
||||||
: [this.trace.config.trigger]
|
if (this.trace && `trigger/${i}` in this.trace.trace) {
|
||||||
).map((trigger, i) => {
|
track_path = [i];
|
||||||
if (this.trace && `trigger/${i}` in this.trace.trace) {
|
}
|
||||||
track_path = [i];
|
return this.render_trigger(trigger, i);
|
||||||
}
|
}
|
||||||
return this.render_trigger(trigger, i);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<hat-graph class="parent">
|
<hat-graph class="parent">
|
||||||
<div></div>
|
<div></div>
|
||||||
@ -435,16 +434,13 @@ class HatScriptGraph extends LitElement {
|
|||||||
${trigger_nodes}
|
${trigger_nodes}
|
||||||
</hat-graph>
|
</hat-graph>
|
||||||
<hat-graph id="condition">
|
<hat-graph id="condition">
|
||||||
${(!this.trace.config.condition ||
|
${ensureArray(this.trace.config.condition)?.map((condition, i) =>
|
||||||
Array.isArray(this.trace.config.condition)
|
this.render_condition(condition!, i)
|
||||||
? this.trace.config.condition
|
)}
|
||||||
: [this.trace.config.condition]
|
|
||||||
)?.map((condition, i) => this.render_condition(condition, i))}
|
|
||||||
</hat-graph>
|
</hat-graph>
|
||||||
${(Array.isArray(this.trace.config.action)
|
${ensureArray(this.trace.config.action).map((action, i) =>
|
||||||
? this.trace.config.action
|
this.render_node(action, `action/${i}`)
|
||||||
: [this.trace.config.action]
|
)}
|
||||||
).map((action, i) => this.render_node(action, `action/${i}`))}
|
|
||||||
</hat-graph>
|
</hat-graph>
|
||||||
<div class="actions">
|
<div class="actions">
|
||||||
<mwc-icon-button
|
<mwc-icon-button
|
||||||
|
@ -77,7 +77,7 @@ export interface WaitAction {
|
|||||||
|
|
||||||
export interface WaitForTriggerAction {
|
export interface WaitForTriggerAction {
|
||||||
alias?: string;
|
alias?: string;
|
||||||
wait_for_trigger: Trigger[];
|
wait_for_trigger: Trigger | Trigger[];
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
continue_on_timeout?: boolean;
|
continue_on_timeout?: boolean;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import secondsToDuration from "../common/datetime/seconds_to_duration";
|
import secondsToDuration from "../common/datetime/seconds_to_duration";
|
||||||
|
import { ensureArray } from "../common/ensure-array";
|
||||||
import { computeStateName } from "../common/entity/compute_state_name";
|
import { computeStateName } from "../common/entity/compute_state_name";
|
||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
import { Condition } from "./automation";
|
import { Condition } from "./automation";
|
||||||
@ -111,7 +112,7 @@ export const describeAction = <T extends ActionType>(
|
|||||||
|
|
||||||
if (actionType === "wait_for_trigger") {
|
if (actionType === "wait_for_trigger") {
|
||||||
const config = action as WaitForTriggerAction;
|
const config = action as WaitForTriggerAction;
|
||||||
return `Wait for ${config.wait_for_trigger
|
return `Wait for ${ensureArray(config.wait_for_trigger)
|
||||||
.map((trigger) => describeTrigger(trigger))
|
.map((trigger) => describeTrigger(trigger))
|
||||||
.join(", ")}`;
|
.join(", ")}`;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { strStartsWith } from "../common/string/starts-with";
|
import { strStartsWith } from "../common/string/starts-with";
|
||||||
import { HomeAssistant, Context } from "../types";
|
import { HomeAssistant, Context } from "../types";
|
||||||
import { AutomationConfig } from "./automation";
|
import { ManualAutomationConfig } from "./automation";
|
||||||
|
|
||||||
interface BaseTraceStep {
|
interface BaseTraceStep {
|
||||||
path: string;
|
path: string;
|
||||||
@ -86,7 +86,7 @@ export interface AutomationTrace {
|
|||||||
export interface AutomationTraceExtended extends AutomationTrace {
|
export interface AutomationTraceExtended extends AutomationTrace {
|
||||||
trace: Record<string, ActionTraceStep[]>;
|
trace: Record<string, ActionTraceStep[]>;
|
||||||
context: Context;
|
context: Context;
|
||||||
config: AutomationConfig;
|
config: ManualAutomationConfig;
|
||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ export const loadTraceContexts = (
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const getDataFromPath = (
|
export const getDataFromPath = (
|
||||||
config: AutomationConfig,
|
config: ManualAutomationConfig,
|
||||||
path: string
|
path: string
|
||||||
): any => {
|
): any => {
|
||||||
const parts = path.split("/").reverse();
|
const parts = path.split("/").reverse();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user