stop action

This commit is contained in:
ari committed 2022-07-14 15:43:12 -04:00
1 parent 07ab4b9089
commit 8bf25547eb
7 files changed
+96 -39

No files matched your search

@@ -0,0 +1,22 @@
import InputText from "components/Inputs/InputText";
import { StopAction } from "types/automations/actions";
import { OptionManager } from "./OptionManager";
export const ActionStopState: OptionManager<StopAction> = {
defaultState: () => ({
alias: "",
stop: "",
}),
isReady: () => true,
Component: ({ state, setState }) => {
return (
<>
<InputText
label="Stop Reason"
value={state.stop ?? []}
onChange={(d) => setState({ ...state, stop: d })}
/>
</>
);
},
};
@@ -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 = <T extends AutomationNodeTypes>(
nodeType: T,
nodeSubtype: AutomationNodeSubtype<T>,
nodeSubtype: AutomationNodeSubtype<T>
): OptionManager<any> => {
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;
};
+3
View File
@@ -20,6 +20,7 @@ export type WaitAction = st.Infer<typeof v.WaitAction>;
export type DelayAction = st.Infer<typeof v.DelayAction>;
export type FireEventAction = st.Infer<typeof v.FireEventAction>;
export type DeviceAction = st.Infer<typeof v.DeviceAction>;
export type StopAction = st.Infer<typeof v.StopAction>;
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";
+6 -7
View File
@@ -30,11 +30,10 @@ export type AutomationNode<N extends AutomationNodeTypes = any> =
: N extends "condition"
? AutomationCondition
: never;
export type AutomationNodeSubTypeMapping = {
trigger: TriggerType;
action: ActionType;
condition: ConditionType;
};
export type AutomationNodeSubtype<T extends AutomationNodeTypes = any> =
T extends "trigger"
? TriggerType
: T extends "action"
? ActionType
: T extends "condition"
? ConditionType
: never;
AutomationNodeSubTypeMapping[T];
+7
View File
@@ -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({
+14 -6
View File
@@ -39,12 +39,17 @@ export const getNodeType = (node: AutomationNode): AutomationNodeTypes => {
}
};
export const getSubTypeList = <T extends AutomationNodeTypes>(
nodeType: T
): AutomationNodeSubtype<T>[] => {
export const getSubTypeList = <T extends AutomationNodeTypes>(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 = <T extends AutomationNodeTypes>(
"time",
"trigger",
"zone",
] as any;
] as AutomationNodeSubtype<"condition">[];
case "trigger":
return [
"device",
@@ -72,7 +77,7 @@ export const getSubTypeList = <T extends AutomationNodeTypes>(
"tag",
"template",
"webhook",
] as any;
] as AutomationNodeSubtype<"trigger">[];
default:
return [];
}
@@ -125,6 +130,8 @@ export const getNodeSubType = <T extends AutomationNodeTypes>(
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<ActionType, any> = {
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]),
};
+18 -1
View File
@@ -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 (
<div
style={{
display: "flex",
alignItems: "center",
gap: "0.5em",
}}
>
<span>{node.stop}</span>
<StopIcon />
</div>
) as any;
}
return `Stopping due to ${node.stop}`;
}
return "n/a";
};