From 52fdbfe44c5acf75cf7509de81b128dde199ef91 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 19 Mar 2024 14:40:25 +0100 Subject: [PATCH] Refactor get value from entity id --- .../lovelace/common/validate-condition.ts | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/panels/lovelace/common/validate-condition.ts b/src/panels/lovelace/common/validate-condition.ts index 00294ff9fe..0a3a35809d 100644 --- a/src/panels/lovelace/common/validate-condition.ts +++ b/src/panels/lovelace/common/validate-condition.ts @@ -56,18 +56,9 @@ export interface AndCondition extends BaseCondition { conditions?: Condition[]; } -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); +function getValueFromEntityId(hass: HomeAssistant, value: string): string { + if (isValidEntityId(value) && hass.states[value]) { + return hass.states[value]?.state; } return value; } @@ -83,9 +74,12 @@ 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") { + if (typeof value === "string") { value = getValueFromEntityId(hass, value); } + if (Array.isArray(value)) { + value = value.map((val) => getValueFromEntityId(hass, val)); + } return condition.state != null ? ensureArray(value).includes(state)