diff --git a/webapp/src/components/ConditionEditor/style.ts b/webapp/src/components/ConditionEditor/style.ts index d4ee212..9ba960d 100644 --- a/webapp/src/components/ConditionEditor/style.ts +++ b/webapp/src/components/ConditionEditor/style.ts @@ -27,7 +27,6 @@ export const useStyles = createAppUseStyles<{ position: 'relative', display: 'flex', flexDirection: hasChildren? 'column' : 'row', - alignItems: hasChildren ? undefined : 'flex-start', }), title: ({hasChildren}) => ({ display: 'flex', @@ -35,6 +34,7 @@ export const useStyles = createAppUseStyles<{ borderRadius: 10, borderBottomRightRadius: 0, borderBottomLeftRadius: 0, + height: '100%', }), titleText: ({hasChildren}) => ({ background: theme.condition.primaryColor, diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx index d524603..d818a0c 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx @@ -1,11 +1,11 @@ import InputYaml from "components/Inputs/InputYaml"; import InputText from "components/Inputs/InputText"; import { ChooseAction } from "types/automations/actions"; -import { BaseOptionManager, updateActionData } from './BaseOptionManager'; +import { OptionManager, updateActionData } from './OptionManager'; -export default class ActionChooseState extends BaseOptionManager { - defaultState = () => ({ +export const ActionChooseState: OptionManager = { + defaultState: () => ({ $smType: 'action', action: 'choose', action_data: { @@ -13,12 +13,12 @@ export default class ActionChooseState extends BaseOptionManager { default: [], choose: [], } - } as ChooseAction) - isReady(state: ChooseAction): boolean { - return state.action_data.alias !== '' - } - renderOptionList(state: ChooseAction): JSX.Element { - const update = updateActionData(state, this.setState); + }), + isReady: ({ action_data: { alias } }) => { + return alias !== '' + }, + renderOptionList: (state, setState) => { + const update = updateActionData(state, setState); return
{ - defaultState = () => ({ +export const ActionDeviceState: OptionManager = { + defaultState: () => ({ $smType: 'action', action: 'device', action_data: { @@ -14,16 +14,24 @@ export default class ActionDeviceState extends BaseOptionManager { device_id: "", domain: "", } - } as DeviceAction) - isReady(state: DeviceAction): boolean { - return state.action_data.alias !== '' && - state.action_data.type !== "" && - state.action_data.entity_id !== "" && - state.action_data.device_id !== "" && - state.action_data.domain !== "" - } - renderOptionList(state: DeviceAction): JSX.Element { - const update = updateActionData(state, this.setState); + }), + isReady: ({ + action_data: { + alias, + type, + entity_id, + device_id, + domain, + } + }) => ( + alias !== '' && + type !== "" && + entity_id !== "" && + device_id !== "" && + domain !== "" + ), + renderOptionList: (state, setState) => { + const update = updateActionData(state, setState); return
= { + defaultState: () => ({ + $smType: "action", + action: 'event', + action_data: { + alias: "Fire Event", + event: "", + event_data: {}, + } + }), + isReady: ({ action_data: { alias, event } }) => ( + alias !== '' && + event !== "" + ), + renderOptionList: (state, setState) => { + const update = updateActionData(state, setState); + 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 new file mode 100644 index 0000000..93b0464 --- /dev/null +++ b/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx @@ -0,0 +1,47 @@ +import InputNumber from "components/Inputs/InputNumber"; +import InputText from "components/Inputs/InputText"; +import InputYaml from "components/Inputs/InputYaml"; +import { RepeatAction } from "types/automations/actions"; +import { OptionManager, updateActionData } from './OptionManager'; + + +export const ActionRepeatState: OptionManager = { + defaultState: () => ({ + $smType: 'action', + action: 'repeat', + action_data: { + alias: "Repeat", + repeat: { + count: 1, + sequence: [], + } + } + }), + isReady: ({ action_data: { + alias, + repeat, + } }) => ( + alias !== '' && + repeat?.sequence.length > 0 + ), + renderOptionList: (state, setState) => { + const update = updateActionData(state, setState); + return
+ update({ alias })} + /> + update({ repeat: { ...state.action_data.repeat, count } })} + /> + update({ repeat: { ...state.action_data.repeat, sequence } })} + /> +
+ } +} diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionService.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionService.tsx index 1e8640c..a9dfd6d 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionService.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionService.tsx @@ -1,10 +1,10 @@ import InputText from "components/Inputs/InputText"; import { ServiceAction } from "types/automations/actions"; -import { BaseOptionManager, updateActionData } from './BaseOptionManager'; +import { OptionManager, updateActionData } from './OptionManager'; -export default class ActionCallServiceState extends BaseOptionManager { - defaultState = () => ({ +export const ActionCallServiceState: OptionManager = { + defaultState: () => ({ $smType: 'action', action: 'service', action_data: { @@ -12,16 +12,17 @@ export default class ActionCallServiceState extends BaseOptionManager ( + service !== '' && + alias !== '' && + target !== '' + ), + renderOptionList: (state, setState) => { const { service, alias = "", target } = state.action_data; - const update = updateActionData(state, this.setState); + const update = updateActionData(state, setState); return
= { + defaultState: () => ({ + $smType: 'action', + action: 'wait', + action_data: { + alias: "Wait", + wait_template: "", + // timeout: 0, + continue_on_timeout: false, + } + }), + isReady: ({ + action_data: { alias, wait_template } + }) => ( + alias !== '' && + wait_template !== "" + ), + renderOptionList: (state, setState) => { + const update = updateActionData(state, setState); + return
+ update({ alias })} + /> + update({ wait_template })} + /> + {/* update({ timeout })} + /> */} + update({ continue_on_timeout })} + /> +
+ } +} diff --git a/webapp/src/components/NodeEditor/OptionManager/ConditionLogic.tsx b/webapp/src/components/NodeEditor/OptionManager/ConditionLogic.tsx index 897da83..afb16e7 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ConditionLogic.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ConditionLogic.tsx @@ -1,51 +1,23 @@ -import InputYaml from "components/Inputs/InputYaml"; -import InputText from "components/Inputs/InputText"; -import { BaseOptionManager } from "./BaseOptionManager"; -import { LogicCondition } from "types/automations/conditions"; -import InputList from "components/Inputs/InputList"; +import { AutomationCondition } from 'types/automations/conditions'; +import { ConditionEditor } from "components/ConditionEditor"; +import { OptionManager } from "./OptionManager"; -export default class ConditionLogicState extends BaseOptionManager { - defaultState = () => ({ +export const ConditionLogicState: OptionManager = { + defaultState: () => ({ $smType: 'condition', condition: 'and', condition_data: { alias: "", conditions: [], } - } as LogicCondition) - isReady(state: LogicCondition): boolean { - const { alias, conditions } = state.condition_data; - return alias !== '' && - conditions?.length > 0; - } - renderOptionList(state: LogicCondition): JSX.Element { - const { alias = "", conditions } = state.condition_data; - return
- this.setState({ - ...state, condition_data: { - alias: a, conditions - } - })} - /> - this.setState({ ...state, condition })} - /> - this.setState({ - ...state, condition_data: { - alias, conditions: c - } - })} - /> -
- } + }), + isReady: _ => true, + renderOptionList: (state, setState) =>
+ { }} + /> +
} diff --git a/webapp/src/components/NodeEditor/OptionManager/ConditionNumericState.tsx b/webapp/src/components/NodeEditor/OptionManager/ConditionNumericState.tsx deleted file mode 100644 index 91e3d77..0000000 --- a/webapp/src/components/NodeEditor/OptionManager/ConditionNumericState.tsx +++ /dev/null @@ -1,52 +0,0 @@ -import InputText from "components/Inputs/InputText"; -import { BaseOptionManager, updateConditionData } from './BaseOptionManager'; -import { NumericCondition } from "types/automations/conditions"; - - -export default class ConditionNumStateState extends BaseOptionManager { - defaultState = () => ({ - $smType: 'condition', - condition_data: { - alias: "", - entity_id: '', - // above: '', - // below: '', - // attribute: '', - value_template: '', - }, - condition: 'numeric_state', - } as NumericCondition) - isReady(state: NumericCondition): boolean { - const { alias, entity_id, value_template } = state.condition_data; - return alias !== '' && - entity_id !== '' && - value_template !== ''; - } - renderOptionList(state: NumericCondition): JSX.Element { - const { alias = "", entity_id, value_template } = state.condition_data; - return
- updateConditionData(this.setState)(state, { - alias - })} - /> - updateConditionData(this.setState)(state, { - entity_id - })} - /> - updateConditionData(this.setState)(state, { - value_template - })} - /> -
- } -} diff --git a/webapp/src/components/NodeEditor/OptionManager/Generic.tsx b/webapp/src/components/NodeEditor/OptionManager/Generic.tsx index 46e8fac..bef1e30 100644 --- a/webapp/src/components/NodeEditor/OptionManager/Generic.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/Generic.tsx @@ -1,14 +1,10 @@ -import { BaseOptionManager } from "./BaseOptionManager"; +import { OptionManager } from "./OptionManager"; -export default class Generic extends BaseOptionManager { - defaultState = () => ({}) - isReady(state: any): boolean { - return true; - } - renderOptionList(state: any): JSX.Element { - return
- THIS IS NOT SUPPORTED YET! -
- } +export const Generic: OptionManager = { + defaultState: () => ({}), + isReady: _ => true, + renderOptionList: () =>
+ THIS IS NOT SUPPORTED YET! +
} diff --git a/webapp/src/components/NodeEditor/OptionManager/BaseOptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts similarity index 74% rename from webapp/src/components/NodeEditor/OptionManager/BaseOptionManager.ts rename to webapp/src/components/NodeEditor/OptionManager/OptionManager.ts index 0e2b08f..bc17946 100644 --- a/webapp/src/components/NodeEditor/OptionManager/BaseOptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts @@ -1,11 +1,9 @@ import { AutomationAction } from 'types/automations/actions'; import { AutomationCondition } from 'types/automations/conditions'; -export abstract class BaseOptionManager { - abstract defaultState(): T ; - abstract isReady(s: T): boolean; - abstract renderOptionList(s: T): JSX.Element; - - constructor(public setState: (s: T) => void) { } +export interface OptionManager { + defaultState(): T ; + isReady(s: T): boolean; + renderOptionList(s: T, set: (s:T) => void): JSX.Element; } diff --git a/webapp/src/components/NodeEditor/OptionManager/ScriptEvent.tsx b/webapp/src/components/NodeEditor/OptionManager/ScriptEvent.tsx deleted file mode 100644 index e5b0887..0000000 --- a/webapp/src/components/NodeEditor/OptionManager/ScriptEvent.tsx +++ /dev/null @@ -1,39 +0,0 @@ -import InputBoolean from "../../components/InputBoolean"; -import InputYaml from "../../components/InputYaml"; -import InputNumber from "../../components/InputNumber"; -import InputText from "../../components/InputText"; -import { FireEventScript } from "../types/script"; -import { BaseOptionManager } from "./BaseOptionManager"; - - -export default class ScriptEventState extends BaseOptionManager { - defaultState = () => ({ - alias: "Fire Event", - event: "", - event_data: {}, - }) - isReady(state: FireEventScript): boolean { - return state.alias !== '' && - state.event !== "" - } - renderOptionList(state: FireEventScript): JSX.Element { - return
- this.setState({ ...state, alias })} - /> - this.setState({ ...state, event })} - /> - this.setState({ ...state, event_data })} - /> -
- } -} diff --git a/webapp/src/components/NodeEditor/OptionManager/ScriptRepeat.tsx b/webapp/src/components/NodeEditor/OptionManager/ScriptRepeat.tsx deleted file mode 100644 index 31d32c8..0000000 --- a/webapp/src/components/NodeEditor/OptionManager/ScriptRepeat.tsx +++ /dev/null @@ -1,39 +0,0 @@ -import InputNumber from "../../components/InputNumber"; -import InputText from "../../components/InputText"; -import InputYaml from "../../components/InputYaml"; -import { RepeatScript } from "../types/script"; -import { BaseOptionManager } from "./BaseOptionManager"; - - -export default class ScriptRepeatState extends BaseOptionManager { - defaultState = () => ({ - alias: "Repeat", - repeat: { - count: 1, - sequence: [], - } - }) - isReady(state: RepeatScript): boolean { - return state.alias !== '' && - state?.repeat?.sequence.length > 0 - } - renderOptionList(state: RepeatScript): JSX.Element { - return
- this.setState({ ...state, alias })} - /> - this.setState({ ...state, repeat: { ...state.repeat, count } })} - /> - this.setState({ ...state, repeat: { ...state.repeat, sequence } })} - /> -
- } -} diff --git a/webapp/src/components/NodeEditor/OptionManager/ScriptWait.tsx b/webapp/src/components/NodeEditor/OptionManager/ScriptWait.tsx deleted file mode 100644 index a01aa3a..0000000 --- a/webapp/src/components/NodeEditor/OptionManager/ScriptWait.tsx +++ /dev/null @@ -1,44 +0,0 @@ -import InputBoolean from "../../components/InputBoolean"; -import InputNumber from "../../components/InputNumber"; -import InputText from "../../components/InputText"; -import { WaitScript } from "../types/script"; -import { BaseOptionManager } from "./BaseOptionManager"; - - -export default class ScriptWaitState extends BaseOptionManager { - defaultState = () => ({ - alias: "Wait", - wait_template: "", - timeout: 0, - continue_on_timeout: false, - }) - isReady(state: WaitScript): boolean { - return state.alias !== '' && - state.wait_template !== "" - } - renderOptionList(state: WaitScript): JSX.Element { - return
- this.setState({ ...state, alias })} - /> - this.setState({ ...state, wait_template })} - /> - this.setState({ ...state, timeout })} - /> - this.setState({ ...state, continue_on_timeout })} - /> -
- } -} diff --git a/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts index 7e8d045..c1406ac 100644 --- a/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts @@ -1,36 +1,38 @@ -import { AutomationNodeTypes, AutomationNodeSubtype, AutomationNode } from "types/automations" -import ActionChooseState from "./ActionChoose"; -import ActionDeviceState from "./ActionDevice"; -import ActionCallServiceState from "./ActionService" -import { BaseOptionManager } from "./BaseOptionManager"; -import Generic from "./Generic"; +import { AutomationNodeTypes, AutomationNodeSubtype } from "types/automations" +import { ActionChooseState } from "./ActionChoose"; +import { ActionDeviceState } from "./ActionDevice"; +import { ActionCallServiceState } from "./ActionService"; +import { OptionManager } from "./OptionManager"; +import { ConditionLogicState } from "./ConditionLogic"; +import { Generic } from "./Generic"; +import { ActionRepeatState } from "./ActionRepeat"; +import { ActionWaitState } from "./ActionWait"; +import { ActionEventState } from "./ActionEvent"; + export const getOptionManager = ( nodeType: T, nodeSubtype: AutomationNodeSubtype, -) : any => { +) : OptionManager => { if ( nodeType === 'action') { switch (nodeSubtype) { + case "choose": + return ActionChooseState; + case "device": + return ActionDeviceState; + case 'event': + return ActionEventState; + case "repeat": + return ActionRepeatState; case 'service': return ActionCallServiceState; - // case "Repeat": - // return ScriptRepeatState; - // case "Wait": - // return ScriptWaitState; - // case "Event": - // return ScriptEventState; - case "device": - return ActionDeviceState; - case "choose": - return ActionChooseState; + case "wait": + return ActionWaitState; } } - // if (nodeType === 'condition') { - // switch (nodeSubtype) { - // case 'Logic': - // return ConditionLogicState - // } - // } + if (nodeType === 'condition') { + return ConditionLogicState + } return Generic } diff --git a/webapp/src/components/NodeEditor/OptionManager/index.ts b/webapp/src/components/NodeEditor/OptionManager/index.ts index 0878377..39b09f3 100644 --- a/webapp/src/components/NodeEditor/OptionManager/index.ts +++ b/webapp/src/components/NodeEditor/OptionManager/index.ts @@ -1,7 +1,6 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { AutomationNode, AutomationNodeTypes } from 'types/automations'; import { AutomationNodeSubtype } from 'types/automations'; -import { BaseOptionManager } from './BaseOptionManager'; import { getOptionManager } from './getOptionManager'; @@ -10,67 +9,27 @@ export const useEditorNodeState = (node: AutomationNode) => { const nodeType: AutomationNodeTypes = state.$smType ?? 'trigger'; const subType: AutomationNodeSubtype = state.$smType === 'action' ? state.action : - state.$smType === 'condition' ? state.condition : - state.platform; - const optionManager: BaseOptionManager = new (getOptionManager(nodeType, subType as any))(setState); + state.$smType === 'condition' ? state.condition : + state.platform; + const subTypes = getSubTypeList(nodeType) + const optionManager = getOptionManager(nodeType, subType as any); return { nodeType, subType, + subTypes, renderOptionList() { - return optionManager.renderOptionList(state); + return optionManager.renderOptionList(state, setState); }, isReady() { return optionManager.isReady(state); }, - get subTypes(): AutomationNodeSubtype[] { - switch (nodeType) { - case 'action': - return [ - "service", - "repeat", - "wait", - "device", - "choose", - ] - case "condition": - return [ - 'or', - 'and', - 'not', - 'numeric_state', - 'state', - 'template', - 'time', - 'trigger', - 'zone', - ] - case 'trigger': - return [ - 'event', - 'homeassistant', - 'mqtt', - 'numeric_state', - 'state', - 'tag', - 'template', - 'time', - 'time_pattern', - 'webhook', - 'zone', - 'device', - ] - default: - return []; - } - }, - setNodeType(newType: AutomationNodeTypes) { if (newType === (node.$smType ?? 'trigger')) { if (node.$smType === 'action') { setState({ ...state, $smType: 'action', - action: node.action + action: node.action, } as any) } else if (node.$smType === 'condition') { setState({ @@ -89,6 +48,7 @@ export const useEditorNodeState = (node: AutomationNode) => { setState({ ...state, $smType: newType === 'trigger' ? undefined : newType, + ...getOptionManager(newType, getSubTypeList(newType)[0]).defaultState() } as any) } }, @@ -97,18 +57,64 @@ export const useEditorNodeState = (node: AutomationNode) => { setState({ ...state, action: newSubType, + ...getOptionManager(state.$smType, newSubType as any).defaultState() } as any) } else if (state.$smType === 'condition') { setState({ ...state, condition: newSubType, + ...getOptionManager(state.$smType, newSubType as any).defaultState() } as any) } else { setState({ ...state, platform: newSubType, + ...getOptionManager(state.$smType, newSubType as any).defaultState() } as any) } } } } + + +const getSubTypeList = (nodeType: T): AutomationNodeSubtype[] => { + switch (nodeType) { + case 'action': + return [ + "service", + "repeat", + "wait", + "device", + "choose", + ] as any + case "condition": + return [ + 'or', + 'and', + 'not', + 'numeric_state', + 'state', + 'template', + 'time', + 'trigger', + 'zone', + ]as any + case 'trigger': + return [ + 'event', + 'homeassistant', + 'mqtt', + 'numeric_state', + 'state', + 'tag', + 'template', + 'time', + 'time_pattern', + 'webhook', + 'zone', + 'device', + ]as any + default: + return []; + } +} diff --git a/webapp/src/components/NodeEditor/index.stories.tsx b/webapp/src/components/NodeEditor/index.stories.tsx index eeb727e..d4a7fd0 100644 --- a/webapp/src/components/NodeEditor/index.stories.tsx +++ b/webapp/src/components/NodeEditor/index.stories.tsx @@ -2,6 +2,7 @@ import React from "react"; import { ComponentMeta, ComponentStory } from '@storybook/react'; import { NodeEditor } from "."; import { usePageTheme } from "styles/page"; +import { AutomationCondition } from "types/automations/conditions"; export default { @@ -35,3 +36,33 @@ SingleOption.args = { ...Action.args, allowedTypes: ['action'], } +export const Condition = Action.bind({}) +Condition.args = { + ...Condition.args, + node: { + "$smType": "condition", + "condition": "and", + "condition_data": { + "conditions": [ + { + "$smType": "condition", + "condition": "numeric_state", + "condition_data": { + "entity_id": [ + "sensor.kitchen_humidity" + ], + "conditions": [], + "above": "40" + } + }, + { + "$smType": "condition", + "condition": "template", + "condition_data": { + "value_template": "states('switch.kitchen') == 'on'" + } + } + ] + } + } as AutomationCondition, +} diff --git a/webapp/src/components/NodeEditor/index.tsx b/webapp/src/components/NodeEditor/index.tsx index 7f7ac32..8fa4ce0 100644 --- a/webapp/src/components/NodeEditor/index.tsx +++ b/webapp/src/components/NodeEditor/index.tsx @@ -34,12 +34,12 @@ export const NodeEditor: FC = ({ options={allowedTypes} onChange={state.setNodeType} /> : {state.nodeType}} - + /> : <>} {state.renderOptionList()}
diff --git a/webapp/src/components/NodeEditor/styles.ts b/webapp/src/components/NodeEditor/styles.ts index 281cb7a..a27724d 100644 --- a/webapp/src/components/NodeEditor/styles.ts +++ b/webapp/src/components/NodeEditor/styles.ts @@ -44,6 +44,7 @@ export const useNodeEditorStyles = createAppUseStyles<{ }, body: { overflow: 'auto', + resize: 'auto', flexGrow: 1 }, footer: { diff --git a/webapp/src/utils/formatting.ts b/webapp/src/utils/formatting.ts index 48fb8d2..280abba 100644 --- a/webapp/src/utils/formatting.ts +++ b/webapp/src/utils/formatting.ts @@ -10,7 +10,7 @@ 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) { + if (node.$smType) { switch (node['$smType']) { case "action": if (node.action_data.alias) { @@ -32,7 +32,9 @@ export const getDescriptionFromAutomationNode = ( case 'device': return `${node.action_data.type} on ${node.action_data.device_id}` case 'choose': - return "Choose" + return "Choose" + default: + return 'n/a' } case "condition": if (node.condition_data.alias) { @@ -64,7 +66,9 @@ export const getDescriptionFromAutomationNode = ( case "state": return `${node.condition_data.entity_id} is '${node.condition_data.state}'` case 'template': - return node.condition_data.value_template + return node.condition_data.value_template + default: + return 'n/a' } } }