mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-10 01:47:46 +00:00
review: make numeric_state below & above working together again, with entity_id support
This commit is contained in:
parent
69310d2d45
commit
f2f9ddce33
@ -245,7 +245,7 @@ const CONFIGS = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
heading:
|
heading:
|
||||||
"Filtered entities by condition above (> lowest_price) [new syntax]",
|
"Filtered entities by condition above (between 1.73 and lowest_price) [new syntax]",
|
||||||
config: `
|
config: `
|
||||||
- type: entity-filter
|
- type: entity-filter
|
||||||
entities:
|
entities:
|
||||||
@ -255,6 +255,7 @@ const CONFIGS = [
|
|||||||
conditions:
|
conditions:
|
||||||
- condition: numeric_state
|
- condition: numeric_state
|
||||||
above: sensor.gas_station_lowest_price
|
above: sensor.gas_station_lowest_price
|
||||||
|
below: 1.73
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -76,6 +76,25 @@ export interface AndCondition extends BaseCondition {
|
|||||||
conditions?: Condition[];
|
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(
|
function checkLegacyFilterCondition(
|
||||||
condition: LegacyFilterCondition,
|
condition: LegacyFilterCondition,
|
||||||
hass: HomeAssistant
|
hass: HomeAssistant
|
||||||
@ -90,19 +109,8 @@ function checkLegacyFilterCondition(
|
|||||||
? entity.attributes[condition.attribute]
|
? entity.attributes[condition.attribute]
|
||||||
: entity.state;
|
: entity.state;
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value) || typeof value === "string") {
|
||||||
value = value.map((v) => {
|
value = getValueFromEntityId(hass, value);
|
||||||
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 (condition.operator === "==" || condition.operator === "!=") {
|
if (condition.operator === "==" || condition.operator === "!=") {
|
||||||
@ -163,19 +171,8 @@ 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)) {
|
if (Array.isArray(value) || typeof value === "string") {
|
||||||
value = value.map((v) => {
|
value = getValueFromEntityId(hass, value);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return condition.state != null
|
return condition.state != null
|
||||||
@ -189,27 +186,32 @@ function checkStateNumericCondition(
|
|||||||
) {
|
) {
|
||||||
const state = (condition.entity ? hass.states[condition.entity] : undefined)
|
const state = (condition.entity ? hass.states[condition.entity] : undefined)
|
||||||
?.state;
|
?.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)
|
// Handle entity_id, UI should be updated for conditionnal card (filters does not have UI for now)
|
||||||
if (
|
if (typeof above === "string") {
|
||||||
typeof value === "string" &&
|
above = getValueFromEntityId(hass, above) as string;
|
||||||
isValidEntityId(value) &&
|
}
|
||||||
hass.states[value]
|
if (typeof below === "string") {
|
||||||
) {
|
below = getValueFromEntityId(hass, below) as string;
|
||||||
value = hass.states[value]?.state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const numericState = Number(state);
|
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 false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
(condition.above && numericValue < numericState) ||
|
(condition.above == null ||
|
||||||
(condition.below && numericValue > numericState)
|
isNaN(numericAbove) ||
|
||||||
|
numericAbove < numericState) &&
|
||||||
|
(condition.below == null ||
|
||||||
|
isNaN(numericBelow) ||
|
||||||
|
numericBelow > numericState)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user