From c118ccc99bdc15a4c919c62bebc4ca1fa8231c79 Mon Sep 17 00:00:00 2001 From: Ari Date: Mon, 9 May 2022 22:14:51 -0400 Subject: [PATCH] no more $smType in webapp --- .../AutomationEditor/index.stories.tsx | 20 +- .../src/components/DAGFlow/index.stories.tsx | 212 +++++++----------- .../components/DAGFlow/sequenceConvertor.ts | 27 +-- webapp/src/components/DAGFlow/updater.test.ts | 46 ++-- webapp/src/components/DAGFlow/updater.ts | 74 +++--- .../src/components/MultiNodeEditors/index.tsx | 10 +- .../NodeEditor/OptionManager/ActionChoose.tsx | 14 +- .../NodeEditor/OptionManager/ActionDevice.tsx | 34 ++- .../NodeEditor/OptionManager/ActionEvent.tsx | 18 +- .../NodeEditor/OptionManager/ActionRepeat.tsx | 26 +-- .../OptionManager/ActionService.tsx | 14 +- .../NodeEditor/OptionManager/ActionWait.tsx | 22 +- .../NodeEditor/OptionManager/OptionManager.ts | 7 +- .../NodeEditor/OptionManager/index.ts | 54 +---- webapp/src/components/NodeEditor/constants.ts | 17 +- .../components/NodeEditor/index.stories.tsx | 10 +- webapp/src/types/automations/actions.ts | 47 ++-- webapp/src/utils/formatting.test.ts | 92 +++----- webapp/src/utils/formatting.ts | 65 +++--- webapp/src/utils/mocks.ts | 20 +- 20 files changed, 327 insertions(+), 502 deletions(-) diff --git a/webapp/src/components/AutomationEditor/index.stories.tsx b/webapp/src/components/AutomationEditor/index.stories.tsx index 8bb2c10..baa288f 100644 --- a/webapp/src/components/AutomationEditor/index.stories.tsx +++ b/webapp/src/components/AutomationEditor/index.stories.tsx @@ -84,18 +84,14 @@ Simple.args = { ] }, { - $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", - } + 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", } } ] diff --git a/webapp/src/components/DAGFlow/index.stories.tsx b/webapp/src/components/DAGFlow/index.stories.tsx index c24106b..9fa3747 100644 --- a/webapp/src/components/DAGFlow/index.stories.tsx +++ b/webapp/src/components/DAGFlow/index.stories.tsx @@ -73,18 +73,14 @@ Simple.args = { ] }, { - $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", - } + 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", } } ] @@ -120,128 +116,92 @@ Complex.args = { ] }, { - "$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" - } + "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": [ - { - "condition": "state", - "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": [ - { - "condition": "state", - "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", + "choose": [ + { + "conditions": [ + { + "condition": "state", + "entity_id": "lights.bathroom", + "state": "off", + } + ], + "sequence": [ + { + "service": "light.turn_on", "target": { "entity_id": "lights.bathroom" }, "data": {} + }, + { + "alias": "Turn off bedroom", + "service": "light.turn_off", + "target": { + "entity_id": "lights.bedroom" + }, + "data": {} } - } - ] - } + ] + }, + { + "conditions": [ + { + "condition": "state", + "entity_id": "lights.bedroom", + "state": "off", + } + ], + "sequence": [ + { + "alias": "Turn on bedroom", + "service": "light.turn_on", + "target": { + "entity_id": "lights.bedroom" + }, + "data": {} + }, + { + "choose": [ + { + "conditions": [], + "sequence": [ + { + "choose": [], + "default": [], + } + ] + } + ], + "default": [ + { + "choose": [], + "default": [], + } + ], + } + ] + } + ], + "default": [ + { + "service": "light.turn_off", + "target": { + "entity_id": "lights.bathroom" + }, + "data": {} + } + ] }, { "condition": "and", diff --git a/webapp/src/components/DAGFlow/sequenceConvertor.ts b/webapp/src/components/DAGFlow/sequenceConvertor.ts index f28332f..8392acb 100644 --- a/webapp/src/components/DAGFlow/sequenceConvertor.ts +++ b/webapp/src/components/DAGFlow/sequenceConvertor.ts @@ -35,8 +35,8 @@ export const sequenceToFlow = ( }; } // draw node - if ('$smType' in node && node.$smType === 'action') { - if (node.action === 'choose') { + if (getNodeType(node) === 'action') { + if ('choose' in node) { position = addChooseNode(flowData, node, position, nodeId, dims, updater, i) } else { addSingleNode(flowData, node, position, nodeId, dims, { @@ -127,7 +127,7 @@ const addChooseNode = ( }); let lastPos: XYPosition = position; // conditions - node.action_data.choose.forEach(({ sequence, conditions }, j) => { + node.choose.forEach(({ sequence, conditions }, j) => { const sequenceId = `${nodeId}.${j}`; // edit/delete circle const circle = makeFlowCircle(`${sequenceId}>cond`, { @@ -158,7 +158,7 @@ const addChooseNode = ( lastPos = xyApply(lastPos, { x: lastPos.x, y: lastPos.y + dims.nodeHeight * dims.distanceFactor }, Math.max) } }) - const totalChildren = node.action_data.choose.length; + const totalChildren = node.choose.length; // add button const addCirlce = makeFlowCircle( @@ -170,16 +170,13 @@ const addChooseNode = ( size: dims.circleSize * dims.distanceFactor, onAdd: () => updater.updateNode(i, { ...node, - action_data: { - ...node.action_data, - choose: [ - ...node.action_data.choose, - { - sequence: [], - conditions: [], - } - ] - } + choose: [ + ...node.choose, + { + sequence: [], + conditions: [], + } + ] }), disableSource: true, } @@ -194,7 +191,7 @@ const addChooseNode = ( }, { size: dims.circleSize * dims.distanceFactor }) flowData.nodes.push(elseCircle) addEdge(flowData, nodeId, elseCircle.id, true) - const lastPoint = sequenceToFlow(flowData, node.action_data.default, elseCircle.id, { + const lastPoint = sequenceToFlow(flowData, node.default, elseCircle.id, { ...dims, padding: { x: position.x - dims.nodeWidth, diff --git a/webapp/src/components/DAGFlow/updater.test.ts b/webapp/src/components/DAGFlow/updater.test.ts index ff37774..04b41ff 100644 --- a/webapp/src/components/DAGFlow/updater.test.ts +++ b/webapp/src/components/DAGFlow/updater.test.ts @@ -27,44 +27,36 @@ const mockSequenceUpdater = (defaultState: AutomationSequenceNode[] = []) => { test("sequence updater updates nodes", () => { const mock = mockSequenceUpdater([ { - "$smType": "action", - "action": "choose", - "action_data": { - "choose": [ - { - "conditions": [], - "sequence": [], - } - ], - "default": [], - } + "choose": [ + { + "conditions": [], + "sequence": [], + } + ], + "default": [], } ]); expect(mock.state.length).toBe(1) - expect((mock.state[0] as ChooseAction).action_data.alias).toBe(undefined) + expect((mock.state[0] as ChooseAction).alias).toBe(undefined) mock.updater.updateNode(0, { ...mock.state[0], action_data: { - ...(mock.state[0] as ChooseAction).action_data, + ...(mock.state[0] as ChooseAction), alias: "Hello" } } as any) - expect((mock.state[0] as ChooseAction).action_data.alias).toBe("Hello") + expect((mock.state[0] as ChooseAction).alias).toBe("Hello") }) test('update conditions for node', () => { const mock = mockSequenceUpdater([ { - "$smType": "action", - "action": "choose", - "action_data": { - "choose": [ - { - "conditions": [], - "sequence": [], - } - ], - "default": [], - } + "choose": [ + { + "conditions": [], + "sequence": [], + } + ], + "default": [], } ]); mock.updater.makeOnEditConditionsForChooseNode(0, 0)(); @@ -77,6 +69,6 @@ test('update conditions for node', () => { "conditions": [], } ] as any) - expect((mock.state[0] as ChooseAction).action_data.choose[0].conditions.length).toBe(1) - expect((mock.state[0] as ChooseAction).action_data.choose[0].conditions[0].condition).toBe("and") + expect((mock.state[0] as ChooseAction).choose[0].conditions.length).toBe(1) + expect((mock.state[0] as ChooseAction).choose[0].conditions[0].condition).toBe("and") }) \ No newline at end of file diff --git a/webapp/src/components/DAGFlow/updater.ts b/webapp/src/components/DAGFlow/updater.ts index 55971bc..cd6d869 100644 --- a/webapp/src/components/DAGFlow/updater.ts +++ b/webapp/src/components/DAGFlow/updater.ts @@ -23,12 +23,8 @@ export const makeSequenceUpdater = ( single: true, saveBtnCreateText: true, node: { - $smType: 'action', - action: 'choose', - action_data: { - choose: [], - default: [], - } + choose: [], + default: [], }, update: n => onChange([ ...sequence, @@ -44,44 +40,38 @@ export const makeSequenceUpdater = ( ...sequence.slice(i + 1), ]) }, - updateChooseActionData(i: number, data: ChooseAction['action_data']) { + updateChooseActionData(i: number, data: ChooseAction) { const node = sequence[i] as ChooseAction; return this.updateNode(i, { ...node, - action_data: data, + ...data, }) }, makeChildUpdaterForChooseAction(i: number, j: number | null) { const node = sequence[i]; - if (('$smType' in node) && (node.$smType === 'action') && (node.action === 'choose')) { + if ('choose' in node) { if (j !== null) { return makeSequenceUpdater( - node.action_data.choose[j].sequence, + node.choose[j].sequence, seq => this.updateNode(i, { ...node, - action_data: { - ...node.action_data, - choose: [ - ...node.action_data.choose.slice(0, j), - { - ...node.action_data.choose[j], - sequence: seq, - }, - ...node.action_data.choose.slice(j + 1), - ] - } + choose: [ + ...node.choose.slice(0, j), + { + ...node.choose[j], + sequence: seq, + }, + ...node.choose.slice(j + 1), + ] }), openModal, ) } else { return makeSequenceUpdater( - node.action_data.default, + node.default, seq => this.updateNode(i, { ...node, - action_data: { - ...node.action_data, - default: seq - } + default: seq }), openModal, ) @@ -108,21 +98,18 @@ export const makeSequenceUpdater = ( const node = sequence[i] as ChooseAction; return openModal({ single: false, - node: node.action_data.choose[j].conditions, + node: node.choose[j].conditions, allowedTypes: ['condition'], update: conditions => this.updateNode(i, { ...node, - action_data: { - ...node.action_data, - choose: [ - ...node.action_data.choose.slice(0, j), - { - sequence: node.action_data.choose[j].sequence, - conditions: conditions as AutomationCondition[], - }, - ...node.action_data.choose.slice(j + 1), - ] - } + choose: [ + ...node.choose.slice(0, j), + { + sequence: node.choose[j].sequence, + conditions: conditions as AutomationCondition[], + }, + ...node.choose.slice(j + 1), + ] }), }) } @@ -136,11 +123,8 @@ export const createOnRemoveForChooseNode = ( j: number ) => () => update({ ...action, - action_data: { - ...action.action_data, - choose: [ - ...action.action_data.choose.slice(0, j), - ...action.action_data.choose.slice(j + 1), - ] - } + choose: [ + ...action.choose.slice(0, j), + ...action.choose.slice(j + 1), + ] }) diff --git a/webapp/src/components/MultiNodeEditors/index.tsx b/webapp/src/components/MultiNodeEditors/index.tsx index 1c295ec..5058e97 100644 --- a/webapp/src/components/MultiNodeEditors/index.tsx +++ b/webapp/src/components/MultiNodeEditors/index.tsx @@ -9,13 +9,9 @@ const makeDefault = (nodeType: keyof AutomationNodeMapping): AutomationNode => { switch (nodeType) { case 'action': return { - $smType: 'action', - action: "service", - action_data: { - 'data': {}, - 'service': '', - 'target': {}, - } + 'data': {}, + 'service': '', + 'target': {}, } case 'condition': return { diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx index 2c15411..7bfd530 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx @@ -5,15 +5,11 @@ import { OptionManager, updateActionData } from './OptionManager'; export const ActionChooseState: OptionManager = { defaultState: () => ({ - $smType: 'action', - action: 'choose', - action_data: { - alias: "Choose", - default: [], - choose: [], - } + alias: "", + default: [], + choose: [], }), - isReady: ({ action_data: { alias } }) => { + isReady: ({ alias }) => { return alias !== '' }, renderOptionList: (state, setState) => { @@ -21,7 +17,7 @@ export const ActionChooseState: OptionManager = { return <> update({ alias })} /> diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionDevice.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionDevice.tsx index e864091..d9be6d9 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionDevice.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionDevice.tsx @@ -5,24 +5,18 @@ import { OptionManager, updateActionData } from './OptionManager'; export const ActionDeviceState: OptionManager = { defaultState: () => ({ - $smType: 'action', - action: 'device', - action_data: { - alias: "Device", - type: "", - entity_id: "", - device_id: "", - domain: "", - } + alias: "", + type: "", + entity_id: "", + device_id: "", + domain: "", }), isReady: ({ - action_data: { - alias, - type, - entity_id, - device_id, - domain, - } + alias, + type, + entity_id, + device_id, + domain, }) => ( alias !== '' && type !== "" && @@ -35,25 +29,25 @@ export const ActionDeviceState: OptionManager = { return <> update({ alias })} /> update({ device_id })} /> update({ type })} /> update({ entity_id, domain: entity_id.split('.')[0] })} /> diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionEvent.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionEvent.tsx index a1be7db..b3da002 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionEvent.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionEvent.tsx @@ -6,15 +6,11 @@ import { OptionManager, updateActionData } from "./OptionManager"; export const ActionEventState: OptionManager = { defaultState: () => ({ - $smType: "action", - action: 'event', - action_data: { - alias: "Fire Event", - event: "", - event_data: {}, - } + alias: "Fire Event", + event: "", + event_data: {}, }), - isReady: ({ action_data: { alias, event } }) => ( + isReady: ({ alias, event }) => ( alias !== '' && event !== "" ), @@ -23,18 +19,18 @@ export const ActionEventState: OptionManager = { return <> update({ alias })} /> update({ event })} /> update({ event_data })} /> diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx index 930653e..ac775ac 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx @@ -7,20 +7,16 @@ import { OptionManager, updateActionData } from './OptionManager'; export const ActionRepeatState: OptionManager = { defaultState: () => ({ - $smType: 'action', - action: 'repeat', - action_data: { - alias: "Repeat", - repeat: { - count: 1, - sequence: [], - } + alias: "Repeat", + repeat: { + count: 1, + sequence: [], } }), - isReady: ({ action_data: { + isReady: ({ alias, repeat, - } }) => ( + }) => ( alias !== '' && repeat?.sequence.length > 0 ), @@ -29,18 +25,18 @@ export const ActionRepeatState: OptionManager = { return <> update({ alias })} /> update({ repeat: { ...state.action_data.repeat, count } })} + value={state.repeat.count} + onChange={(count = 1) => update({ repeat: { ...state.repeat, count } })} /> update({ repeat: { ...state.action_data.repeat, sequence } })} + value={state.repeat.sequence ?? []} + onChange={sequence => update({ repeat: { ...state.repeat, sequence } })} /> } diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionService.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionService.tsx index 7d43b87..f5086a7 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionService.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionService.tsx @@ -6,23 +6,19 @@ import { OptionManager, updateActionData } from './OptionManager'; export const ActionCallServiceState: OptionManager = { defaultState: () => ({ - $smType: 'action', - action: 'service', - action_data: { - service: "", - target: "", - data: {}, - } + service: "", + target: "", + data: {}, }), isReady: ({ - action_data: { service, alias, target } + service, alias, target }) => ( service !== '' && alias !== '' && target !== '' ), renderOptionList: (state, setState) => { - const { service, alias = "", target } = state.action_data; + const { service, alias = "", target } = state; const update = updateActionData(state, setState); return <> = { defaultState: () => ({ - $smType: 'action', - action: 'wait', - action_data: { - alias: "Wait", - wait_template: "", - // timeout: 0, - continue_on_timeout: false, - } + alias: "Wait", + wait_template: "", + // timeout: 0, + continue_on_timeout: false, }), isReady: ({ - action_data: { alias, wait_template } + alias, wait_template }) => ( alias !== '' && wait_template !== "" @@ -26,23 +22,23 @@ export const ActionWaitState: OptionManager = { return <> update({ alias })} /> update({ wait_template })} /> {/* update({ timeout })} /> */} update({ continue_on_timeout })} /> diff --git a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts index a957c81..afcff06 100644 --- a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts @@ -21,11 +21,8 @@ export const updateActionData = ( state: T, setState: (s: T) => void ) => ( - data: Partial, + data: Partial, ) => setState({ ...state, - action_data: { - ...state.action_data, - ...data, - }, + ...data, }) diff --git a/webapp/src/components/NodeEditor/OptionManager/index.ts b/webapp/src/components/NodeEditor/OptionManager/index.ts index 137e8a6..fe59f7f 100644 --- a/webapp/src/components/NodeEditor/OptionManager/index.ts +++ b/webapp/src/components/NodeEditor/OptionManager/index.ts @@ -3,9 +3,7 @@ import { AutomationNode, AutomationNodeTypes } from 'types/automations'; import { AutomationNodeSubtype } from 'types/automations'; import { getOptionManager } from './getOptionManager'; import { getNodeType, getSubTypeList } from 'utils/automations'; -import { AutomationCondition } from 'types/automations/conditions'; -import { AutomationAction } from 'types/automations/actions'; -import { AutomationTrigger } from 'types/automations/triggers'; +import { getNodeSubType } from '../constants'; export const useEditorNodeState = (node: AutomationNode) => { @@ -18,11 +16,9 @@ export const useEditorNodeState = (node: AutomationNode) => { }, [node]) const setState = (node: AutomationNode) => setAllState(({ node, isModified: true })); const originalNodeType = getNodeType(node); + const originalNodeSubType = getNodeSubType(node); const nodeType = getNodeType(state); - const subType: AutomationNodeSubtype = - nodeType === 'action' ? (state as AutomationAction).action : - nodeType === 'condition' ? (state as AutomationCondition).condition : - (state as AutomationTrigger).platform; + const subType = getNodeSubType(state); const subTypes = getSubTypeList(nodeType) const optionManager = getOptionManager(nodeType, subType as any); return { @@ -43,50 +39,16 @@ export const useEditorNodeState = (node: AutomationNode) => { }, setNodeType(newType: AutomationNodeTypes) { if (newType === originalNodeType) { - if (originalNodeType === 'action') { - setState({ - ...state, - $smType: 'action', - action: (node as any).action, - } as any) - } else if (originalNodeType === 'condition') { - setState({ - ...state, - condition: (node as any).condition - } as any) - } else { - setState({ - ...state, - platform: (node as any).platform - } as any) - } + setState(node) } else { - setState({ - ...state, - $smType: newType === 'trigger' ? undefined : newType, - ...getOptionManager(newType, getSubTypeList(newType)[0]).defaultState() - } as any) + setState(getOptionManager(newType, getSubTypeList(newType)[0]).defaultState()) } }, setSubType(newSubType: AutomationNodeSubtype) { - if (nodeType === 'action') { - setState({ - ...state, - action: newSubType, - ...getOptionManager(nodeType, newSubType as any).defaultState() - } as any) - } else if (nodeType === 'condition') { - setState({ - ...state, - condition: newSubType, - ...getOptionManager(nodeType, newSubType as any).defaultState() - } as any) + if (newSubType === originalNodeSubType) { + setState(node) } else { - setState({ - ...state, - platform: newSubType, - ...getOptionManager(nodeType, newSubType as any).defaultState() - } as any) + setState(getOptionManager(nodeType, newSubType as any).defaultState()) } } } diff --git a/webapp/src/components/NodeEditor/constants.ts b/webapp/src/components/NodeEditor/constants.ts index addfffd..2639c87 100644 --- a/webapp/src/components/NodeEditor/constants.ts +++ b/webapp/src/components/NodeEditor/constants.ts @@ -1,4 +1,5 @@ import { AutomationNodeSubtype, AutomationNodeTypes } from "types/automations"; +import { ActionType } from "types/automations/actions"; import { AutomationNode } from '../../types/automations/index'; @@ -54,12 +55,22 @@ export const getSubTypeList = ( export const getNodeSubType = ( node: AutomationNode ): AutomationNodeSubtype => { - if ('$smType' in node && node.$smType === 'action') { - return node.action as any; - } else if ('condition' in node) { + if ('condition' in node) { return node.condition as any; } else if ('platform' in node) { return node.platform as any; + } else if ('service' in node) { + return 'service' as any + } else if ('repeat' in node) { + return 'repeat' as any + } else if ('wait_template' in node) { + return 'wait' as any + } else if ('event' in node) { + return 'event' as any + } else if ('device_id' in node) { + return 'device' as any + } else if ('choose' in node) { + return 'choose' as any } else { throw new Error("Invalid node type!") } diff --git a/webapp/src/components/NodeEditor/index.stories.tsx b/webapp/src/components/NodeEditor/index.stories.tsx index 38a4ae1..07c56b4 100644 --- a/webapp/src/components/NodeEditor/index.stories.tsx +++ b/webapp/src/components/NodeEditor/index.stories.tsx @@ -14,13 +14,9 @@ export default { parameters: { actions: { argTypesRegex: '^on.*' } }, args: { node: { - $smType: 'action', - action: 'service', - action_data: { - 'data': {}, - 'service': '', - 'target': '', - } + 'data': {}, + 'service': '', + 'target': '', } } } as ComponentMeta diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts index 30ffadc..2d2e8cc 100644 --- a/webapp/src/types/automations/actions.ts +++ b/webapp/src/types/automations/actions.ts @@ -2,54 +2,57 @@ import { AutomationSequenceNode } from "."; import { AutomationTime } from "./common"; import { AutomationCondition } from "./conditions"; -interface AutomationActionNodeBase { - $smType: 'action'; - action: Name; - action_data: Data & { - alias?: string; - variables?: Record; - }; +type AutomationActionNodeBase = Data & { + alias?: string; + variables?: Record; } -export interface ServiceAction extends AutomationActionNodeBase<"service", { +export type ServiceAction = AutomationActionNodeBase<{ service: string; target: any; data: any; -}>{} -export interface RepeatAction extends AutomationActionNodeBase<"repeat", { +}> +export type RepeatAction = AutomationActionNodeBase<{ repeat: { count: number; sequence: AutomationAction[]; } -}>{} -export interface WaitAction extends AutomationActionNodeBase<"wait", { +}> +export type WaitAction = AutomationActionNodeBase<{ wait_template: string; timeout?: AutomationTime; continue_on_timeout?: boolean; -}>{} -export interface FireEventAction extends AutomationActionNodeBase<"event", { +}> +export type FireEventAction = AutomationActionNodeBase<{ event: string; event_data: any; -}>{} -export interface DeviceAction extends AutomationActionNodeBase<"device", { +}> +export type DeviceAction = AutomationActionNodeBase<{ type: string; device_id: string; entity_id?: string; domain: string; -}>{} -export interface ChooseAction extends AutomationActionNodeBase<"choose", { +}> +export type ChooseAction = AutomationActionNodeBase<{ choose: Array<{ conditions: AutomationCondition[]; sequence: AutomationSequenceNode[]; }>; default: AutomationSequenceNode[]; -}>{} -export type AutomationAction = +}> +export type AutomationAction = | ServiceAction | RepeatAction | WaitAction | FireEventAction | DeviceAction | ChooseAction -; -export type ActionType = AutomationAction['action']; + ; +export type ActionType = + | 'service' + | 'repeat' + | 'wait' + | 'event' + | 'device' + | 'choose' + ; diff --git a/webapp/src/utils/formatting.test.ts b/webapp/src/utils/formatting.test.ts index f27939e..6c431a8 100644 --- a/webapp/src/utils/formatting.test.ts +++ b/webapp/src/utils/formatting.test.ts @@ -10,18 +10,14 @@ test('converting time to a string', () => { test('using alias as description', () => { expect(getDescriptionFromAutomationNode({ - $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", - } + 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", } })).toEqual("Start Music In Kitchen") }) @@ -29,30 +25,22 @@ test('using alias as description', () => { test('get service action description', () => { expect(getDescriptionFromAutomationNode({ - $smType: "action", - action: "service", - action_data: { - service: 'media_player.play_media', - target: { - entity_id: "media_player.kitchen_dot" - }, - data: { - media_content_id: "Good Morning", - media_content_type: "SPOTIFY", - } + service: 'media_player.play_media', + target: { + entity_id: "media_player.kitchen_dot" + }, + data: { + media_content_id: "Good Morning", + media_content_type: "SPOTIFY", } })).toEqual("media_player.play_media") }) test('get repeat action description', () => { expect(getDescriptionFromAutomationNode({ - $smType: "action", - action: "repeat", - action_data: { - repeat: { - count: 10, - sequence: [] - } + repeat: { + count: 10, + sequence: [] } })).toEqual("Repeat 10") }) @@ -60,57 +48,35 @@ test('get repeat action description', () => { test('get wait action description', () => { expect(getDescriptionFromAutomationNode({ - $smType: "action", - action: "wait", - action_data: { - wait_template: "states(switch.kitchen_light) == 'on'" - } + wait_template: "states(switch.kitchen_light) == 'on'" })).toEqual("Wait on states(switch.kitchen_light) == 'on'") expect(getDescriptionFromAutomationNode({ - $smType: "action", - action: "wait", - action_data: { - wait_template: "states(switch.kitchen_light) == 'on'", - // timeout: '00:01:00', - timeout: { - 'minutes': 1, - } + wait_template: "states(switch.kitchen_light) == 'on'", + timeout: { + 'minutes': 1, } })).toEqual("Wait on states(switch.kitchen_light) == 'on' for 00:01:00:00") }) test('get fire event action description', () => { expect(getDescriptionFromAutomationNode({ - $smType: 'action', - action: 'event', - action_data: { - event: 'test_event', - event_data: {}, - } + event: 'test_event', + event_data: {}, })).toEqual("Trigger test_event") }) test('get device action description', () => { expect(getDescriptionFromAutomationNode({ - $smType: 'action', - action: 'device', - action_data: { - device_id: "12310das01231", - domain: "zwave_js", - type: "set_value" - } - + device_id: "12310das01231", + domain: "zwave_js", + type: "set_value" })).toEqual("set_value on 12310das01231") }) test('get choose action description', () => { expect(getDescriptionFromAutomationNode({ - $smType: 'action', - action: 'choose', - action_data: { - choose: [], - default: [], - } + choose: [], + default: [], })).toEqual("Choose") }) diff --git a/webapp/src/utils/formatting.ts b/webapp/src/utils/formatting.ts index 146a731..691152f 100644 --- a/webapp/src/utils/formatting.ts +++ b/webapp/src/utils/formatting.ts @@ -10,36 +10,13 @@ export const convertTimeToString = ({ }: AutomationTime) => [hours, minutes, seconds, milliseconds].map(n => String(n).padStart(2, '0')).join(':') export const getDescriptionFromAutomationNode = (node: AutomationNode): string => { - if ('$smType' in node) { - switch (node['$smType']) { - case "action": - if (node.action_data.alias) { - return node.action_data.alias; - } - switch (node.action) { - case 'service': - return node.action_data.service - case 'repeat': - return 'Repeat ' + node.action_data.repeat.count; - case 'wait': - let out = 'Wait on ' + node.action_data.wait_template; - if (node.action_data.timeout) { - out += (' for ' + convertTimeToString(node.action_data.timeout)) - } - return out; - case 'event': - return `Trigger ${node.action_data.event}` - case 'device': - return `${node.action_data.type} on ${node.action_data.device_id}` - case 'choose': - return "Choose" - } - } - return 'n/a' - } else if ('condition' in node) { - if (node.alias) { - return node.alias; - } + if (node.alias) { + return node.alias; + } + if ('platform' in node) { + return node.platform + } + if ('condition' in node) { if ( (node.condition === 'and') || (node.condition === 'or') || @@ -71,12 +48,30 @@ export const getDescriptionFromAutomationNode = ( return 'n/a' } } - } else { - if (node.alias) { - return node.alias; - } - return node.platform } + if ('service' in node) { + return node.service + } + if ('repeat' in node) { + return 'Repeat ' + node.repeat.count + } + if ('wait_template' in node) { + let out = 'Wait on ' + node.wait_template; + if (node.timeout) { + out += (' for ' + convertTimeToString(node.timeout)) + } + return out; + } + if ('event' in node) { + return `Trigger ${node.event}` + } + if ('device_id' in node) { + return `${node.type} on ${node.device_id}` + } + if ('choose' in node) { + return "Choose" + } + return 'n/a' } export const prettyEntityId = (entityId: string | string[]) => { diff --git a/webapp/src/utils/mocks.ts b/webapp/src/utils/mocks.ts index c1ba78d..0e56f42 100644 --- a/webapp/src/utils/mocks.ts +++ b/webapp/src/utils/mocks.ts @@ -38,18 +38,14 @@ export const createMockAuto = (tags: Record = {}): AutomationDat ] }, { - $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", - } + 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", } } ]