diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionStop.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionStop.tsx new file mode 100644 index 0000000..c7799a6 --- /dev/null +++ b/webapp/src/components/NodeEditor/OptionManager/ActionStop.tsx @@ -0,0 +1,22 @@ +import InputText from "components/Inputs/InputText"; +import { StopAction } from "types/automations/actions"; +import { OptionManager } from "./OptionManager"; + +export const ActionStopState: OptionManager = { + defaultState: () => ({ + alias: "", + stop: "", + }), + isReady: () => true, + Component: ({ state, setState }) => { + return ( + <> + setState({ ...state, stop: d })} + /> + + ); + }, +}; diff --git a/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts index 4d97a4b..57b22bc 100644 --- a/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts @@ -1,4 +1,4 @@ -import { AutomationNodeTypes, AutomationNodeSubtype } from "types/automations" +import { AutomationNodeTypes, AutomationNodeSubtype } from "types/automations"; import { ActionChooseState } from "./ActionChoose"; import { ActionDeviceState } from "./ActionDevice"; import { ActionCallServiceState } from "./ActionService"; @@ -19,59 +19,60 @@ import { TriggerTimePattern } from "./TriggerTimePattern"; import { TriggerWebhook } from "./TriggerWebhook"; import { TriggerZone } from "./TriggerZone"; import { TriggerDevice } from "./TriggerDevice"; - - +import { ActionStopState } from "./ActionStop"; export const getOptionManager = ( nodeType: T, - nodeSubtype: AutomationNodeSubtype, + nodeSubtype: AutomationNodeSubtype ): OptionManager => { - if (nodeType === 'action') { + if (nodeType === "action") { switch (nodeSubtype) { case "choose": return ActionChooseState; case "device": return ActionDeviceState; - case 'event': + case "event": return ActionEventState; case "repeat": return ActionRepeatState; - case 'service': + case "service": return ActionCallServiceState; case "wait": return ActionWaitState; + case "stop": + return ActionStopState; case "delay": return ActionWaitState; } } - if (nodeType === 'condition') { - return ConditionLogicState + if (nodeType === "condition") { + return ConditionLogicState; } if (nodeType === "trigger") { switch (nodeSubtype) { case "event": - return TriggerEventState + return TriggerEventState; case "homeassistant": - return TriggerHAState + return TriggerHAState; case "mqtt": - return TriggerMQTTState + return TriggerMQTTState; case "numeric_state": - return TriggerNumericState + return TriggerNumericState; case "state": - return TriggerState + return TriggerState; case "template": - return TriggerTemplate - case 'time': - return TriggerTime - case 'time_pattern': - return TriggerTimePattern - case 'webhook': - return TriggerWebhook + return TriggerTemplate; + case "time": + return TriggerTime; + case "time_pattern": + return TriggerTimePattern; + case "webhook": + return TriggerWebhook; case "zone": - return TriggerZone + return TriggerZone; case "device": - return TriggerDevice + return TriggerDevice; } } - return Generic -} + return Generic; +}; diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts index 61c0959..2c3ee0a 100644 --- a/webapp/src/types/automations/actions.ts +++ b/webapp/src/types/automations/actions.ts @@ -20,6 +20,7 @@ export type WaitAction = st.Infer; export type DelayAction = st.Infer; export type FireEventAction = st.Infer; export type DeviceAction = st.Infer; +export type StopAction = st.Infer; export type ChooseAction = AutomationActionNodeBase<{ choose: Array<{ conditions: AutomationCondition[]; @@ -34,6 +35,7 @@ export type AutomationAction = | DelayAction | FireEventAction | DeviceAction + | StopAction | ChooseAction; export type ActionType = | "service" @@ -41,4 +43,5 @@ export type ActionType = | "wait" | "event" | "device" + | "stop" | "choose"; diff --git a/webapp/src/types/automations/index.ts b/webapp/src/types/automations/index.ts index 29f65f2..c121328 100644 --- a/webapp/src/types/automations/index.ts +++ b/webapp/src/types/automations/index.ts @@ -30,11 +30,10 @@ export type AutomationNode = : N extends "condition" ? AutomationCondition : never; +export type AutomationNodeSubTypeMapping = { + trigger: TriggerType; + action: ActionType; + condition: ConditionType; +}; export type AutomationNodeSubtype = - T extends "trigger" - ? TriggerType - : T extends "action" - ? ActionType - : T extends "condition" - ? ConditionType - : never; + AutomationNodeSubTypeMapping[T]; diff --git a/webapp/src/types/validators/actions.ts b/webapp/src/types/validators/actions.ts index 09deeab..d57db88 100644 --- a/webapp/src/types/validators/actions.ts +++ b/webapp/src/types/validators/actions.ts @@ -63,6 +63,13 @@ export const DeviceAction = st.assign( st.partial(AutomationDeviceState) ); +export const StopAction = st.assign( + AutomationActionNodeBase, + st.type({ + stop: st.string(), + }) +); + export const ChooseAction = st.intersection([ AutomationActionNodeBase, st.type({ diff --git a/webapp/src/utils/automations.ts b/webapp/src/utils/automations.ts index 6d6c770..113bc96 100644 --- a/webapp/src/utils/automations.ts +++ b/webapp/src/utils/automations.ts @@ -39,12 +39,17 @@ export const getNodeType = (node: AutomationNode): AutomationNodeTypes => { } }; -export const getSubTypeList = ( - nodeType: T -): AutomationNodeSubtype[] => { +export const getSubTypeList = (nodeType: T) => { switch (nodeType) { case "action": - return ["service", "repeat", "wait", "device", "choose"] as any; + return [ + "service", + "repeat", + "wait", + "device", + "stop", + "choose", + ] as AutomationNodeSubtype<"action">[]; case "condition": return [ "or", @@ -57,7 +62,7 @@ export const getSubTypeList = ( "time", "trigger", "zone", - ] as any; + ] as AutomationNodeSubtype<"condition">[]; case "trigger": return [ "device", @@ -72,7 +77,7 @@ export const getSubTypeList = ( "tag", "template", "webhook", - ] as any; + ] as AutomationNodeSubtype<"trigger">[]; default: return []; } @@ -125,6 +130,8 @@ export const getNodeSubType = ( return "choose" as any; } else if ("delay" in node) { return "wait" as any; + } else if ("stop" in node) { + return "stop" as any; } else { throw new Error("Invalid node type!"); } @@ -151,6 +158,7 @@ export const ActionValidators: Record = { event: v.actions.FireEventAction, repeat: v.actions.RepeatAction, service: v.actions.ServiceAction, + stop: v.actions.StopAction, wait: st.union([v.actions.DelayAction, v.actions.WaitAction]), }; diff --git a/webapp/src/utils/formatting.tsx b/webapp/src/utils/formatting.tsx index e09f369..5544803 100644 --- a/webapp/src/utils/formatting.tsx +++ b/webapp/src/utils/formatting.tsx @@ -7,7 +7,7 @@ import { DayOfWeek, } from "types/automations/common"; import { AutomationTriggerZone } from "types/automations/triggers"; - +import StopIcon from "@mui/icons-material/PanToolOutlined"; export interface Namer { getEntityName(entity_id: string | string[], maxEntities?: number): string; getDeviceName(device_id: string): string; @@ -171,6 +171,23 @@ export const getDescriptionFromAutomationNode = < } return "Switch (choose)"; } + if ("stop" in node) { + if (allowJs) { + return ( +
+ {node.stop} + +
+ ) as any; + } + return `Stopping due to ${node.stop}`; + } return "n/a"; };