Improve/extend description of conditions (#15943)

This commit is contained in:
Franck Nijhof 2023-03-28 16:43:00 +02:00 committed by GitHub
parent 48c74c8660
commit a6f9482bf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 13 deletions

View File

@ -197,6 +197,7 @@ export interface StateCondition extends BaseCondition {
attribute?: string; attribute?: string;
state: string | number | string[]; state: string | number | string[];
for?: string | number | ForDict; for?: string | number | ForDict;
match?: "all" | "any";
} }
export interface NumericStateCondition extends BaseCondition { export interface NumericStateCondition extends BaseCondition {

View File

@ -513,36 +513,108 @@ export const describeCondition = (
// State Condition // State Condition
if (condition.condition === "state") { if (condition.condition === "state") {
let base = "Confirm"; let base = "Confirm";
const stateObj = hass.states[condition.entity_id]; if (!condition.entity_id) {
const entity = stateObj return `${base} state`;
? computeStateName(stateObj) }
: condition.entity_id
? condition.entity_id
: "an entity";
if ("attribute" in condition) { if (condition.attribute) {
base += ` ${condition.attribute} from`; const stateObj = Array.isArray(condition.entity_id)
? hass.states[condition.entity_id[0]]
: hass.states[condition.entity_id];
base += ` ${computeAttributeNameDisplay(
hass.localize,
stateObj,
hass.entities,
condition.attribute
)} of`;
}
if (Array.isArray(condition.entity_id)) {
let entities = "";
for (const [index, entity] of condition.entity_id.entries()) {
if (hass.states[entity]) {
entities += `${index > 0 ? "," : ""} ${
condition.entity_id.length > 1 &&
index === condition.entity_id.length - 1
? condition.match === "any"
? "or"
: "and"
: ""
} ${computeStateName(hass.states[entity]) || entity}`;
}
}
if (entities) {
base += ` ${entities} ${condition.entity_id.length > 1 ? "are" : "is"}`;
} else {
// no entity_id or empty array
base += " an entity";
}
} else if (condition.entity_id) {
base += ` ${
hass.states[condition.entity_id]
? computeStateName(hass.states[condition.entity_id])
: condition.entity_id
} is`;
} }
let states = ""; let states = "";
const stateObj =
hass.states[
Array.isArray(condition.entity_id)
? condition.entity_id[0]
: condition.entity_id
];
if (Array.isArray(condition.state)) { if (Array.isArray(condition.state)) {
for (const [index, state] of condition.state.entries()) { for (const [index, state] of condition.state.entries()) {
states += `${index > 0 ? "," : ""} ${ states += `${index > 0 ? "," : ""} ${
condition.state.length > 1 && index === condition.state.length - 1 condition.state.length > 1 && index === condition.state.length - 1
? "or" ? "or"
: "" : ""
} ${state}`; } '${
condition.attribute
? computeAttributeValueDisplay(
hass.localize,
stateObj,
hass.locale,
hass.entities,
condition.attribute,
state
)
: computeStateDisplay(
hass.localize,
stateObj,
hass.locale,
hass.entities,
state
)
}'`;
} }
} else if (condition.state) { } else if (condition.state !== "") {
states = condition.state.toString(); states = `'${
condition.attribute
? computeAttributeValueDisplay(
hass.localize,
stateObj,
hass.locale,
hass.entities,
condition.attribute,
condition.state
).toString()
: computeStateDisplay(
hass.localize,
stateObj,
hass.locale,
hass.entities,
condition.state.toString()
).toString()
}'`;
} }
if (!states) { if (!states) {
states = "a state"; states = "a state";
} }
base += ` ${entity} is ${states}`; base += ` ${states}`;
if (condition.for) { if (condition.for) {
const duration = describeDuration(condition.for); const duration = describeDuration(condition.for);