diff --git a/webapp/src/types/automations/common.ts b/webapp/src/types/automations/common.ts index cd972cd..9a0078e 100644 --- a/webapp/src/types/automations/common.ts +++ b/webapp/src/types/automations/common.ts @@ -6,3 +6,4 @@ export type AutomationTimeObject = st.Infer; export type AutomationTimeString = st.Infer; export type EntityId = st.Infer; export type AutomationDeviceState = st.Infer; +export type DayOfWeek = st.Infer; diff --git a/webapp/src/types/validators/common.ts b/webapp/src/types/validators/common.ts index e99e7fe..aa3d7e2 100644 --- a/webapp/src/types/validators/common.ts +++ b/webapp/src/types/validators/common.ts @@ -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", +]); diff --git a/webapp/src/types/validators/conditions.ts b/webapp/src/types/validators/conditions.ts index d870c2f..6c3dd09 100644 --- a/webapp/src/types/validators/conditions.ts +++ b/webapp/src/types/validators/conditions.ts @@ -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( diff --git a/webapp/src/utils/formatting.tsx b/webapp/src/utils/formatting.tsx index b5f968c..3c48818 100644 --- a/webapp/src/utils/formatting.tsx +++ b/webapp/src/utils/formatting.tsx @@ -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