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
1 changed files with 18 additions and 15 deletions

View File

@ -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);