description for condition: time

This commit is contained in:
ari committed 2022-06-30 20:37:51 -04:00
1 parent e6e57be3ed
commit d99512d6e5
4 files changed
+57 -4

No files matched your search

+1
View File
@@ -6,3 +6,4 @@ export type AutomationTimeObject = st.Infer<typeof v.AutomationTimeObject>;
export type AutomationTimeString = st.Infer<typeof v.AutomationTimeString>;
export type EntityId = st.Infer<typeof v.EntityId>;
export type AutomationDeviceState = st.Infer<typeof v.AutomationDeviceState>;
export type DayOfWeek = st.Infer<typeof v.DayOfWeek>;
+9
View File
@@ -55,3 +55,12 @@ export const AutomationDeviceState = st.type({
device_id: st.string(),
domain: st.string(),
});
export const DayOfWeek = st.enums([
"mon",
"tue",
"wed",
"thu",
"fri",
"sat",
"sun",
]);
+7 -4
View File
@@ -1,5 +1,10 @@
import * as st from "superstruct";
import { AutomationDeviceState, AutomationTime, EntityId } from "./common";
import {
AutomationDeviceState,
AutomationTime,
EntityId,
DayOfWeek,
} from "./common";
export const AutomationConditionNodeBase = st.type({
enabled: st.optional(st.boolean()),
@@ -59,9 +64,7 @@ export const TimeCondition = st.assign(
condition: st.literal("time"),
after: st.optional(AutomationTime),
before: st.optional(AutomationTime),
weekday: st.optional(
st.enums(["mon", "tue", "wed", "thu", "fri", "sat", "sun"])
),
weekday: st.optional(st.union([DayOfWeek, st.array(DayOfWeek)])),
})
);
export const ZoneCondition = st.assign(
+40
View File
@@ -4,6 +4,7 @@ import { ReactNode } from "react";
import {
AutomationDeviceState,
AutomationTime,
DayOfWeek,
} from "types/automations/common";
import { AutomationTriggerZone } from "types/automations/triggers";
@@ -116,6 +117,17 @@ export const getDescriptionFromAutomationNode = <
return getDescriptionForDeviceType(node, namer, true);
case "trigger":
return node.id;
case "time":
const before = node.before
? `before ${convertTimeToString(node.before)}`
: "";
const after = node.after
? `after ${convertTimeToString(node.after)}`
: "";
const weekday = node.weekday
? `on ${getDescriptionForWeekDay(node.weekday, "or")}`
: "";
return [before, after, weekday].filter((x) => x).join(", ");
default:
return JSON.stringify(node);
}
@@ -205,6 +217,34 @@ const getDescriptionForZoneType = (
return out + " " + actionPort;
};
export const getDescriptionForWeekDay = (
n: DayOfWeek | DayOfWeek[],
acc = "and"
): string => {
if (typeof n === "string") {
return n;
} else {
let allWeekends = true;
let allWeekdays = true;
for (const d of n) {
if (!["mon", "tue", "wed", "thu", "fri"].includes(d)) {
allWeekdays = false;
} else {
allWeekends = false;
}
if (!allWeekdays && !allWeekends) {
break;
}
}
if (allWeekends) {
return "weekends";
} else if (allWeekdays) {
return "weekdays";
}
return n.join(` ${acc.trim()} `);
}
};
export const prettyName = (n: string) => {
const clean = n.replaceAll(/[.\-_]/g, " ");
return clean