diff --git a/web/src/components/AutomationEditor/SequenceNodes.tsx b/web/src/components/AutomationEditor/SequenceNodes.tsx new file mode 100644 index 0000000..5bdf225 --- /dev/null +++ b/web/src/components/AutomationEditor/SequenceNodes.tsx @@ -0,0 +1,60 @@ +import { FC } from "react"; +import { AutomationSequenceNode } from "~/automations/types"; +import { getDescriptionFromAutomationNode } from "~/automations/utils"; +import DAGEdge from "./DAGEdge"; +import DAGNode from "./DAGNode"; +import { useNodeStyles } from "./styles"; +import { Point } from "./types"; + + + +export const SequenceNodes: FC<{ + styles: ReturnType, + sequence: AutomationSequenceNode[], + onUpdate: (s: AutomationSequenceNode[]) => void, + nodeWidth: number, + nodeHeight: number, + triggerPoint: Point, + nHDF: number, +}> = ({ + styles: { classes, theme }, + sequence, + onUpdate, + nodeWidth, + nodeHeight, + triggerPoint, + nHDF, +}) => { + const renderSequence = (node: AutomationSequenceNode, i: number) => { + const nodeLoc: Point = [triggerPoint[0] + nodeWidth/2 , 0]; + let lastNodeLoc = triggerPoint; + if (i > 0) { + lastNodeLoc = [nodeLoc[0] + nodeWidth*((i - 1)*nHDF+1), nodeLoc[1] + nodeHeight/2]; + nodeLoc[0] += nodeWidth*i*nHDF + } + return <> + { + onUpdate([ + ...sequence.slice(0, i), + ...sequence.slice(i+1), + ]) + }} + /> + + + } + return + {sequence.map(renderSequence)} + +} \ No newline at end of file diff --git a/web/src/components/AutomationEditor/index.stories.tsx b/web/src/components/AutomationEditor/index.stories.tsx index 6dea0aa..9741274 100644 --- a/web/src/components/AutomationEditor/index.stories.tsx +++ b/web/src/components/AutomationEditor/index.stories.tsx @@ -87,4 +87,115 @@ ServiceAction.args = { } ], } as AutomationData +} + + +export const ChooseAction = Template.bind({}) +ChooseAction.args = { + automation: { + metadata: { + id: "X12314akx", + alias: "root", + description: 'Really Long Description', + mode: 'single', + }, + trigger: [ + { + platform: 'time', + at: '10:00:00' + }, + { + platform: "device", + device_id: "34749", + domain: "zha", + type: "remote_button_double_press", + subtype: "remote_button_double_press" + } + ], + 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: "choose", + action_data: { + choose: [ + { + conditions: [ + { + $smType: "condition", + condition: 'time', + condition_data: { + after: { + hours: 16, + minutes: 30 + } + } + } + ], + sequence: [ + { + $smType: 'action', + action: 'event', + action_data: { + event: "superImportant_time", + event_data: { + time: '4:30PM' + } + } + } + ] + }, + { + conditions: [ + { + $smType: "condition", + condition: 'time', + condition_data: { + after: { + hours: 18, + minutes: 45 + } + } + } + ], + sequence: [ + { + $smType: 'action', + action: 'event', + action_data: { + event: "superImportant_time", + event_data: { + time: '6:45PM' + } + } + } + ] + } + ] + } + } + ], + } as AutomationData } \ No newline at end of file diff --git a/web/src/components/AutomationEditor/index.tsx b/web/src/components/AutomationEditor/index.tsx index 3a2ed90..79dd18e 100644 --- a/web/src/components/AutomationEditor/index.tsx +++ b/web/src/components/AutomationEditor/index.tsx @@ -7,6 +7,7 @@ import AutoInfoBox from "./AutoInfoBox"; import { NODE_HEIGHT, NODE_WIDTH } from "./constants"; import DAGEdge from "./DAGEdge"; import DAGNode from "./DAGNode"; +import { SequenceNodes } from "./SequenceNodes"; import { useEditorStyles } from "./styles"; import { Point } from "./types"; @@ -33,7 +34,20 @@ export default function AutomationEditor({ const renderTrigger = (trigger: AutomationTrigger, i: number) => { const nodeLoc: Point = [0 ,i*NODE_HEIGHT*1.25]; return <> - + { + onUpdate({ + ...automation, + trigger: [ + ...automation.trigger.slice(0, i), + ...automation.trigger.slice(i+1), + ] + }) + }} + /> } - const renderSequence = (node: AutomationSequenceNode, i: number) => { - const nodeLoc: Point = [pointTrigToCond[0] + NODE_WIDTH/2 , 0]; - let lastNodeLoc = pointTrigToCond; - if (i > 0) { - lastNodeLoc = [nodeLoc[0] + NODE_WIDTH*((i - 1)*nHDF+1), nodeLoc[1] + NODE_HEIGHT/2]; - nodeLoc[0] += NODE_WIDTH*i*nHDF - } - return <> - - - - } // render return
{automation.trigger.map(renderTrigger)} - - {automation.sequence.map(renderSequence)} - + onUpdate({ + ...automation, + sequence, + })} + nHDF={nHDF} + nodeHeight={NODE_HEIGHT} + nodeWidth={NODE_WIDTH} + triggerPoint={pointTrigToCond} + /> diff --git a/web/src/components/SequenceEditor/index.stories.tsx b/web/src/components/SequenceEditor/index.stories.tsx deleted file mode 100644 index 9845d15..0000000 --- a/web/src/components/SequenceEditor/index.stories.tsx +++ /dev/null @@ -1,38 +0,0 @@ -import { ComponentMeta, ComponentStory } from "@storybook/react"; -import { useState } from "react"; -import { AutomationAction } from "~/automations/types/actions"; -import { usePageTheme } from "~/styles/page"; -import { SequenceEditor } from "."; - -export default { - title: 'SequenceEditor', - component: SequenceEditor, - parameters: { actions: { argTypesRegex: '^on.*' } }, - } as ComponentMeta; - - -const Template: ComponentStory = ({node, ...args}) => { - const {classes} = usePageTheme({}); - const [data, setData] = useState(node); - return
- { - setData(data) - }} - /> -
-} - -export const ServiceAction = Template.bind({}) -ServiceAction.args = { - node: { - $smType: 'action', - action: 'service', - action_data: { - service: 'switch.toggle', - data: {}, - target: {}, - } - } as AutomationAction -} \ No newline at end of file diff --git a/web/src/components/SequenceEditor/index.tsx b/web/src/components/SequenceEditor/index.tsx deleted file mode 100644 index d4c1741..0000000 --- a/web/src/components/SequenceEditor/index.tsx +++ /dev/null @@ -1,18 +0,0 @@ -import { FC } from "react"; -import { AutomationAction } from "~/automations/types/actions"; - - -export const SequenceEditor: FC<{ - node: AutomationAction, - onUpdate: (u: AutomationAction) => void, -}> = ({ - node, - onUpdate, -}) => { - - return
-
- {node.action} -
-
-} \ No newline at end of file