From 32fd7f2dffc8368eaecc1df31b0524e0e0c3e4b3 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Fri, 29 Oct 2021 12:39:31 -0400 Subject: [PATCH] added state modifier --- .../automation/StateManager/BaseManager.ts | 17 --- web/src/automation/StateManager/BaseState.ts | 12 ++ web/src/automation/StateManager/Generic.tsx | 28 +++++ .../automation/StateManager/ScriptService.tsx | 80 +++++++------ .../automation/StateManager/getBaseState.ts | 17 +++ web/src/automation/StateManager/index.ts | 32 +++++ web/src/automation/types/condition.ts | 12 ++ web/src/automation/types/index.ts | 17 ++- web/src/automation/types/script.ts | 9 ++ web/src/automation/types/triggers.ts | 17 ++- web/src/automation/useNodeState.ts | 112 ------------------ web/src/automation/utils.ts | 2 +- .../AutomationEditor/NewNodeModal.tsx | 18 ++- web/src/components/AutomationEditor/index.tsx | 8 +- 14 files changed, 199 insertions(+), 182 deletions(-) delete mode 100644 web/src/automation/StateManager/BaseManager.ts create mode 100644 web/src/automation/StateManager/BaseState.ts create mode 100644 web/src/automation/StateManager/Generic.tsx create mode 100644 web/src/automation/StateManager/getBaseState.ts create mode 100644 web/src/automation/StateManager/index.ts delete mode 100644 web/src/automation/useNodeState.ts diff --git a/web/src/automation/StateManager/BaseManager.ts b/web/src/automation/StateManager/BaseManager.ts deleted file mode 100644 index 594af9a..0000000 --- a/web/src/automation/StateManager/BaseManager.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { useState } from "react"; - - - -export abstract class BaseStateManager { - abstract defaultState(): T ; - abstract isReady(): boolean; - abstract renderOptionList(): JSX.Element; - - public state: T; - public setState: (s:T) => void; - - constructor(){ - [this.state, this.setState] = useState(this.defaultState()); - } - -} \ No newline at end of file diff --git a/web/src/automation/StateManager/BaseState.ts b/web/src/automation/StateManager/BaseState.ts new file mode 100644 index 0000000..8f4e467 --- /dev/null +++ b/web/src/automation/StateManager/BaseState.ts @@ -0,0 +1,12 @@ +import { useState } from "react"; + + + +export abstract class BaseState { + abstract defaultState(): T ; + abstract isReady(s: T): boolean; + abstract renderOptionList(s: T): JSX.Element; + + constructor(public setState: (s: T) => void) {} + +} \ No newline at end of file diff --git a/web/src/automation/StateManager/Generic.tsx b/web/src/automation/StateManager/Generic.tsx new file mode 100644 index 0000000..233e6bd --- /dev/null +++ b/web/src/automation/StateManager/Generic.tsx @@ -0,0 +1,28 @@ +import InputWrapper from "../../components/InputWrapper"; +import { ServiceScript } from "../types/script"; +import { BaseState } from "./BaseState"; + + +export default class Generic extends BaseState<{alias: string}> { + defaultState = () => ({ + alias: "Unknown", + }) + isReady(state: ServiceScript): boolean { + return true; + } + renderOptionList(state: ServiceScript): JSX.Element { + return
+ + { + e.preventDefault(); + this.setState({ + ...state, + alias: e.target.value, + }) + }}/> + +
+ THIS IS NOT SUPPORTED YET! +
+ } +} \ No newline at end of file diff --git a/web/src/automation/StateManager/ScriptService.tsx b/web/src/automation/StateManager/ScriptService.tsx index 50061eb..fa3f4a0 100644 --- a/web/src/automation/StateManager/ScriptService.tsx +++ b/web/src/automation/StateManager/ScriptService.tsx @@ -1,47 +1,49 @@ +import InputWrapper from "../../components/InputWrapper"; import { ServiceScript } from "../types/script"; -import { BaseStateManager } from "./BaseManager"; +import { BaseState } from "./BaseState"; -export class ScriptCallService extends BaseStateManager { - defaultState(): ServiceScript { - return { - service: "", - alias: "Call Service", - target: "", - data: {}, - } +export default class ScriptCallServiceState extends BaseState { + defaultState = () => ({ + service: "", + alias: "Call Service", + target: "", + data: {}, + }) + isReady(state: ServiceScript): boolean { + return state.service !== '' && + state.alias !== '' && + state.target !== '' } - isReady(): boolean { - return this.state.service !== '' && - this.state.alias !== '' && - this.state.target !== '' - } - renderOptionList(): JSX.Element { + renderOptionList(state: ServiceScript): JSX.Element { return
- - { - e.preventDefault(); - this.setState({ - ...this.state, - alias: e.target.value, - }) - }}/> - - { - e.preventDefault(); - this.setState({ - ...this.state, - service: e.target.value, - }) - }}/> - - { - e.preventDefault(); - this.setState({ - ...this.state, - target: e.target.value, - }) - }}/> + + { + e.preventDefault(); + this.setState({ + ...state, + alias: e.target.value, + }) + }}/> + + + { + e.preventDefault(); + this.setState({ + ...state, + service: e.target.value, + }) + }}/> + + + { + e.preventDefault(); + this.setState({ + ...state, + target: e.target.value, + }) + }}/> +
} } \ No newline at end of file diff --git a/web/src/automation/StateManager/getBaseState.ts b/web/src/automation/StateManager/getBaseState.ts new file mode 100644 index 0000000..577f035 --- /dev/null +++ b/web/src/automation/StateManager/getBaseState.ts @@ -0,0 +1,17 @@ +import { AutomationNode, AutomationNodeType, NodeSubType } from "../types" +import { BaseState } from "./BaseState" +import ScriptCallServiceState from "./ScriptService" + + +export const getBaseState = ( + node_type: T, + node_subtype: NodeSubType +) : any => { + if ( node_type === 'action') { + switch (node_subtype) { + case 'Service': + return ScriptCallServiceState + } + } + throw new Error(`Invalid or Not implemented node type ${node_type} -> ${node_subtype}`) +} \ No newline at end of file diff --git a/web/src/automation/StateManager/index.ts b/web/src/automation/StateManager/index.ts new file mode 100644 index 0000000..9e41f6d --- /dev/null +++ b/web/src/automation/StateManager/index.ts @@ -0,0 +1,32 @@ +import { useState } from "react"; +import { AutomationNodeType, NodeSubType } from "../types"; +import { BaseState } from "./BaseState"; +import Generic from "./Generic"; +import { getBaseState } from "./getBaseState"; + +export default class StateManager { + public state: any; + private setState: (s: any) => void; + private baseState: BaseState; + + constructor( + public nodeType: AutomationNodeType, + public nodeSubtype: NodeSubType + ){ + [this.state, this.setState] = useState({}); + this.baseState = this.swapBaseState(this.nodeType, this.nodeSubtype); + } + swapBaseState(nodeType: AutomationNodeType, nodeSubtype: NodeSubType) { + try { + this.baseState = new (getBaseState(nodeType, nodeSubtype))(this.setState); + } catch (error) { + this.baseState = new Generic(this.setState); + } + this.nodeType = nodeType; + this.nodeSubtype = nodeSubtype; + return this.baseState; + } + renderOptionList() { + return this.baseState.renderOptionList(this.state) + } +} \ No newline at end of file diff --git a/web/src/automation/types/condition.ts b/web/src/automation/types/condition.ts index 1c4d4dd..37c6f9d 100644 --- a/web/src/automation/types/condition.ts +++ b/web/src/automation/types/condition.ts @@ -56,3 +56,15 @@ export type AutomationCondition = | ZoneCondition | GenericCondition ; +export const conditionTypes = [ + 'And' , + 'Or' , + 'Not', + 'Numeric State', + 'State', + 'Template', + 'Time', + 'Trigger', + 'Zone', +] as const; +export type ConditionType = typeof conditionTypes[number]; \ No newline at end of file diff --git a/web/src/automation/types/index.ts b/web/src/automation/types/index.ts index 0a0e2a8..7be10fd 100644 --- a/web/src/automation/types/index.ts +++ b/web/src/automation/types/index.ts @@ -1,6 +1,6 @@ -import { AutomationCondition } from "./condition"; -import { AutomationScript } from "./script"; -import { AutomationTrigger } from "./triggers"; +import { AutomationCondition, ConditionType, conditionTypes } from "./condition"; +import { AutomationScript, ScriptType, scriptTypes } from "./script"; +import { AutomationTrigger, TriggerType, triggerTypes } from "./triggers"; export interface Automation { id: string; @@ -19,3 +19,14 @@ export type AutomationAction = AutomationScript | AutomationCondition; export type AutomationNode = AutomationNodeTypes[T][number]; +export type NodeSubType = + T extends 'action' ? ScriptType : + T extends 'condition' ? ConditionType : + T extends 'trigger' ? TriggerType : + never; + +export const typeList = { + 'action': scriptTypes, + 'condition': conditionTypes, + 'trigger': triggerTypes, +} as const; \ No newline at end of file diff --git a/web/src/automation/types/script.ts b/web/src/automation/types/script.ts index 26e6d29..0674f6c 100644 --- a/web/src/automation/types/script.ts +++ b/web/src/automation/types/script.ts @@ -46,3 +46,12 @@ export type AutomationScript = | DeviceScript | ChooseScript ; +export const scriptTypes = [ + 'Service', + 'Repeat', + 'Wait', + 'Event', + 'Device', + 'Choose' +] as const; +export type ScriptType = typeof scriptTypes[number]; \ No newline at end of file diff --git a/web/src/automation/types/triggers.ts b/web/src/automation/types/triggers.ts index 9d76c20..3e23df3 100644 --- a/web/src/automation/types/triggers.ts +++ b/web/src/automation/types/triggers.ts @@ -87,4 +87,19 @@ export type AutomationTrigger = | AutomationTriggerZone | AutomationTriggerDevice | AutomationTriggerGeneric -; \ No newline at end of file +; +export const triggerTypes = [ + 'Event', + 'Home Assistant', + 'MQTT', + 'Numeric State', + 'State', + 'Tag', + 'Template', + 'Time', + 'Time Pattern', + 'Webhook', + 'Zone', + 'Device', +] as const; +export type TriggerType = typeof triggerTypes[number]; \ No newline at end of file diff --git a/web/src/automation/useNodeState.ts b/web/src/automation/useNodeState.ts deleted file mode 100644 index 71c4225..0000000 --- a/web/src/automation/useNodeState.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { BaseStateManager } from "./StateManager/BaseManager"; -import { ScriptCallService } from "./StateManager/ScriptService"; -import { AutomationNode, AutomationNodeType } from "./types"; - -export const triggerTypes = [ - 'Event', - 'Home Assistant', - 'MQTT', - 'Numeric State', - 'State', - 'Tag', - 'Template', - 'Time', - 'Time Pattern', - 'Webhook', - 'Zone', - 'Device', -] as const; -export type TriggerType = typeof triggerTypes[number]; -export const conditionTypes = [ - 'And' , - 'Or' , - 'Not', - 'Numeric State', - 'State', - 'Template', - 'Time', - 'Trigger', - 'Zone', -] as const; -export type ConditionType = typeof conditionTypes[number]; -export const scriptTypes = [ - 'Service', - 'Repeat', - 'Wait', - 'Event', - 'Device', - 'Choose' -] as const; -export type ScriptType = typeof scriptTypes[number]; -export type NodeStateType = - T extends 'action' ? ScriptType : - T extends 'condition' ? ConditionType : - T extends 'trigger' ? TriggerType : - never; - -export const typeList = { - 'action': scriptTypes, - 'condition': conditionTypes, - 'trigger': triggerTypes, -} as const; - -export const useNewNodeState = ( - node_type: T, - node_subtype: NodeStateType -) : BaseStateManager> => { - if ( node_type === 'action') { - switch (node_subtype) { - case 'Service': - return new ScriptCallService() as any; - // case 'Repeat': - // return { - // repeat: { - // count: 0, - // sequence: [], - // } - // } - // case 'Wait': - // return { - // wait_template: "" - // } - // case 'Event': - // return { - // event: "", - // event_data: {} - // } - // case "Device": - // return { - // type: "", - // device_id: "", - // entity_id: "", - // domain: "", - // } - // case "Choose": - // return { - // choose: [], - // default: [], - // } - } - } - // else if (node_type === "condition") { - // if (node_subtype in ['And', 'Or', 'Noe']) { - // return { - // condition: node_subtype.toLowerCase(), - // conditions: [], - // } - // } else if (node_subtype === 'Numeric State') { - // return { - // condition: "numeric_state", - // entity_id: "" - // } - // } else if (node_subtype === 'State') { - // return { - // condition: "state", - // entity_id: "", - // state: "", - // } - // } - // } - - throw new Error(`Invalid or Not implemented node type ${node_type} -> ${node_subtype}`) -} \ No newline at end of file diff --git a/web/src/automation/utils.ts b/web/src/automation/utils.ts index eb78d9b..4f962d4 100644 --- a/web/src/automation/utils.ts +++ b/web/src/automation/utils.ts @@ -1,7 +1,7 @@ import { AutomationNode } from "./types"; -export const getNameFromAction = (action: AutomationNode): string => { +export const getNameFromAction = (action: AutomationNode): string => { if(typeof action === 'string') { return action; } diff --git a/web/src/components/AutomationEditor/NewNodeModal.tsx b/web/src/components/AutomationEditor/NewNodeModal.tsx index 6cc27a8..6eb0c50 100644 --- a/web/src/components/AutomationEditor/NewNodeModal.tsx +++ b/web/src/components/AutomationEditor/NewNodeModal.tsx @@ -1,6 +1,6 @@ -import { useState } from "react"; -import { AutomationNodeType, AutomationNodeTypes } from "../../automation/types"; -import { typeList } from "../../automation/useNodeState"; +import { useState, useEffect } from "react"; +import StateManager from "../../automation/StateManager"; +import { AutomationNodeType, AutomationNodeTypes, NodeSubType, typeList } from "../../automation/types"; import InputList from "../InputList"; import InputWrapper from "../InputWrapper"; @@ -14,7 +14,14 @@ export default function NewNodeModal({ onClose, onCreate, }: Props) { - const [selectedSubType, setSubType] = useState(typeList[node_type][0]); + const [selectedSubType, setSubType] = useState(typeList[node_type][0]); + const stateManager = new StateManager(node_type, selectedSubType); + + useEffect(() => { + if (stateManager.nodeType !== node_type || stateManager.nodeSubtype !== selectedSubType) { + stateManager.swapBaseState(node_type, selectedSubType) + } + }, [node_type, selectedSubType]); return
onClose()}>
@@ -22,8 +29,9 @@ export default function NewNodeModal({
Create {node_type[0].toUpperCase()}{node_type.slice(1)}
- setSubType(n)}/> + setSubType(n as NodeSubType)}/> + {stateManager.renderOptionList()}
diff --git a/web/src/components/AutomationEditor/index.tsx b/web/src/components/AutomationEditor/index.tsx index b94f3a3..86c1d31 100644 --- a/web/src/components/AutomationEditor/index.tsx +++ b/web/src/components/AutomationEditor/index.tsx @@ -14,14 +14,14 @@ export default function AutomationEditor({ automation }: Props) { // helper state functions const addNode = (node_type: T, node: types.AutomationNodeTypes[T]) => { - const nodes: types.AutomationNode[] = state[node_type]; + const nodes: types.AutomationNode[] = state[node_type]; setState({ ...state, [node_type]: nodes.concat([node as any]) }); } const removeNode = (node_type: T, node_id: number) => { - const nodes: types.AutomationNode[] = state[node_type]; + const nodes: types.AutomationNode[] = state[node_type]; setState({ ...state, [node_type]: nodes @@ -35,8 +35,8 @@ export default function AutomationEditor({ automation }: Props) { }); } // helper element functions - const makeChild = (node_type: types.AutomationNodeType) => { - const nodes: types.AutomationNode[] = state[node_type]; + const makeChild = (node_type: T) => { + const nodes: types.AutomationNode[] = state[node_type]; return [
{state[node_type].map((n, i) => (