choose flow

This commit is contained in:
ari committed 2022-04-30 22:51:51 -04:00
1 parent 468a022582
commit 56d68d9065
5 files changed
+338 -58

No files matched your search

+195 -1
View File
@@ -18,7 +18,10 @@ export default {
nodeWidth: dgconst.NODE_WIDTH, nodeWidth: dgconst.NODE_WIDTH,
distanceFactor: dgconst.DISTANCE_FACTOR, distanceFactor: dgconst.DISTANCE_FACTOR,
circleSize: dgconst.CIRCLE_SIZE, circleSize: dgconst.CIRCLE_SIZE,
padding: dgconst.PADDING, padding: {
x: dgconst.PADDING,
y: dgconst.PADDING,
},
} }
} }
} as ComponentMeta<typeof DAGFlow> } as ComponentMeta<typeof DAGFlow>
@@ -120,6 +123,197 @@ Simple.args = {
} }
export const Complex = Template.bind({})
Complex.args = {
...Complex.args,
automation: {
tags: {
"Room": "Bathroom",
"For": "Toliet",
"Type": "Smell",
"Use": "Flushing"
},
metadata: {
id: "random",
alias: "Random",
description: "Example Metadata",
trigger_variables: {
'wowo': '!'
},
mode: 'single',
},
trigger: [
{
"$smType": undefined,
"platform": "numeric_state",
"entity_id": "test",
"alias": "1",
},
{
"$smType": undefined,
"platform": "homeassistant",
"event": "start",
"alias": "2",
}
],
sequence: [
{
"$smType": "condition",
"condition": "and",
"condition_data": {
"conditions": [
{
"$smType": "condition",
"condition": "numeric_state",
"condition_data": {
"entity_id": "sensor.temperature_kitchen",
"below": "15"
}
},
{
"$smType": "condition",
"condition": "template",
"condition_data": {
"value_template": "states(switch.kitchen_light) == \"on\""
}
}
]
}
},
{
"$smType": "action",
"action": "service",
"action_data": {
"alias": "Start Music In Kitchen",
"service": "media_player.play_media",
"target": {
"entity_id": "media_player.kitchen_dot"
},
"data": {
"media_content_id": "Good Morning",
"media_content_type": "SPOTIFY"
}
}
},
{
"$smType": "action",
"action": "choose",
"action_data": {
"choose": [
{
"conditions": [
{
"$smType": "condition",
"condition": "state",
"condition_data": {
"entity_id": "lights.bathroom",
"state": "off",
}
}
],
"sequence": [
{
"$smType": "action",
"action": "service",
"action_data": {
"service": "light.turn_on",
"target": {
"entity_id": "lights.bathroom"
},
"data": {}
}
},
{
"$smType": "action",
"action": "service",
"action_data": {
"alias": "Turn off bedroom",
"service": "light.turn_off",
"target": {
"entity_id": "lights.bedroom"
},
"data": {}
}
}
]
},
{
"conditions": [
{
"$smType": "condition",
"condition": "state",
"condition_data": {
"entity_id": "lights.bedroom",
"state": "off",
}
}
],
"sequence": [
{
"$smType": "action",
"action": "service",
"action_data": {
"alias": "Turn on bedroom",
"service": "light.turn_on",
"target": {
"entity_id": "lights.bedroom"
},
"data": {}
}
},
{
"$smType": "action",
"action": "choose",
"action_data": {
"choose": [
{
"conditions": [],
"sequence": [
{
"$smType": "action",
"action": "choose",
"action_data": {
"choose": [],
"default": [],
}
}
]
}
],
"default": [
{
"$smType": "action",
"action": "choose",
"action_data": {
"choose": [],
"default": [],
}
}
],
}
}
]
}
],
"default": [
{
"$smType": "action",
"action": "service",
"action_data": {
"service": "light.turn_off",
"target": {
"entity_id": "lights.bathroom"
},
"data": {}
}
}
]
}
}
]
}
}
export const EmptyStart = Template.bind({}) export const EmptyStart = Template.bind({})
EmptyStart.args = { EmptyStart.args = {
...EmptyStart.args, ...EmptyStart.args,
+27 -8
View File
@@ -4,8 +4,9 @@ import ReactFlow, { MiniMap, Controls } from 'react-flow-renderer';
import { AutomationData } from "types/automations"; import { AutomationData } from "types/automations";
import { triggerToFlow } from "./triggerConvertor"; import { triggerToFlow } from "./triggerConvertor";
import { DAGNode } from "./DAGNode"; import { DAGNode } from "./DAGNode";
import { DAGFlowDims, TriggerMakerOptions } from "./types"; import { DAGFlowDims, TriggerMakerOptions, FlowData } from './types';
import { DAGCircle } from './DAGCircle'; import { DAGCircle } from './DAGCircle';
import { sequenceToFlow } from "./sequenceConvertor";
const nodeTypes = { const nodeTypes = {
'dagnode': DAGNode, 'dagnode': DAGNode,
@@ -25,22 +26,40 @@ export const DAGFlow: FC<Props> = ({
opts, opts,
dims, dims,
}) => { }) => {
const { const condPoint = makeConditionPoint(dims);
nodes, const flowData: FlowData = {
edges, nodes: [condPoint],
} = triggerToFlow(automation.trigger, dims, opts.triggers); edges: [],
}
triggerToFlow(flowData, automation.trigger, condPoint.id, dims, opts.triggers);
sequenceToFlow(flowData, automation.sequence, condPoint.id, dims);
return ( return (
<ReactFlow <ReactFlow
nodeTypes={nodeTypes} nodeTypes={nodeTypes}
nodes={nodes}
edges={edges}
snapToGrid snapToGrid
onlyRenderVisibleElements onlyRenderVisibleElements
nodesConnectable={false} nodesConnectable={false}
nodesDraggable={false} nodesDraggable={false}
{...flowData}
> >
<MiniMap /> <MiniMap />
<Controls /> <Controls />
</ReactFlow> </ReactFlow>
); );
} }
const makeConditionPoint = ({
padding,
nodeWidth,
nodeHeight,
circleSize,
}: DAGFlowDims) => ({
id: `c`,
type: 'dagcircle',
position: { x: padding.x + nodeWidth * 2, y: padding.y + nodeHeight * 0.25 },
data: {
size: circleSize,
backgroundColor: 'blue',
}
})
@@ -0,0 +1,98 @@
import { AutomationSequenceNode } from 'types/automations';
import { DAGFlowDims, FlowData } from './types';
import { getDescriptionFromAutomationNode } from 'utils/formatting';
import { XYPosition } from 'react-flow-renderer';
import { ChooseAction } from 'types/automations/actions';
export const sequenceToFlow = (
flowData: FlowData,
sequence: AutomationSequenceNode[],
fromPoint: string,
dims: DAGFlowDims,
prefix: string = "",
) => {
let lastPointId = fromPoint;
for (let i = 0; i < sequence.length; i++) {
const node = sequence[i];
const nodeId = `${prefix}n-${i}`;
const position = computeNodePos(i, dims);
if (node.$smType === 'action') {
if (node.action === 'choose') {
addChooseNode(flowData, node, i, nodeId, lastPointId, dims)
} else {
addSingleNode(flowData, node, position, nodeId, dims);
}
} else {
addSingleNode(flowData, node, position, nodeId, dims);
}
flowData.edges.push({
id: `${lastPointId}-${nodeId}`,
source: lastPointId,
target: nodeId,
});
lastPointId = nodeId;
}
}
const computeNodePos = (i: number, { padding, distanceFactor, nodeWidth }: DAGFlowDims): XYPosition => ({
x: padding.x + nodeWidth * distanceFactor * (i + 2),
y: padding.y
})
const addSingleNode = (
flowData: FlowData,
node: AutomationSequenceNode,
position: XYPosition,
nodeId: string,
{
nodeHeight,
nodeWidth,
}: DAGFlowDims,
extra: Record<string, any> = {},
) => flowData.nodes.push({
id: nodeId,
type: 'dagnode',
data: {
label: getDescriptionFromAutomationNode(node),
height: nodeHeight,
width: nodeWidth,
color: node.$smType === 'action' ? 'green' : 'blue',
// onEdit: () => opts.onEdit(i),
// onXClick: () => opts.onDelete(i, flowId),
hasInput: true,
},
position,
...extra
});
const addChooseNode = (
flowData: FlowData,
node: ChooseAction,
i: number,
nodeId: string,
lastPointId: string,
dims: DAGFlowDims,
) => {
const { x, y } = computeNodePos(i, dims);
addSingleNode(flowData, node, { x, y }, nodeId, dims);
node.action_data.choose.forEach(({ sequence, conditions }, j) => {
sequenceToFlow(flowData, sequence, nodeId, {
...dims,
padding: {
x: x - dims.nodeWidth,
y: y + dims.nodeHeight * (j + 0.25) * dims.distanceFactor,
},
}, `${nodeId}.${j}.`)
})
const totalChildren = node.action_data.choose.length;
sequenceToFlow(flowData, node.action_data.default, nodeId, {
...dims,
padding: {
x: x - dims.nodeWidth,
y: y + dims.nodeHeight * (totalChildren + 0.25) * dims.distanceFactor,
},
}, `${nodeId}.${totalChildren}.`)
}
@@ -1,19 +1,11 @@
import { Edge, Node } from "react-flow-renderer";
import { AutomationTrigger } from 'types/automations/triggers'; import { AutomationTrigger } from 'types/automations/triggers';
import { DAGFlowDims, FlowData, TriggerMakerOptions, DAGFlowNode } from './types'; import { DAGFlowDims, FlowData, TriggerMakerOptions } from './types';
// export const makeTriggerMakerOptions = (
// triggers: AutomationTrigger[],
// onUpdate: (t: AutomationTrigger[]) => void,
// ): TriggerMakerOptions => ({
// onAdd: () => onUpdate([...triggers, {
// alias: "",
// }])
// })
export const triggerToFlow = ( export const triggerToFlow = (
flowData: FlowData,
trigger: AutomationTrigger[], trigger: AutomationTrigger[],
toPoint: string,
{ {
distanceFactor, distanceFactor,
nodeHeight, nodeHeight,
@@ -22,27 +14,11 @@ export const triggerToFlow = (
padding, padding,
}: DAGFlowDims, }: DAGFlowDims,
opts: TriggerMakerOptions, opts: TriggerMakerOptions,
): FlowData => { ) => {
// outputs
const nodes: DAGFlowNode[] = [],
edges: Edge[] = [];
// target node
const condPos = { x: padding + nodeWidth * 2, y: padding + nodeHeight * 0.25 };
nodes.push({
id: `c`,
type: 'dagcircle',
position: condPos,
data: {
size: circleSize,
backgroundColor: 'blue',
}
})
// convert all triggers to dag nodes // convert all triggers to dag nodes
trigger.forEach((t, i) => { trigger.forEach((t, i) => {
const flowId = `t-${i}`; const flowId = `t-${i}`;
nodes.push({ flowData.nodes.push({
id: flowId, id: flowId,
type: 'dagnode', type: 'dagnode',
data: { data: {
@@ -51,24 +27,24 @@ export const triggerToFlow = (
width: nodeWidth, width: nodeWidth,
color: 'red', color: 'red',
onEdit: () => opts.onEdit(i), onEdit: () => opts.onEdit(i),
onXClick: () => opts.onDelete(i, flowId), onXClick: () => opts.onDelete(i),
}, },
position: { x: padding, y: padding + nodeHeight * distanceFactor * i } position: { x: padding.x, y: padding.y + nodeHeight * distanceFactor * i }
}) })
edges.push({ flowData.edges.push({
id: `e(${flowId})-(c)`, id: `e(${flowId})-(${toPoint})`,
source: flowId, source: flowId,
target: 'c', target: toPoint,
}) })
}); });
// create a 'add button' // create a 'add button'
nodes.push({ flowData.nodes.push({
id: `trigger-add`, id: `trigger-add`,
type: 'dagcircle', type: 'dagcircle',
position: { position: {
x: padding + nodeWidth * 0.4, x: padding.x + nodeWidth * 0.4,
y: padding + nodeHeight * distanceFactor * (nodes.length - 1), y: padding.y + nodeHeight * distanceFactor * trigger.length,
}, },
data: { data: {
size: circleSize, size: circleSize,
@@ -78,10 +54,4 @@ export const triggerToFlow = (
disableTarget: true, disableTarget: true,
} }
}) })
return {
nodes,
edges
}
} }
+5 -6
View File
@@ -1,11 +1,10 @@
import { Edge, Node } from 'react-flow-renderer'; import { Edge, Node, XYPosition } from 'react-flow-renderer';
import { AutomationTrigger } from 'types/automations/triggers';
import { DAGCircleProps } from './DAGCircle'; import { DAGCircleProps } from './DAGCircle';
import { DAGNodeProps } from './DAGNode'; import { DAGNodeProps } from './DAGNode';
export type DAGFlowDims = { export type DAGFlowDims = {
padding: number; padding: XYPosition;
nodeHeight: number; nodeHeight: number;
nodeWidth: number; nodeWidth: number;
// addHeight: number; // addHeight: number;
@@ -14,8 +13,9 @@ export type DAGFlowDims = {
distanceFactor: number; distanceFactor: number;
} }
export type DAGFlowNode = Node<DAGNodeProps | DAGCircleProps | any>;
export type FlowData = { export type FlowData = {
nodes: Node[], nodes: DAGFlowNode[],
edges: Edge[], edges: Edge[],
} }
@@ -23,5 +23,4 @@ export type TriggerMakerOptions = {
onAdd: () => void, onAdd: () => void,
onEdit: (i: number) => void, onEdit: (i: number) => void,
onDelete: (i: number) => void, onDelete: (i: number) => void,
} }
export type DAGFlowNode = Node<DAGNodeProps | DAGCircleProps>;