From 41fdf31e34c5ba31bc68cb059f4901c17befd448 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 2 Apr 2024 20:39:39 +0200 Subject: [PATCH] Check for entity state and entity string in conditional card (#20331) Co-authored-by: Simon Lamon <32477463+silamon@users.noreply.github.com> --- .../lovelace/common/validate-condition.ts | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/panels/lovelace/common/validate-condition.ts b/src/panels/lovelace/common/validate-condition.ts index 00294ff9fe..908d5719dd 100644 --- a/src/panels/lovelace/common/validate-condition.ts +++ b/src/panels/lovelace/common/validate-condition.ts @@ -58,18 +58,12 @@ export interface AndCondition extends BaseCondition { function getValueFromEntityId( hass: HomeAssistant, - value: string | string[] -): string | string[] { - if ( - typeof value === "string" && - isValidEntityId(value) && - hass.states[value] - ) { - value = hass.states[value]?.state; - } else if (Array.isArray(value)) { - value = value.map((v) => getValueFromEntityId(hass, v) as string); + value: string +): string | undefined { + if (isValidEntityId(value) && hass.states[value]) { + return hass.states[value]?.state; } - return value; + return undefined; } function checkStateCondition( @@ -83,8 +77,17 @@ function checkStateCondition( let value = condition.state ?? condition.state_not; // Handle entity_id, UI should be updated for conditionnal card (filters does not have UI for now) - if (Array.isArray(value) || typeof value === "string") { - value = getValueFromEntityId(hass, value); + if (Array.isArray(value)) { + const entityValues = value + .map((v) => getValueFromEntityId(hass, v)) + .filter((v): v is string => v !== undefined); + value = [...value, ...entityValues]; + } else if (typeof value === "string") { + const entityValue = getValueFromEntityId(hass, value); + value = [value]; + if (entityValue) { + value.push(entityValue); + } } return condition.state != null @@ -103,10 +106,10 @@ function checkStateNumericCondition( // Handle entity_id, UI should be updated for conditionnal card (filters does not have UI for now) if (typeof above === "string") { - above = getValueFromEntityId(hass, above) as string; + above = getValueFromEntityId(hass, above) ?? above; } if (typeof below === "string") { - below = getValueFromEntityId(hass, below) as string; + below = getValueFromEntityId(hass, below) ?? below; } const numericState = Number(state);