Refactor get value from entity id

This commit is contained in:
Paul Bottein 2024-03-19 14:40:25 +01:00
parent 552eeeddf6
commit 52fdbfe44c
No known key found for this signature in database

View File

@ -56,18 +56,9 @@ export interface AndCondition extends BaseCondition {
conditions?: Condition[]; conditions?: Condition[];
} }
function getValueFromEntityId( function getValueFromEntityId(hass: HomeAssistant, value: string): string {
hass: HomeAssistant, if (isValidEntityId(value) && hass.states[value]) {
value: string | string[] return hass.states[value]?.state;
): 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);
} }
return value; return value;
} }
@ -83,9 +74,12 @@ 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 (typeof value === "string") {
value = getValueFromEntityId(hass, value); value = getValueFromEntityId(hass, value);
} }
if (Array.isArray(value)) {
value = value.map((val) => getValueFromEntityId(hass, val));
}
return condition.state != null return condition.state != null
? ensureArray(value).includes(state) ? ensureArray(value).includes(state)