Check for entity state and entity string in conditional card (#20331)

Co-authored-by: Simon Lamon <32477463+silamon@users.noreply.github.com>
This commit is contained in:
Paul Bottein 2024-04-02 20:39:39 +02:00 committed by GitHub
parent 9bef5c2af9
commit 41fdf31e34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -58,18 +58,12 @@ export interface AndCondition extends BaseCondition {
function getValueFromEntityId( function getValueFromEntityId(
hass: HomeAssistant, hass: HomeAssistant,
value: string | string[] value: string
): string | string[] { ): string | undefined {
if ( if (isValidEntityId(value) && hass.states[value]) {
typeof value === "string" && return hass.states[value]?.state;
isValidEntityId(value) &&
hass.states[value]
) {
value = hass.states[value]?.state;
} else if (Array.isArray(value)) {
value = value.map((v) => getValueFromEntityId(hass, v) as string);
} }
return value; return undefined;
} }
function checkStateCondition( function checkStateCondition(
@ -83,8 +77,17 @@ function checkStateCondition(
let value = condition.state ?? condition.state_not; let value = condition.state ?? condition.state_not;
// Handle entity_id, UI should be updated for conditionnal card (filters does not have UI for now) // 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 (Array.isArray(value)) {
value = getValueFromEntityId(hass, 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 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) // Handle entity_id, UI should be updated for conditionnal card (filters does not have UI for now)
if (typeof above === "string") { if (typeof above === "string") {
above = getValueFromEntityId(hass, above) as string; above = getValueFromEntityId(hass, above) ?? above;
} }
if (typeof below === "string") { if (typeof below === "string") {
below = getValueFromEntityId(hass, below) as string; below = getValueFromEntityId(hass, below) ?? below;
} }
const numericState = Number(state); const numericState = Number(state);