Refactor get value from entity id
This commit is contained in:
parent
552eeeddf6
commit
52fdbfe44c
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue