diff --git a/webapp/src/components/DAGFlow/constants.ts b/webapp/src/components/DAGFlow/constants.ts
index b732c41..e761c6e 100644
--- a/webapp/src/components/DAGFlow/constants.ts
+++ b/webapp/src/components/DAGFlow/constants.ts
@@ -19,4 +19,5 @@ export const DEFAULT_DIMS = {
x: PADDING,
y: PADDING,
},
+ flipped: false,
} as DAGAutomationFlowDims
\ No newline at end of file
diff --git a/webapp/src/components/DAGFlow/flowDataMods.ts b/webapp/src/components/DAGFlow/flowDataMods.ts
index 79b9c60..a0e0742 100644
--- a/webapp/src/components/DAGFlow/flowDataMods.ts
+++ b/webapp/src/components/DAGFlow/flowDataMods.ts
@@ -7,6 +7,7 @@ export const makeAddButton = (
position: XYPosition,
{
circleSize,
+ flipped,
}: DAGAutomationFlowDims,
onAdd: () => void,
disableTarget: boolean = true,
@@ -14,6 +15,7 @@ export const makeAddButton = (
id,
position,
{
+ flipped,
size: circleSize,
onAdd,
disableSource: true,
diff --git a/webapp/src/components/DAGFlow/index.stories.tsx b/webapp/src/components/DAGFlow/index.stories.tsx
index 0832e27..f221734 100644
--- a/webapp/src/components/DAGFlow/index.stories.tsx
+++ b/webapp/src/components/DAGFlow/index.stories.tsx
@@ -92,7 +92,10 @@ Complex.args = {
{
"platform": "homeassistant",
"event": "start",
- }
+ },
+ {
+ "bad": "node"
+ } as any
],
sequence: [
{
@@ -120,6 +123,9 @@ Complex.args = {
"media_content_type": "SPOTIFY"
}
},
+ {
+ "alias": "Bad Node",
+ },
{
"choose": [
{
@@ -231,4 +237,16 @@ Errors.args = {
"choose": {}
} as any
]
+}
+
+
+export const ComplexFlipped = Complex.bind({})
+
+ComplexFlipped.args = {
+ ...Complex.args,
+ dims: {
+ ...dgconst.DEFAULT_DIMS,
+ ...Complex.args.dims,
+ flipped: true,
+ }
}
\ No newline at end of file
diff --git a/webapp/src/components/DAGFlow/sequenceConvertor.ts b/webapp/src/components/DAGFlow/sequenceConvertor.ts
index 9e9da1b..f3bf2d6 100644
--- a/webapp/src/components/DAGFlow/sequenceConvertor.ts
+++ b/webapp/src/components/DAGFlow/sequenceConvertor.ts
@@ -176,6 +176,7 @@ const addChooseNode = (
x: position.x + dims.nodeWidth * dims.distanceFactor,
y: lastPos.y + dims.nodeHeight * dims.distanceFactor,
}, {
+ flipped: dims.flipped,
size: dims.circleSize,
onRemove: createOnRemoveForChooseNode(
node,
@@ -241,7 +242,7 @@ const addChooseNode = (
const elseCircle = makeFlowCircle(`${nodeId}>else`, {
x: position.x + dims.nodeWidth * dims.distanceFactor,
y: lastPos.y + dims.nodeHeight * dims.distanceFactor * 2,
- }, { size: dims.circleSize })
+ }, { size: dims.circleSize, flipped: dims.flipped })
flowData.nodes.push(elseCircle)
addEdge(flowData, nodeId, elseCircle.id, true)
const lastPoint = sequenceToFlow(flowData, node.default ?? [], elseCircle.id, {
diff --git a/webapp/src/components/DAGFlow/types.ts b/webapp/src/components/DAGFlow/types.ts
index de992e8..ece7e70 100644
--- a/webapp/src/components/DAGFlow/types.ts
+++ b/webapp/src/components/DAGFlow/types.ts
@@ -15,6 +15,7 @@ export type DAGAutomationFlowDims = {
conditionHeight: number;
circleSize: number;
distanceFactor: number;
+ flipped: boolean;
}
export type DAGAutomationFlowNode = Node
;
diff --git a/webapp/src/components/Inputs/InputWrapper.tsx b/webapp/src/components/Inputs/InputWrapper.tsx
index 81a8469..451619b 100644
--- a/webapp/src/components/Inputs/InputWrapper.tsx
+++ b/webapp/src/components/Inputs/InputWrapper.tsx
@@ -7,7 +7,7 @@ export interface Props {
labelSize?: 'normal' | 'small';
noMargin?: boolean;
className?: string;
- error?: string;
+ error?: JSX.Element | string;
children: ReactNode;
}
const InputWrapper: FC = ({ error, label, children, labelSize = 'normal', noMargin = false, className = "" }) => {
diff --git a/webapp/src/components/Inputs/InputYaml.tsx b/webapp/src/components/Inputs/InputYaml.tsx
index 25ae2f4..418681a 100644
--- a/webapp/src/components/Inputs/InputYaml.tsx
+++ b/webapp/src/components/Inputs/InputYaml.tsx
@@ -1,5 +1,5 @@
import * as yaml from "js-yaml";
-import { useState } from "react";
+import { useEffect, useState } from "react";
import { useDelayEffect } from 'utils/useDelay';
import CodeMirror from '@uiw/react-codemirror';
@@ -10,18 +10,20 @@ export interface Props {
label: string;
value: T;
onChange: (v: T) => void;
+ error?: JSX.Element | string,
}
export default function InputYaml({
label,
+ error: incmError,
value = {} as any,
onChange,
}: Props) {
const [{ text, error }, setState] = useState<{
text: string,
- error: undefined | string,
+ error: undefined | JSX.Element | string,
}>({
text: yaml.dump(value),
- error: undefined
+ error: incmError,
});
const setText = (t: string) => {
try {
@@ -31,6 +33,12 @@ export default function InputYaml({
setState({ text: t, error: "! Invalid Yaml !" })
}
}
+ useEffect(() => {
+ setState({
+ text: yaml.dump(value),
+ error: incmError,
+ })
+ }, [value, incmError])
useDelayEffect(() => {
if (!error && (yaml.dump(value) !== text)) {
if (text) {
diff --git a/webapp/src/components/NodeEditor/OptionManager/index.tsx b/webapp/src/components/NodeEditor/OptionManager/index.tsx
index 49d93b3..5ad5327 100644
--- a/webapp/src/components/NodeEditor/OptionManager/index.tsx
+++ b/webapp/src/components/NodeEditor/OptionManager/index.tsx
@@ -2,42 +2,44 @@ import { Dispatch, SetStateAction, useEffect, useState } from 'react';
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
import { AutomationNodeSubtype } from 'types/automations';
import { getOptionManager } from './getOptionManager';
-import { getNodeType, getNodeSubType, getSubTypeList, validateNode } from 'utils/automations';
+import { getNodeSubType, getSubTypeList, validateNode } from 'utils/automations';
import InputYaml from 'components/Inputs/InputYaml';
import InputText from 'components/Inputs/InputText';
import { getDescriptionFromAutomationNode } from 'utils/formatting';
import { useHAEntities } from 'haService';
+import { Generic } from './Generic';
+import { getNodeTypeAndValidate } from '../../../utils/automations';
type UseState = (s: S) => [S, Dispatch>]
-export const useEditorNodeState = (originalNode: AutomationNode, isErrored: boolean, uState: UseState = useState) => {
+export const useEditorNodeState = (originalNode: AutomationNode, isErrored: boolean, allowedTypes: AutomationNodeTypes[], uState: UseState = useState) => {
+ // state
const [allState, setAllState] = uState({
yamlMode: false,
isModified: false,
- ...computeInvalidYaml(originalNode)
+ ...computeInvalidYaml(originalNode, allowedTypes)
});
const entities = useHAEntities();
+ // aliases
const { node: currentNode, isModified, yamlMode, invalidManualYaml } = allState;
+ // effects
useEffect(() => {
setAllState({ ...allState, node: originalNode, isModified: false, })
// eslint-disable-next-line
}, [originalNode])
+ // function aliases
const setState = (node: AutomationNode) => setAllState(({
...allState, node,
isModified: JSON.stringify(node) !== JSON.stringify(originalNode),
}));
const setYamlMode = (yamlMode: boolean) => setAllState({ ...allState, yamlMode, });
const setStateForManualYaml = (yaml: any) => {
- try {
- createData(yaml, yaml)
- setAllState({
- ...allState,
- ...computeInvalidYaml(yaml)
- })
- } catch (err) {
- setAllState({ ...allState, invalidManualYaml: <>{String(err)}>, isErrored: true, })
- }
+ setAllState({
+ ...allState,
+ ...computeInvalidYaml(yaml, allowedTypes)
+ })
}
+ // value aliases
const {
originalNodeType,
originalNodeSubType,
@@ -45,7 +47,8 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
subType,
subTypes,
optionManager,
- } = createData(originalNode, currentNode);
+ } = createData(originalNode, currentNode, allowedTypes)
+ // publuc
return {
nodeType,
subType,
@@ -54,6 +57,13 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
invalidManualYaml,
isModified,
isErrored: allState.isErrored,
+ get prettyNodeType() {
+ if (nodeType) {
+ return `${nodeType.slice(0, 1).toUpperCase()}${nodeType.slice(1)} Type`
+ } else {
+ return 'n/a'
+ }
+ },
get yamlMode() {
return allState.isErrored || yamlMode
},
@@ -61,9 +71,11 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
return currentNode
},
renderOptionList() {
+ console.log('rendering option list')
let aliasEditor = <>>
if (nodeType === 'trigger') {
aliasEditor = setState({
@@ -74,6 +86,7 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
}
if (nodeType === 'action') {
aliasEditor = setState({
@@ -89,8 +102,8 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
label=''
value={currentNode}
onChange={setStateForManualYaml}
+ error={invalidManualYaml}
/>
- {invalidManualYaml}
>
}
return <>
@@ -108,25 +121,43 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
setState(getOptionManager(newType, getSubTypeList(newType)[0]).defaultState())
}
},
- setSubType(newSubType: AutomationNodeSubtype) {
+ setSubType(newSubType: AutomationNodeSubtype) {
if (newSubType === originalNodeSubType) {
setState(originalNode)
} else {
- setState(getOptionManager(nodeType, newSubType as any).defaultState())
+ if (nodeType) {
+ setState(getOptionManager(nodeType, newSubType as any).defaultState())
+ } else {
+ setState(getOptionManager(allowedTypes[0], newSubType as any).defaultState())
+ }
}
}
}
}
-const createData = (originalNode: AutomationNode, currentNode: AutomationNode) => {
- const originalNodeType = getNodeType(originalNode);
- const originalNodeSubType = getNodeSubType(originalNode);
- const nodeType = getNodeType(currentNode);
- const subType = getNodeSubType(currentNode);
- const subTypes = getSubTypeList(nodeType)
- const optionManager = getOptionManager(nodeType, subType as any);
+const createData = (originalNode: AutomationNode, currentNode: AutomationNode, allowedTypes: AutomationNodeTypes[]) => {
+ let originalNodeType: AutomationNodeTypes = allowedTypes[0];
+ let originalNodeSubType: AutomationNodeSubtype | null = null;
+ let nodeType: AutomationNodeTypes = originalNodeType;
+ let subType: AutomationNodeSubtype | null = originalNodeSubType;
+ let subTypes: AutomationNodeSubtype[] = getSubTypeList(allowedTypes[0]);
+ let optionManager = Generic;
+ try {
+ originalNodeType = getNodeTypeAndValidate(originalNode, allowedTypes);
+ originalNodeSubType = getNodeSubType(originalNode);
+ } catch (err) {
+ console.error(`ORIGINAL NODE ERROR ${JSON.stringify(originalNode)}`)
+ }
+ try {
+ nodeType = getNodeTypeAndValidate(currentNode, allowedTypes);
+ subType = getNodeSubType(currentNode);
+ subTypes = getSubTypeList(nodeType)
+ optionManager = getOptionManager(nodeType, subType as any);
+ } catch (err) {
+ console.error(`CURRENT NODE ERROR ${JSON.stringify(currentNode)}`)
+ }
return {
originalNodeType,
originalNodeSubType,
@@ -137,13 +168,14 @@ const createData = (originalNode: AutomationNode, currentNode: AutomationNode) =
}
}
-const computeInvalidYaml = (node: AutomationNode) => {
- const errors = validateNode(node);
+const computeInvalidYaml = (node: AutomationNode, allowedNodeTypes: AutomationNodeTypes[]) => {
+ const errors = validateNode(node, allowedNodeTypes);
if (errors) {
+ console.log({ errors })
return {
node,
isErrored: true,
- invalidManualYaml:
+ invalidManualYaml:
{errors.map(({ message, path }, i) => -
{path}: {message}
)}
diff --git a/webapp/src/components/NodeEditor/index.css b/webapp/src/components/NodeEditor/index.css
index 50eb527..a6cde99 100644
--- a/webapp/src/components/NodeEditor/index.css
+++ b/webapp/src/components/NodeEditor/index.css
@@ -58,4 +58,9 @@
max-width: 30em;
overflow: auto;
max-height: 10em;
+ display: flex;
+ flex-direction: column;
+ align-items: flex-start;
+ padding: 0 0.5em 0 1.5em;
+ margin: 0;
}
\ No newline at end of file
diff --git a/webapp/src/components/NodeEditor/index.tsx b/webapp/src/components/NodeEditor/index.tsx
index dc72ffb..5a306b0 100644
--- a/webapp/src/components/NodeEditor/index.tsx
+++ b/webapp/src/components/NodeEditor/index.tsx
@@ -29,7 +29,7 @@ export const NodeEditor: FC = ({
isErrored = false,
}) => {
// state
- const state = useEditorNodeState(node, isErrored);
+ const state = useEditorNodeState(node, isErrored, allowedTypes);
// alias
const isModified = state.isModified;
@@ -53,7 +53,7 @@ export const NodeEditor: FC = ({
onChange={state.setNodeType}
/> : <>>}
{state.nodeType !== 'condition' ? {
+ const nodeType = getNodeType(node);
+ if (allowedTypes.includes(nodeType)) {
+ return nodeType
+ }
+ throw new Error(`Detected '${nodeType}' but expected one of {${allowedTypes}}`)
+}
export const getNodeType = (node: AutomationNode): AutomationNodeTypes => {
if ('condition' in node) {
@@ -107,72 +117,75 @@ export const getNodeSubType = (
}
}
+const TriggerValidators: Record = {
+ "homeassistant": v.triggers.AutomationTriggerHA,
+ "device": v.triggers.AutomationTriggerDevice,
+ "event": v.triggers.AutomationTriggerEvent,
+ "mqtt": v.triggers.AutomationTriggerMQTT,
+ 'numeric_state': v.triggers.AutomationTriggerNumericState,
+ "state": v.triggers.AutomationTriggerState,
+ "tag": v.triggers.AutomationTriggerTag,
+ "template": v.triggers.AutomationTriggerTemplate,
+ "time": v.triggers.AutomationTriggerTime,
+ "time_pattern": v.triggers.AutomationTriggerTimePattern,
+ "webhook": v.triggers.AutomationTriggerWebhook,
+ "zone": v.triggers.AutomationTriggerZone,
+}
-export const validateNode = (node: AutomationNode): MiniFailure[] | null => {
- const nodeType = getNodeType(node);
+const ActionValidators: Record = {
+ "choose": v.actions.ChooseAction,
+ "device": v.actions.DeviceAction,
+ "event": v.actions.FireEventAction,
+ "repeat": v.actions.RepeatAction,
+ "service": v.actions.ServiceAction,
+ "wait": v.actions.WaitAction,
+}
+
+const ConditionValidators: Record = {
+ "and": v.conditions.LogicCondition,
+ "or": v.conditions.LogicCondition,
+ "not": v.conditions.LogicCondition,
+ "numeric_state": v.conditions.NumericCondition,
+ "state": v.conditions.StateCondition,
+ "template": v.conditions.TemplateCondition,
+ "time": v.conditions.TimeCondition,
+ "trigger": v.conditions.TriggerCondition,
+ "zone": v.conditions.ZoneCondition,
+}
+
+const AllValidators: Record = {
+ "action": ActionValidators,
+ "condition": ConditionValidators,
+ "trigger": TriggerValidators,
+}
+
+export const validateNode = (node: AutomationNode, allowedNodeTypes: AutomationNodeTypes[] = []): MiniFailure[] | null => {
+ let nodeType = getNodeType(node);
+ let errors: MiniFailure[] = []
+ if (allowedNodeTypes.length > 0 && !allowedNodeTypes.includes(nodeType)) {
+ errors.push(
+ { "message": [`Got node of type "${nodeType}" but expected one of {${allowedNodeTypes}}`], path: "" }
+ )
+ nodeType = allowedNodeTypes[0]
+ }
+ let nodeSubType: null | AutomationNodeSubtype = null;
try {
- const nodeSubType = getNodeSubType(node);
- if (nodeType === 'action') {
- if (nodeSubType === 'service') {
- return getFailures(node, v.actions.ServiceAction)
- } else if (nodeSubType === 'repeat') {
- return getFailures(node, v.actions.RepeatAction)
- } else if (nodeSubType === 'wait') {
- return getFailures(node, v.actions.WaitAction)
- } else if (nodeSubType === 'event') {
- return getFailures(node, v.actions.FireEventAction)
- } else if (nodeSubType === 'device') {
- return getFailures(node, v.actions.DeviceAction)
- } else if (nodeSubType === 'choose') {
- return getFailures(node, v.actions.ChooseAction)
- }
- } else if (nodeType === 'trigger') {
- if (nodeSubType === 'event') {
- return getFailures(node, v.triggers.AutomationTriggerEvent)
- } else if (nodeSubType === 'homeassistant') {
- return getFailures(node, v.triggers.AutomationTriggerHA)
- } else if (nodeSubType === 'mqtt') {
- return getFailures(node, v.triggers.AutomationTriggerMQTT)
- } else if (nodeSubType === 'numeric_state') {
- return getFailures(node, v.triggers.AutomationTriggerNumericState)
- } else if (nodeSubType === 'state') {
- return getFailures(node, v.triggers.AutomationTriggerState)
- } else if (nodeSubType === 'tag') {
- return getFailures(node, v.triggers.AutomationTriggerTag)
- } else if (nodeSubType === 'template') {
- return getFailures(node, v.triggers.AutomationTriggerTemplate)
- } else if (nodeSubType === 'time') {
- return getFailures(node, v.triggers.AutomationTriggerTime)
- } else if (nodeSubType === 'time_pattern') {
- return getFailures(node, v.triggers.AutomationTriggerTimePattern)
- } else if (nodeSubType === 'webhook') {
- return getFailures(node, v.triggers.AutomationTriggerWebhook)
- } else if (nodeSubType === 'zone') {
- return getFailures(node, v.triggers.AutomationTriggerZone)
- } else if (nodeSubType === 'device') {
- return getFailures(node, v.triggers.AutomationTriggerDevice)
- }
- } else if (nodeType === 'condition') {
- if (['and', 'or', 'not'].includes(nodeSubType)) {
- return getFailures(node, v.conditions.LogicCondition)
- } else if (nodeSubType === 'numeric_state') {
- return getFailures(node, v.conditions.NumericCondition)
- } else if (nodeSubType === 'state') {
- return getFailures(node, v.conditions.StateCondition)
- } else if (nodeSubType === 'template') {
- return getFailures(node, v.conditions.TemplateCondition)
- } else if (nodeSubType === 'trigger') {
- return getFailures(node, v.conditions.TriggerCondition)
- } else if (nodeSubType === 'time') {
- return getFailures(node, v.conditions.TimeCondition)
- } else if (nodeSubType === 'zone') {
- return getFailures(node, v.conditions.ZoneCondition)
- }
+ nodeSubType = getNodeSubType(node);
+
+ } catch (err) {
+ errors.push(
+ { "message": [`Failed to identifiy subtype`], path: "" }
+ )
+ }
+ if (nodeSubType) {
+ const failures = getFailures(node, AllValidators[nodeType][nodeSubType]);
+ if (failures) {
+ errors = errors.concat(failures)
}
- } catch (err) {
- console.warn(err)
+ }
+ if (errors.length > 0) {
+ return errors
+ } else {
+ return null
}
- return [
- { "message": ["Invalid or unknown " + nodeType], "path": "" },
- ]
}
\ No newline at end of file