diff --git a/src/common/ensure-array.ts b/src/common/ensure-array.ts index 43d3dbb1f5..4290c040c1 100644 --- a/src/common/ensure-array.ts +++ b/src/common/ensure-array.ts @@ -1,6 +1,8 @@ -export const ensureArray = (value?: any) => { - if (!value || Array.isArray(value)) { +export function ensureArray(value: undefined): undefined; +export function ensureArray(value: T | T[]): T[]; +export function ensureArray(value) { + if (value === undefined || Array.isArray(value)) { return value; } return [value]; -}; +} diff --git a/src/components/ha-target-picker.ts b/src/components/ha-target-picker.ts index 574c1b36f2..11eabcae30 100644 --- a/src/components/ha-target-picker.ts +++ b/src/components/ha-target-picker.ts @@ -125,35 +125,41 @@ export class HaTargetPicker extends SubscribeMixin(LitElement) { return html``; } return html`
- ${ensureArray(this.value?.area_id)?.map((area_id) => { - const area = this._areas![area_id]; - return this._renderChip( - "area_id", - area_id, - area?.name || area_id, - undefined, - mdiSofa - ); - })} - ${ensureArray(this.value?.device_id)?.map((device_id) => { - const device = this._devices![device_id]; - return this._renderChip( - "device_id", - device_id, - device ? computeDeviceName(device, this.hass) : device_id, - undefined, - mdiDevices - ); - })} - ${ensureArray(this.value?.entity_id)?.map((entity_id) => { - const entity = this.hass.states[entity_id]; - return this._renderChip( - "entity_id", - entity_id, - entity ? computeStateName(entity) : entity_id, - entity ? stateIcon(entity) : undefined - ); - })} + ${this.value?.area_id + ? ensureArray(this.value.area_id).map((area_id) => { + const area = this._areas![area_id]; + return this._renderChip( + "area_id", + area_id, + area?.name || area_id, + undefined, + mdiSofa + ); + }) + : ""} + ${this.value?.device_id + ? ensureArray(this.value.device_id).map((device_id) => { + const device = this._devices![device_id]; + return this._renderChip( + "device_id", + device_id, + device ? computeDeviceName(device, this.hass) : device_id, + undefined, + mdiDevices + ); + }) + : ""} + ${this.value?.entity_id + ? ensureArray(this.value.entity_id).map((entity_id) => { + const entity = this.hass.states[entity_id]; + return this._renderChip( + "entity_id", + entity_id, + entity ? computeStateName(entity) : entity_id, + entity ? stateIcon(entity) : undefined + ); + }) + : ""}
${this._renderPicker()}
diff --git a/src/components/trace/hat-script-graph.ts b/src/components/trace/hat-script-graph.ts index d347adad77..7a5e04cac7 100644 --- a/src/components/trace/hat-script-graph.ts +++ b/src/components/trace/hat-script-graph.ts @@ -48,6 +48,7 @@ import { WaitAction, WaitForTriggerAction, } from "../../data/script"; +import { ensureArray } from "../../common/ensure-array"; declare global { interface HASSDomEvents { @@ -412,16 +413,14 @@ class HatScriptGraph extends LitElement { const manual_triggered = this.trace && "trigger" in this.trace.trace; let track_path = manual_triggered ? undefined : [0]; - const trigger_nodes = (Array.isArray(this.trace.config.trigger) - ? this.trace.config.trigger - : [this.trace.config.trigger] - ).map((trigger, i) => { - if (this.trace && `trigger/${i}` in this.trace.trace) { - track_path = [i]; + const trigger_nodes = ensureArray(this.trace.config.trigger).map( + (trigger, 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`
@@ -435,16 +434,13 @@ class HatScriptGraph extends LitElement { ${trigger_nodes}
- ${(!this.trace.config.condition || - Array.isArray(this.trace.config.condition) - ? this.trace.config.condition - : [this.trace.config.condition] - )?.map((condition, i) => this.render_condition(condition, i))} + ${ensureArray(this.trace.config.condition)?.map((condition, i) => + this.render_condition(condition!, i) + )} - ${(Array.isArray(this.trace.config.action) - ? this.trace.config.action - : [this.trace.config.action] - ).map((action, i) => this.render_node(action, `action/${i}`))} + ${ensureArray(this.trace.config.action).map((action, i) => + this.render_node(action, `action/${i}`) + )}
( if (actionType === "wait_for_trigger") { const config = action as WaitForTriggerAction; - return `Wait for ${config.wait_for_trigger + return `Wait for ${ensureArray(config.wait_for_trigger) .map((trigger) => describeTrigger(trigger)) .join(", ")}`; } diff --git a/src/data/trace.ts b/src/data/trace.ts index fd38797ac8..6953353b87 100644 --- a/src/data/trace.ts +++ b/src/data/trace.ts @@ -1,6 +1,6 @@ import { strStartsWith } from "../common/string/starts-with"; import { HomeAssistant, Context } from "../types"; -import { AutomationConfig } from "./automation"; +import { ManualAutomationConfig } from "./automation"; interface BaseTraceStep { path: string; @@ -86,7 +86,7 @@ export interface AutomationTrace { export interface AutomationTraceExtended extends AutomationTrace { trace: Record; context: Context; - config: AutomationConfig; + config: ManualAutomationConfig; error?: string; } @@ -138,7 +138,7 @@ export const loadTraceContexts = ( }); export const getDataFromPath = ( - config: AutomationConfig, + config: ManualAutomationConfig, path: string ): any => { const parts = path.split("/").reverse();