mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-25 05:47:20 +00:00
Fix some descriptions (#13562)
This commit is contained in:
parent
9f9b0b6457
commit
ffad6f340f
@ -1,4 +1,5 @@
|
|||||||
import secondsToDuration from "../common/datetime/seconds_to_duration";
|
import secondsToDuration from "../common/datetime/seconds_to_duration";
|
||||||
|
import { ensureArray } from "../common/ensure-array";
|
||||||
import { computeStateName } from "../common/entity/compute_state_name";
|
import { computeStateName } from "../common/entity/compute_state_name";
|
||||||
import type { HomeAssistant } from "../types";
|
import type { HomeAssistant } from "../types";
|
||||||
import { Condition, Trigger } from "./automation";
|
import { Condition, Trigger } from "./automation";
|
||||||
@ -74,7 +75,7 @@ export const describeTrigger = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// State Trigger
|
// State Trigger
|
||||||
if (trigger.platform === "state" && trigger.entity_id) {
|
if (trigger.platform === "state") {
|
||||||
let base = "When";
|
let base = "When";
|
||||||
let entities = "";
|
let entities = "";
|
||||||
|
|
||||||
@ -95,12 +96,17 @@ export const describeTrigger = (
|
|||||||
} ${computeStateName(states[entity]) || entity}`;
|
} ${computeStateName(states[entity]) || entity}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else if (trigger.entity_id) {
|
||||||
entities = states[trigger.entity_id]
|
entities = states[trigger.entity_id]
|
||||||
? computeStateName(states[trigger.entity_id])
|
? computeStateName(states[trigger.entity_id])
|
||||||
: trigger.entity_id;
|
: trigger.entity_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!entities) {
|
||||||
|
// no entity_id or empty array
|
||||||
|
entities = "something";
|
||||||
|
}
|
||||||
|
|
||||||
base += ` ${entities} changes`;
|
base += ` ${entities} changes`;
|
||||||
|
|
||||||
if (trigger.from) {
|
if (trigger.from) {
|
||||||
@ -286,7 +292,7 @@ export const describeTrigger = (
|
|||||||
}
|
}
|
||||||
// MQTT Trigger
|
// MQTT Trigger
|
||||||
if (trigger.platform === "mqtt") {
|
if (trigger.platform === "mqtt") {
|
||||||
return "When a MQTT payload has been received";
|
return "When an MQTT message has been received";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Template Trigger
|
// Template Trigger
|
||||||
@ -300,6 +306,9 @@ export const describeTrigger = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (trigger.platform === "device") {
|
if (trigger.platform === "device") {
|
||||||
|
if (!trigger.device_id) {
|
||||||
|
return "Device trigger";
|
||||||
|
}
|
||||||
const config = trigger as DeviceTrigger;
|
const config = trigger as DeviceTrigger;
|
||||||
const localized = localizeDeviceAutomationTrigger(hass, config);
|
const localized = localizeDeviceAutomationTrigger(hass, config);
|
||||||
if (localized) {
|
if (localized) {
|
||||||
@ -311,7 +320,9 @@ export const describeTrigger = (
|
|||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${trigger.platform || "Unknown"} trigger`;
|
return `${
|
||||||
|
trigger.platform ? trigger.platform.replace(/_/g, " ") : "Unknown"
|
||||||
|
} trigger`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const describeCondition = (
|
export const describeCondition = (
|
||||||
@ -323,15 +334,64 @@ export const describeCondition = (
|
|||||||
return condition.alias;
|
return condition.alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (["or", "and", "not"].includes(condition.condition)) {
|
if (!condition.condition) {
|
||||||
return `multiple conditions using "${condition.condition}"`;
|
const shorthands: Array<"and" | "or" | "not"> = ["and", "or", "not"];
|
||||||
|
for (const key of shorthands) {
|
||||||
|
if (!(key in condition)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (ensureArray(condition[key])) {
|
||||||
|
condition = {
|
||||||
|
condition: key,
|
||||||
|
conditions: condition[key],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (condition.condition === "or") {
|
||||||
|
const conditions = ensureArray(condition.conditions);
|
||||||
|
|
||||||
|
let count = "condition";
|
||||||
|
|
||||||
|
if (conditions && conditions.length > 0) {
|
||||||
|
count = `of ${conditions.length} conditions`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `Test if any ${count} matches`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (condition.condition === "and") {
|
||||||
|
const conditions = ensureArray(condition.conditions);
|
||||||
|
|
||||||
|
const count =
|
||||||
|
conditions && conditions.length > 0
|
||||||
|
? `${conditions.length} `
|
||||||
|
: "multiple";
|
||||||
|
|
||||||
|
return `Test if ${count} conditions match`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (condition.condition === "not") {
|
||||||
|
const conditions = ensureArray(condition.conditions);
|
||||||
|
|
||||||
|
const what =
|
||||||
|
conditions && conditions.length > 0
|
||||||
|
? `none of ${conditions.length} conditions match`
|
||||||
|
: "no condition matches";
|
||||||
|
|
||||||
|
return `Test if ${what}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// State Condition
|
// State Condition
|
||||||
if (condition.condition === "state" && condition.entity_id) {
|
if (condition.condition === "state") {
|
||||||
let base = "Confirm";
|
let base = "Confirm";
|
||||||
const stateObj = hass.states[condition.entity_id];
|
const stateObj = hass.states[condition.entity_id];
|
||||||
const entity = stateObj ? computeStateName(stateObj) : condition.entity_id;
|
const entity = stateObj
|
||||||
|
? computeStateName(stateObj)
|
||||||
|
: condition.entity_id
|
||||||
|
? condition.entity_id
|
||||||
|
: "an entity";
|
||||||
|
|
||||||
if ("attribute" in condition) {
|
if ("attribute" in condition) {
|
||||||
base += ` ${condition.attribute} from`;
|
base += ` ${condition.attribute} from`;
|
||||||
@ -347,10 +407,14 @@ export const describeCondition = (
|
|||||||
: ""
|
: ""
|
||||||
} ${state}`;
|
} ${state}`;
|
||||||
}
|
}
|
||||||
} else {
|
} else if (condition.state) {
|
||||||
states = condition.state.toString();
|
states = condition.state.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!states) {
|
||||||
|
states = "a state";
|
||||||
|
}
|
||||||
|
|
||||||
base += ` ${entity} is ${states}`;
|
base += ` ${entity} is ${states}`;
|
||||||
|
|
||||||
if ("for" in condition) {
|
if ("for" in condition) {
|
||||||
@ -487,6 +551,9 @@ export const describeCondition = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (condition.condition === "device") {
|
if (condition.condition === "device") {
|
||||||
|
if (!condition.device_id) {
|
||||||
|
return "Device condition";
|
||||||
|
}
|
||||||
const config = condition as DeviceCondition;
|
const config = condition as DeviceCondition;
|
||||||
const localized = localizeDeviceAutomationCondition(hass, config);
|
const localized = localizeDeviceAutomationCondition(hass, config);
|
||||||
if (localized) {
|
if (localized) {
|
||||||
@ -498,5 +565,7 @@ export const describeCondition = (
|
|||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${condition.condition} condition`;
|
return `${
|
||||||
|
condition.condition ? condition.condition.replace(/_/g, " ") : "Unknown"
|
||||||
|
} condition`;
|
||||||
};
|
};
|
||||||
|
@ -61,7 +61,7 @@ export const describeAction = <T extends ActionType>(
|
|||||||
? `${domainToName(hass.localize, domain)}: ${service.name}`
|
? `${domainToName(hass.localize, domain)}: ${service.name}`
|
||||||
: `Call service: ${config.service}`;
|
: `Call service: ${config.service}`;
|
||||||
} else {
|
} else {
|
||||||
return actionType;
|
return "Call a service";
|
||||||
}
|
}
|
||||||
if (config.target) {
|
if (config.target) {
|
||||||
const targets: string[] = [];
|
const targets: string[] = [];
|
||||||
@ -137,9 +137,11 @@ export const describeAction = <T extends ActionType>(
|
|||||||
} else if (typeof config.delay === "string") {
|
} else if (typeof config.delay === "string") {
|
||||||
duration = isTemplate(config.delay)
|
duration = isTemplate(config.delay)
|
||||||
? "based on a template"
|
? "based on a template"
|
||||||
: `for ${config.delay}`;
|
: `for ${config.delay || "a duration"}`;
|
||||||
} else {
|
} else if (config.delay) {
|
||||||
duration = `for ${formatDuration(config.delay)}`;
|
duration = `for ${formatDuration(config.delay)}`;
|
||||||
|
} else {
|
||||||
|
duration = "for a duration";
|
||||||
}
|
}
|
||||||
|
|
||||||
return `Delay ${duration}`;
|
return `Delay ${duration}`;
|
||||||
@ -153,13 +155,12 @@ export const describeAction = <T extends ActionType>(
|
|||||||
} else {
|
} else {
|
||||||
entityId = config.target?.entity_id || config.entity_id;
|
entityId = config.target?.entity_id || config.entity_id;
|
||||||
}
|
}
|
||||||
|
if (!entityId) {
|
||||||
|
return "Activate a scene";
|
||||||
|
}
|
||||||
const sceneStateObj = entityId ? hass.states[entityId] : undefined;
|
const sceneStateObj = entityId ? hass.states[entityId] : undefined;
|
||||||
return `Scene ${
|
return `Active scene ${
|
||||||
sceneStateObj
|
sceneStateObj ? computeStateName(sceneStateObj) : entityId
|
||||||
? computeStateName(sceneStateObj)
|
|
||||||
: "scene" in config
|
|
||||||
? config.scene
|
|
||||||
: config.target?.entity_id || config.entity_id || ""
|
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,16 +168,22 @@ export const describeAction = <T extends ActionType>(
|
|||||||
const config = action as PlayMediaAction;
|
const config = action as PlayMediaAction;
|
||||||
const entityId = config.target?.entity_id || config.entity_id;
|
const entityId = config.target?.entity_id || config.entity_id;
|
||||||
const mediaStateObj = entityId ? hass.states[entityId] : undefined;
|
const mediaStateObj = entityId ? hass.states[entityId] : undefined;
|
||||||
return `Play ${config.metadata.title || config.data.media_content_id} on ${
|
return `Play ${
|
||||||
|
config.metadata.title || config.data.media_content_id || "media"
|
||||||
|
} on ${
|
||||||
mediaStateObj
|
mediaStateObj
|
||||||
? computeStateName(mediaStateObj)
|
? computeStateName(mediaStateObj)
|
||||||
: config.target?.entity_id || config.entity_id
|
: entityId || "a media player"
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (actionType === "wait_for_trigger") {
|
if (actionType === "wait_for_trigger") {
|
||||||
const config = action as WaitForTriggerAction;
|
const config = action as WaitForTriggerAction;
|
||||||
return `Wait for ${ensureArray(config.wait_for_trigger)
|
const triggers = ensureArray(config.wait_for_trigger);
|
||||||
|
if (!triggers || triggers.length === 0) {
|
||||||
|
return "Wait for a trigger";
|
||||||
|
}
|
||||||
|
return `Wait for ${triggers
|
||||||
.map((trigger) => describeTrigger(trigger, hass))
|
.map((trigger) => describeTrigger(trigger, hass))
|
||||||
.join(", ")}`;
|
.join(", ")}`;
|
||||||
}
|
}
|
||||||
@ -199,12 +206,12 @@ export const describeAction = <T extends ActionType>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (actionType === "check_condition") {
|
if (actionType === "check_condition") {
|
||||||
return `Test ${describeCondition(action as Condition, hass)}`;
|
return describeCondition(action as Condition, hass);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (actionType === "stop") {
|
if (actionType === "stop") {
|
||||||
const config = action as StopAction;
|
const config = action as StopAction;
|
||||||
return `Stopped${config.stop ? ` because: ${config.stop}` : ""}`;
|
return `Stop${config.stop ? ` because: ${config.stop}` : ""}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (actionType === "if") {
|
if (actionType === "if") {
|
||||||
@ -258,6 +265,9 @@ export const describeAction = <T extends ActionType>(
|
|||||||
|
|
||||||
if (actionType === "device_action") {
|
if (actionType === "device_action") {
|
||||||
const config = action as DeviceAction;
|
const config = action as DeviceAction;
|
||||||
|
if (!config.device_id) {
|
||||||
|
return "Device action";
|
||||||
|
}
|
||||||
const localized = localizeDeviceAutomationAction(hass, config);
|
const localized = localizeDeviceAutomationAction(hass, config);
|
||||||
if (localized) {
|
if (localized) {
|
||||||
return localized;
|
return localized;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user