improve duration rendering for state trigger (#13723)

This commit is contained in:
Bram Kragten 2022-09-13 20:49:39 +02:00 committed by GitHub
parent 5d4c090b26
commit 23e5a47b3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
import { formatDuration } from "../common/datetime/format_duration";
import secondsToDuration from "../common/datetime/seconds_to_duration"; import secondsToDuration from "../common/datetime/seconds_to_duration";
import { ensureArray } from "../common/ensure-array"; import { ensureArray } from "../common/ensure-array";
import { computeStateName } from "../common/entity/compute_state_name"; import { computeStateName } from "../common/entity/compute_state_name";
@ -140,18 +141,20 @@ export const describeTrigger = (
base += ` to ${to}`; base += ` to ${to}`;
} }
if ("for" in trigger) { if (trigger.for) {
let duration: string; let duration: string | null;
if (typeof trigger.for === "number") { if (typeof trigger.for === "number") {
duration = `${secondsToDuration(trigger.for)!}`; duration = secondsToDuration(trigger.for);
} else if (typeof trigger.for === "string") { } else if (typeof trigger.for === "string") {
duration = `${trigger.for}`; duration = trigger.for;
} else { } else {
duration = `${JSON.stringify(trigger.for)}`; duration = formatDuration(trigger.for);
} }
if (duration) {
base += ` for ${duration}`; base += ` for ${duration}`;
} }
}
return base; return base;
} }