better support for repeats
This commit is contained in:
1 parent
9dd23fd8e7
commit
bbe6a5abf3
11 files changed
+223
-142
No files matched your search
@@ -409,7 +409,6 @@ RepeatAuto.args = {
|
||||
},
|
||||
{
|
||||
repeat: {
|
||||
count: 2,
|
||||
until: [
|
||||
{
|
||||
condition: "state",
|
||||
@@ -425,6 +424,17 @@ RepeatAuto.args = {
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
repeat: {
|
||||
count: 2,
|
||||
sequence: [
|
||||
{
|
||||
entity_id: "button.ping_mainboard",
|
||||
service: "button.pres",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
@@ -148,7 +148,6 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
{
|
||||
color: getNodeType(node) === "action" ? "green" : "blue",
|
||||
enabled: node.enabled ?? true,
|
||||
hasInput: true,
|
||||
label: getDescriptionFromAutomationNode(node, namer, true),
|
||||
...state.getIsClosedActions(nodeId, node),
|
||||
...stateUpdater.createNodeActions("sequence", nodeIndex, {
|
||||
@@ -403,27 +402,37 @@ export const makeRepeatNodes = makeSpecialNodeMaker(
|
||||
args.nodeIndex
|
||||
);
|
||||
|
||||
let whileState: DAGElementsOutputState | null = null;
|
||||
let untilState: DAGElementsOutputState | null = null;
|
||||
|
||||
// while
|
||||
const whileState = makeConditionNodes(
|
||||
convertScriptConditionFieldToAutomationConditions(node.repeat.while),
|
||||
{
|
||||
...args,
|
||||
stateUpdater: stateUpdater.while,
|
||||
position: offsetPos,
|
||||
lastNodeId,
|
||||
nodeIndex: 0,
|
||||
nodeId: `${args.nodeId}-repeat-while`,
|
||||
}
|
||||
);
|
||||
whileState.data.edges[0].sourceHandle = `side`;
|
||||
whileState.data.edges[0].label = `While`;
|
||||
outputState.extend(whileState);
|
||||
offsetPos = distance.moveFromTo("collection", "node", offsetPos, args.dims);
|
||||
lastNodeId = whileState.lastNodeId;
|
||||
if ("while" in node.repeat) {
|
||||
whileState = makeConditionNodes(
|
||||
convertScriptConditionFieldToAutomationConditions(node.repeat.while),
|
||||
{
|
||||
...args,
|
||||
stateUpdater: stateUpdater,
|
||||
position: offsetPos,
|
||||
lastNodeId,
|
||||
nodeIndex: 0,
|
||||
nodeId: `${args.nodeId}-repeat-while`,
|
||||
}
|
||||
);
|
||||
whileState.data.edges[0].sourceHandle = `side`;
|
||||
whileState.data.edges[0].label = `While`;
|
||||
outputState.extend(whileState);
|
||||
offsetPos = distance.moveFromTo(
|
||||
"collection",
|
||||
"node",
|
||||
offsetPos,
|
||||
args.dims
|
||||
);
|
||||
lastNodeId = whileState.lastNodeId;
|
||||
}
|
||||
// sequence
|
||||
const sequenceState = makeSequenceNodes(node.repeat.sequence, {
|
||||
...args,
|
||||
stateUpdater: stateUpdater.sequence,
|
||||
stateUpdater: stateUpdater,
|
||||
position: offsetPos,
|
||||
lastNodeId: undefined,
|
||||
nodeIndex: 0,
|
||||
@@ -432,7 +441,12 @@ export const makeRepeatNodes = makeSpecialNodeMaker(
|
||||
if (lastNodeId && sequenceState.data.nodes.length > 0) {
|
||||
outputState.addEdge(lastNodeId, sequenceState.data.nodes[0].id, {
|
||||
animated: true,
|
||||
label: "do",
|
||||
label:
|
||||
"count" in node.repeat
|
||||
? `repeat ${node.repeat.count} time${
|
||||
node.repeat.count !== 1 ? "s" : ""
|
||||
}`
|
||||
: "do",
|
||||
});
|
||||
}
|
||||
lastNodeId = sequenceState.lastNodeId;
|
||||
@@ -443,28 +457,41 @@ export const makeRepeatNodes = makeSpecialNodeMaker(
|
||||
"node"
|
||||
);
|
||||
// until
|
||||
const untilState = makeConditionNodes(
|
||||
convertScriptConditionFieldToAutomationConditions(node.repeat.until),
|
||||
{
|
||||
...args,
|
||||
stateUpdater: stateUpdater.until,
|
||||
position: offsetPos,
|
||||
lastNodeId,
|
||||
nodeIndex: 0,
|
||||
nodeId: `${args.nodeId}-repeat-until`,
|
||||
}
|
||||
);
|
||||
untilState.data.edges[0].label = `Until`;
|
||||
untilState.data.edges[0].targetHandle = `side`;
|
||||
outputState.extend(untilState);
|
||||
offsetPos = distance.moveFromTo("collection", "node", offsetPos, args.dims);
|
||||
lastNodeId = untilState.lastNodeId;
|
||||
if ("until" in node.repeat) {
|
||||
untilState = makeConditionNodes(
|
||||
convertScriptConditionFieldToAutomationConditions(node.repeat.until),
|
||||
{
|
||||
...args,
|
||||
stateUpdater: stateUpdater,
|
||||
position: offsetPos,
|
||||
lastNodeId,
|
||||
nodeIndex: 0,
|
||||
nodeId: `${args.nodeId}-repeat-until`,
|
||||
}
|
||||
);
|
||||
untilState.data.edges[0].label = `Until`;
|
||||
untilState.data.edges[0].targetHandle = `side`;
|
||||
outputState.extend(untilState);
|
||||
offsetPos = distance.moveFromTo(
|
||||
"collection",
|
||||
"node",
|
||||
offsetPos,
|
||||
args.dims
|
||||
);
|
||||
lastNodeId = untilState.lastNodeId;
|
||||
}
|
||||
// loop
|
||||
if (untilState.lastNodeId && whileState.lastNodeId) {
|
||||
outputState.addEdge(untilState.lastNodeId, whileState.lastNodeId, {
|
||||
const firstNodeId =
|
||||
whileState === null
|
||||
? sequenceState.data.nodes[0].id
|
||||
: whileState.lastNodeId;
|
||||
const finalNodeId =
|
||||
untilState === null ? sequenceState.lastNodeId : untilState.lastNodeId;
|
||||
if (finalNodeId && firstNodeId) {
|
||||
outputState.addEdge(finalNodeId, firstNodeId, {
|
||||
animated: true,
|
||||
label: "repeat",
|
||||
sourceHandle: "head",
|
||||
sourceHandle: untilState === null ? "default" : "head",
|
||||
targetHandle: "return",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,7 +16,11 @@ export const ButtonNode: FC<ButtonNodeProps> = ({
|
||||
}) => {
|
||||
const inner = (
|
||||
<>
|
||||
<Handle type="target" position={flipped ? Position.Top : Position.Left} />
|
||||
<Handle
|
||||
id="default"
|
||||
type="target"
|
||||
position={flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
{icon}
|
||||
<Handle
|
||||
id="default"
|
||||
|
||||
@@ -3,20 +3,24 @@ import { Handle, Position } from "react-flow-renderer";
|
||||
import { SequenceNodeProps } from "./types";
|
||||
import { SequenceNodeElement } from "./SequenceNodeElement";
|
||||
|
||||
export const SequenceNode: FC<SequenceNodeProps> = ({
|
||||
hasInput = false,
|
||||
...props
|
||||
}) => {
|
||||
export const SequenceNode: FC<SequenceNodeProps> = (props) => {
|
||||
return (
|
||||
<SequenceNodeElement {...props}>
|
||||
{hasInput && (
|
||||
<>
|
||||
<Handle
|
||||
type="target"
|
||||
position={props.flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
<>
|
||||
<Handle
|
||||
id="default"
|
||||
key="target-default"
|
||||
type="target"
|
||||
position={props.flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
|
||||
<Handle
|
||||
id="return"
|
||||
key="target-return"
|
||||
type="target"
|
||||
position={props.flipped ? Position.Right : Position.Bottom}
|
||||
/>
|
||||
</>
|
||||
<Handle
|
||||
type="source"
|
||||
position={props.flipped ? Position.Bottom : Position.Right}
|
||||
|
||||
@@ -28,9 +28,7 @@ export type SequenceNodeElementProps = SequenceNodeBaseProps & {
|
||||
width: number;
|
||||
flipped: boolean;
|
||||
};
|
||||
export type SequenceNodeDataProps = SequenceNodeBaseProps & {
|
||||
hasInput?: boolean;
|
||||
};
|
||||
export type SequenceNodeDataProps = SequenceNodeBaseProps & {};
|
||||
export interface SequenceNodeProps extends SequenceNodeDataProps {
|
||||
height: number;
|
||||
width: number;
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
} from "types/automations/actions";
|
||||
import { ScriptConditionField } from "types/automations/common";
|
||||
import { convertScriptConditionFieldToAutomationConditions } from "utils/automations";
|
||||
import { AutomationNode } from "types/automations";
|
||||
|
||||
export const createBasicUpdater =
|
||||
<K extends keyof AutomationActionData, N extends AutomationActionData[K][0]>(
|
||||
@@ -260,28 +261,23 @@ export const createUpdater = (
|
||||
},
|
||||
createRepeatNodeUpdater(i: number) {
|
||||
const { repeat, ...rest } = args.sequence.data[i] as RepeatAction;
|
||||
const sequence = {
|
||||
data: repeat.sequence,
|
||||
onUpdate: (upd: AutomationNode[]) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
...rest,
|
||||
repeat: {
|
||||
...repeat,
|
||||
sequence: upd,
|
||||
},
|
||||
},
|
||||
i
|
||||
),
|
||||
};
|
||||
|
||||
return {
|
||||
sequence: createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: createDummyUpdater(),
|
||||
sequence: {
|
||||
data: repeat.sequence,
|
||||
onUpdate: (upd) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
...rest,
|
||||
repeat: {
|
||||
...repeat,
|
||||
sequence: upd,
|
||||
},
|
||||
},
|
||||
i
|
||||
),
|
||||
},
|
||||
trigger: createDummyUpdater(),
|
||||
}),
|
||||
while: createUpdater({
|
||||
if ("while" in repeat) {
|
||||
return createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: {
|
||||
data: convertScriptConditionFieldToAutomationConditions(
|
||||
@@ -299,10 +295,13 @@ export const createUpdater = (
|
||||
i
|
||||
),
|
||||
},
|
||||
sequence: createDummyUpdater(),
|
||||
trigger: createDummyUpdater(),
|
||||
}),
|
||||
until: createUpdater({
|
||||
sequence,
|
||||
});
|
||||
}
|
||||
|
||||
if ("until" in repeat) {
|
||||
return createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: {
|
||||
data: convertScriptConditionFieldToAutomationConditions(
|
||||
@@ -320,10 +319,17 @@ export const createUpdater = (
|
||||
i
|
||||
),
|
||||
},
|
||||
sequence: createDummyUpdater(),
|
||||
trigger: createDummyUpdater(),
|
||||
}),
|
||||
};
|
||||
sequence,
|
||||
});
|
||||
}
|
||||
|
||||
return createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: createDummyUpdater(),
|
||||
trigger: createDummyUpdater(),
|
||||
sequence,
|
||||
});
|
||||
},
|
||||
createParallelNodeUpdater(i: number, j: number) {
|
||||
const { parallel, ...rest } = args.sequence.data[i] as ParallelAction;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
import { ChooseAction } from "types/automations/actions";
|
||||
import { OptionManager, updateActionData } from "./OptionManager";
|
||||
import { OptionManager } from "./OptionManager";
|
||||
|
||||
export const ActionChooseState: OptionManager<ChooseAction> = {
|
||||
defaultState: () => ({
|
||||
@@ -9,20 +8,7 @@ export const ActionChooseState: OptionManager<ChooseAction> = {
|
||||
choose: [],
|
||||
}),
|
||||
isReady: () => true,
|
||||
Component: ({ state, setState }) => {
|
||||
return (
|
||||
<>
|
||||
<InputYaml
|
||||
label="Options"
|
||||
value={state.choose ?? []}
|
||||
onChange={(choose) => setState({ ...state, choose })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Else"
|
||||
value={state.default ?? []}
|
||||
onChange={(d) => setState({ ...state, default: d })}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
Component: ({}) => {
|
||||
return <></>;
|
||||
},
|
||||
};
|
||||
@@ -1,7 +1,12 @@
|
||||
import InputNumber from "components/Inputs/InputNumber";
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
import { useState } from "react";
|
||||
import { RepeatAction } from "types/automations/actions";
|
||||
import { OptionManager, updateActionData } from "./OptionManager";
|
||||
import { InputAutoComplete } from "components/Inputs/InputAutoComplete";
|
||||
|
||||
const determineType = (repeatNode: RepeatAction["repeat"]) =>
|
||||
"count" in repeatNode ? "count" : "until" in repeatNode ? "until" : "while";
|
||||
|
||||
export const ActionRepeatState: OptionManager<RepeatAction> = {
|
||||
defaultState: () => ({
|
||||
@@ -12,36 +17,57 @@ export const ActionRepeatState: OptionManager<RepeatAction> = {
|
||||
},
|
||||
}),
|
||||
isReady: ({}) => true,
|
||||
renderOptionList: (state, setState) => {
|
||||
const update = updateActionData(state, setState);
|
||||
Component: ({ state, setState }) => {
|
||||
const repeatType = determineType(state.repeat);
|
||||
return (
|
||||
<>
|
||||
<InputNumber
|
||||
label="How many times?"
|
||||
value={state.repeat.count}
|
||||
onChange={(count = 1) =>
|
||||
update({ repeat: { ...state.repeat, count } })
|
||||
}
|
||||
/>
|
||||
<InputYaml
|
||||
label="While"
|
||||
value={state.repeat.while ?? []}
|
||||
onChange={(sequence) =>
|
||||
update({ repeat: { ...state.repeat, while: sequence } })
|
||||
}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Sequence"
|
||||
value={state.repeat.sequence ?? []}
|
||||
onChange={(sequence) =>
|
||||
update({ repeat: { ...state.repeat, sequence } })
|
||||
}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Until"
|
||||
value={state.repeat.until ?? []}
|
||||
onChange={(until) => update({ repeat: { ...state.repeat, until } })}
|
||||
<InputAutoComplete
|
||||
label="Type"
|
||||
value={repeatType}
|
||||
options={["while", "until", "count"]}
|
||||
onChange={(v) => {
|
||||
if (v === "while") {
|
||||
setState({
|
||||
...state,
|
||||
repeat: {
|
||||
sequence: state.repeat.sequence,
|
||||
while: "until" in state.repeat ? state.repeat.until : [],
|
||||
},
|
||||
});
|
||||
} else if (v === "until") {
|
||||
setState({
|
||||
...state,
|
||||
repeat: {
|
||||
sequence: state.repeat.sequence,
|
||||
until: "while" in state.repeat ? state.repeat.while : [],
|
||||
},
|
||||
});
|
||||
} else {
|
||||
setState({
|
||||
...state,
|
||||
repeat: {
|
||||
sequence: state.repeat.sequence,
|
||||
count: 1,
|
||||
},
|
||||
});
|
||||
}
|
||||
}}
|
||||
/>
|
||||
{"count" in state.repeat && (
|
||||
<InputNumber
|
||||
label="How many times?"
|
||||
value={state.repeat.count}
|
||||
onChange={(count = 1) =>
|
||||
setState({
|
||||
...state,
|
||||
repeat: {
|
||||
sequence: state.repeat.sequence,
|
||||
count,
|
||||
},
|
||||
})
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
},
|
||||
|
||||
@@ -9,12 +9,19 @@ type AutomationActionNodeBase<Data extends Object> = Data &
|
||||
|
||||
export type ServiceAction = st.Infer<typeof v.ServiceAction>;
|
||||
export type RepeatAction = AutomationActionNodeBase<{
|
||||
repeat: {
|
||||
count?: number;
|
||||
while?: ScriptConditionField;
|
||||
until?: ScriptConditionField;
|
||||
sequence: AutomationAction[];
|
||||
};
|
||||
repeat:
|
||||
| {
|
||||
sequence: AutomationAction[];
|
||||
count: number;
|
||||
}
|
||||
| {
|
||||
sequence: AutomationAction[];
|
||||
until: ScriptConditionField;
|
||||
}
|
||||
| {
|
||||
sequence: AutomationAction[];
|
||||
while: ScriptConditionField;
|
||||
};
|
||||
}>;
|
||||
export type WaitAction = st.Infer<typeof v.WaitAction>;
|
||||
export type DelayAction = st.Infer<typeof v.DelayAction>;
|
||||
|
||||
@@ -25,12 +25,20 @@ export const ServiceAction = st.intersection([
|
||||
export const RepeatAction = st.intersection([
|
||||
AutomationActionNodeBase,
|
||||
st.type({
|
||||
repeat: st.object({
|
||||
count: st.optional(st.number()),
|
||||
while: st.optional(ScriptConditionField),
|
||||
until: st.optional(ScriptConditionField),
|
||||
sequence: st.array(st.object()),
|
||||
}),
|
||||
repeat: st.union([
|
||||
st.object({
|
||||
count: st.optional(st.number()),
|
||||
sequence: st.array(st.object()),
|
||||
}),
|
||||
st.object({
|
||||
until: st.optional(ScriptConditionField),
|
||||
sequence: st.array(st.object()),
|
||||
}),
|
||||
st.object({
|
||||
while: st.optional(ScriptConditionField),
|
||||
sequence: st.array(st.object()),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
]);
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from "types/automations/common";
|
||||
import { AutomationTriggerZone } from "types/automations/triggers";
|
||||
import StopIcon from "@mui/icons-material/PanToolOutlined";
|
||||
import { RepeatAction } from "types/automations/actions";
|
||||
export interface Namer {
|
||||
getEntityName(entity_id: string | string[], maxEntities?: number): string;
|
||||
getDeviceName(device_id: string): string;
|
||||
@@ -137,10 +138,14 @@ export const getDescriptionFromAutomationNode = <
|
||||
return namer.getServiceName(node.service);
|
||||
}
|
||||
if ("repeat" in node) {
|
||||
return (
|
||||
"Repeat " +
|
||||
(Number.isInteger(node.repeat.count) ? node.repeat.count : "indefintely")
|
||||
);
|
||||
const repeatNode = node.repeat;
|
||||
if ("count" in repeatNode) {
|
||||
return `Repeat ${repeatNode.count}`;
|
||||
}
|
||||
if ("while" in repeatNode) {
|
||||
return `Repeat While`;
|
||||
}
|
||||
return "Repeat Until";
|
||||
}
|
||||
if ("delay" in node) {
|
||||
return `Wait for ${convertTimeToString(node.delay)}`;
|
||||
|
||||
Reference in new issue
Block a user