latest thinking about automation editor
This commit is contained in:
1 parent
5b14210f95
commit
3e79ddffcc
5 files changed
+198
-83
No files matched your search
@@ -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<typeof useNodeStyles>,
|
||||||
|
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 <>
|
||||||
|
<DAGNode
|
||||||
|
key={`node.${i}`}
|
||||||
|
loc={nodeLoc}
|
||||||
|
text={getDescriptionFromAutomationNode(node)}
|
||||||
|
color={node.$smType === 'action' ? 'green' : 'blue'}
|
||||||
|
onXClick={() => {
|
||||||
|
onUpdate([
|
||||||
|
...sequence.slice(0, i),
|
||||||
|
...sequence.slice(i+1),
|
||||||
|
])
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<DAGEdge
|
||||||
|
key={`edge.${i}`}
|
||||||
|
p1={lastNodeLoc}
|
||||||
|
p2={[nodeLoc[0], nodeLoc[1] + nodeHeight/2]}
|
||||||
|
direction="1->2"
|
||||||
|
className={classes.dagEdge}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
}
|
||||||
|
return <g id="sequence">
|
||||||
|
{sequence.map(renderSequence)}
|
||||||
|
</g>
|
||||||
|
}
|
||||||
@@ -88,3 +88,114 @@ ServiceAction.args = {
|
|||||||
],
|
],
|
||||||
} as AutomationData
|
} 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
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import AutoInfoBox from "./AutoInfoBox";
|
|||||||
import { NODE_HEIGHT, NODE_WIDTH } from "./constants";
|
import { NODE_HEIGHT, NODE_WIDTH } from "./constants";
|
||||||
import DAGEdge from "./DAGEdge";
|
import DAGEdge from "./DAGEdge";
|
||||||
import DAGNode from "./DAGNode";
|
import DAGNode from "./DAGNode";
|
||||||
|
import { SequenceNodes } from "./SequenceNodes";
|
||||||
import { useEditorStyles } from "./styles";
|
import { useEditorStyles } from "./styles";
|
||||||
import { Point } from "./types";
|
import { Point } from "./types";
|
||||||
|
|
||||||
@@ -33,7 +34,20 @@ export default function AutomationEditor({
|
|||||||
const renderTrigger = (trigger: AutomationTrigger, i: number) => {
|
const renderTrigger = (trigger: AutomationTrigger, i: number) => {
|
||||||
const nodeLoc: Point = [0 ,i*NODE_HEIGHT*1.25];
|
const nodeLoc: Point = [0 ,i*NODE_HEIGHT*1.25];
|
||||||
return <>
|
return <>
|
||||||
<DAGNode key={`node.${i}`} loc={nodeLoc} text={getDescriptionFromAutomationNode(trigger)} color="red"/>
|
<DAGNode key={`node.${i}`}
|
||||||
|
loc={nodeLoc}
|
||||||
|
text={getDescriptionFromAutomationNode(trigger)}
|
||||||
|
color="red"
|
||||||
|
onXClick={() => {
|
||||||
|
onUpdate({
|
||||||
|
...automation,
|
||||||
|
trigger: [
|
||||||
|
...automation.trigger.slice(0, i),
|
||||||
|
...automation.trigger.slice(i+1),
|
||||||
|
]
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
/>
|
||||||
<DAGEdge
|
<DAGEdge
|
||||||
key={`edge.${i}`}
|
key={`edge.${i}`}
|
||||||
p1={[NODE_WIDTH , nodeLoc[1] + 0.5 * NODE_HEIGHT]}
|
p1={[NODE_WIDTH , nodeLoc[1] + 0.5 * NODE_HEIGHT]}
|
||||||
@@ -43,29 +57,6 @@ export default function AutomationEditor({
|
|||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
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 <>
|
|
||||||
<DAGNode
|
|
||||||
key={`node.${i}`}
|
|
||||||
loc={nodeLoc}
|
|
||||||
text={getDescriptionFromAutomationNode(node)}
|
|
||||||
color={node.$smType === 'action' ? 'green' : 'blue'}
|
|
||||||
/>
|
|
||||||
<DAGEdge
|
|
||||||
key={`edge.${i}`}
|
|
||||||
p1={lastNodeLoc}
|
|
||||||
p2={[nodeLoc[0], nodeLoc[1] + NODE_HEIGHT/2]}
|
|
||||||
direction="1->2"
|
|
||||||
className={classes.dagEdge}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
}
|
|
||||||
|
|
||||||
// render
|
// render
|
||||||
return <div className={classes.root} style={{
|
return <div className={classes.root} style={{
|
||||||
@@ -92,9 +83,18 @@ export default function AutomationEditor({
|
|||||||
<g className="triggers">
|
<g className="triggers">
|
||||||
{automation.trigger.map(renderTrigger)}
|
{automation.trigger.map(renderTrigger)}
|
||||||
</g>
|
</g>
|
||||||
<g className="sequence">
|
<SequenceNodes
|
||||||
{automation.sequence.map(renderSequence)}
|
styles={{classes, theme}}
|
||||||
</g>
|
sequence={automation.sequence}
|
||||||
|
onUpdate={sequence => onUpdate({
|
||||||
|
...automation,
|
||||||
|
sequence,
|
||||||
|
})}
|
||||||
|
nHDF={nHDF}
|
||||||
|
nodeHeight={NODE_HEIGHT}
|
||||||
|
nodeWidth={NODE_WIDTH}
|
||||||
|
triggerPoint={pointTrigToCond}
|
||||||
|
/>
|
||||||
<circle className={classes.circle} cx={pointTrigToCond[0]} cy={pointTrigToCond[1]} r={2}/>
|
<circle className={classes.circle} cx={pointTrigToCond[0]} cy={pointTrigToCond[1]} r={2}/>
|
||||||
<g className="actions">
|
<g className="actions">
|
||||||
</g>
|
</g>
|
||||||
|
|||||||
@@ -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<typeof SequenceEditor>;
|
|
||||||
|
|
||||||
|
|
||||||
const Template: ComponentStory<typeof SequenceEditor> = ({node, ...args}) => {
|
|
||||||
const {classes} = usePageTheme({});
|
|
||||||
const [data, setData] = useState(node);
|
|
||||||
return <div className={classes.page}>
|
|
||||||
<SequenceEditor {...args}
|
|
||||||
node={data}
|
|
||||||
onUpdate={data => {
|
|
||||||
setData(data)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ServiceAction = Template.bind({})
|
|
||||||
ServiceAction.args = {
|
|
||||||
node: {
|
|
||||||
$smType: 'action',
|
|
||||||
action: 'service',
|
|
||||||
action_data: {
|
|
||||||
service: 'switch.toggle',
|
|
||||||
data: {},
|
|
||||||
target: {},
|
|
||||||
}
|
|
||||||
} as AutomationAction
|
|
||||||
}
|
|
||||||
@@ -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 <div>
|
|
||||||
<div>
|
|
||||||
<span>{node.action}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user