From f2f9ddce337cdb0d796b7ea0fdb0d37d21a9a476 Mon Sep 17 00:00:00 2001 From: Quentin POLLET Date: Mon, 22 Jan 2024 19:13:25 +0000 Subject: [PATCH] review: make numeric_state below & above working together again, with entity_id support --- .../src/pages/lovelace/entity-filter-card.ts | 3 +- .../lovelace/common/validate-condition.ts | 76 ++++++++++--------- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/gallery/src/pages/lovelace/entity-filter-card.ts b/gallery/src/pages/lovelace/entity-filter-card.ts index 05c0af374d..def9ae8b39 100644 --- a/gallery/src/pages/lovelace/entity-filter-card.ts +++ b/gallery/src/pages/lovelace/entity-filter-card.ts @@ -245,7 +245,7 @@ const CONFIGS = [ }, { heading: - "Filtered entities by condition above (> lowest_price) [new syntax]", + "Filtered entities by condition above (between 1.73 and lowest_price) [new syntax]", config: ` - type: entity-filter entities: @@ -255,6 +255,7 @@ const CONFIGS = [ conditions: - condition: numeric_state above: sensor.gas_station_lowest_price + below: 1.73 `, }, { diff --git a/src/panels/lovelace/common/validate-condition.ts b/src/panels/lovelace/common/validate-condition.ts index ac86fed600..eab274a522 100644 --- a/src/panels/lovelace/common/validate-condition.ts +++ b/src/panels/lovelace/common/validate-condition.ts @@ -76,6 +76,25 @@ export interface AndCondition extends BaseCondition { conditions?: Condition[]; } +function getValueFromEntityId( + hass: HomeAssistant, + value: string | string[] +): string | string[] { + let returned: string | string[]; + if ( + typeof value === "string" && + isValidEntityId(value) && + hass.states[value] + ) { + returned = hass.states[value]?.state; + } else if (Array.isArray(value)) { + returned = value.map((v) => getValueFromEntityId(hass, v) as string); + } else { + returned = value; + } + return returned; +} + function checkLegacyFilterCondition( condition: LegacyFilterCondition, hass: HomeAssistant @@ -90,19 +109,8 @@ function checkLegacyFilterCondition( ? entity.attributes[condition.attribute] : entity.state; - if (Array.isArray(value)) { - value = value.map((v) => { - if (typeof v === "string" && isValidEntityId(v) && hass.states[v]) { - v = hass.states[v]?.state; - } - return `${v}`; - }); - } else if ( - typeof value === "string" && - isValidEntityId(value) && - hass.states[value] - ) { - value = hass.states[value]?.state; + if (Array.isArray(value) || typeof value === "string") { + value = getValueFromEntityId(hass, value); } if (condition.operator === "==" || condition.operator === "!=") { @@ -163,19 +171,8 @@ 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)) { - value = value.map((v) => { - if (typeof v === "string" && isValidEntityId(v) && hass.states[v]) { - v = hass.states[v]?.state; - } - return `${v}`; - }); - } else if ( - typeof value === "string" && - isValidEntityId(value) && - hass.states[value] - ) { - value = hass.states[value]?.state; + if (Array.isArray(value) || typeof value === "string") { + value = getValueFromEntityId(hass, value); } return condition.state != null @@ -189,27 +186,32 @@ function checkStateNumericCondition( ) { const state = (condition.entity ? hass.states[condition.entity] : undefined) ?.state; - let value = condition.above ?? condition.below; + let above = condition.above; + let below = condition.below; // Handle entity_id, UI should be updated for conditionnal card (filters does not have UI for now) - if ( - typeof value === "string" && - isValidEntityId(value) && - hass.states[value] - ) { - value = hass.states[value]?.state; + if (typeof above === "string") { + above = getValueFromEntityId(hass, above) as string; + } + if (typeof below === "string") { + below = getValueFromEntityId(hass, below) as string; } const numericState = Number(state); - const numericValue = Number(value); + const numericAbove = Number(above); + const numericBelow = Number(below); - if (isNaN(numericState) || isNaN(numericValue)) { + if (isNaN(numericState)) { return false; } return ( - (condition.above && numericValue < numericState) || - (condition.below && numericValue > numericState) + (condition.above == null || + isNaN(numericAbove) || + numericAbove < numericState) && + (condition.below == null || + isNaN(numericBelow) || + numericBelow > numericState) ); }