good enough node editor
This commit is contained in:
1 parent
54e628481c
commit
1643558240
20 files changed
+329
-345
No files matched your search
@@ -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<ChooseAction> {
|
||||
defaultState = () => ({
|
||||
export const ActionChooseState: OptionManager<ChooseAction> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'action',
|
||||
action: 'choose',
|
||||
action_data: {
|
||||
@@ -13,12 +13,12 @@ export default class ActionChooseState extends BaseOptionManager<ChooseAction> {
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { DeviceAction } from "types/automations/actions";
|
||||
import { BaseOptionManager, updateActionData } from './BaseOptionManager';
|
||||
import { OptionManager, updateActionData } from './OptionManager';
|
||||
|
||||
|
||||
export default class ActionDeviceState extends BaseOptionManager<DeviceAction> {
|
||||
defaultState = () => ({
|
||||
export const ActionDeviceState: OptionManager<DeviceAction> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'action',
|
||||
action: 'device',
|
||||
action_data: {
|
||||
@@ -14,16 +14,24 @@ export default class ActionDeviceState extends BaseOptionManager<DeviceAction> {
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { FireEventAction } from "types/automations/actions";
|
||||
import { OptionManager, updateActionData } from "./OptionManager";
|
||||
|
||||
|
||||
export const ActionEventState: OptionManager<FireEventAction> = {
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.action_data.alias ?? ""}
|
||||
onChange={alias => update({ alias })}
|
||||
/>
|
||||
<InputText
|
||||
textBoxFor="event"
|
||||
label="Event"
|
||||
value={state.action_data.event}
|
||||
onChange={event => update({ event })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Event Data"
|
||||
value={state.action_data.event_data}
|
||||
onChange={event_data => update({ event_data })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -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<RepeatAction> = {
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.action_data.alias ?? ""}
|
||||
onChange={alias => update({ alias })}
|
||||
/>
|
||||
<InputNumber
|
||||
label="How many times?"
|
||||
value={state.action_data.repeat.count}
|
||||
onChange={(count = 1) => update({ repeat: { ...state.action_data.repeat, count } })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Sequence"
|
||||
value={state.action_data.repeat.sequence ?? []}
|
||||
onChange={sequence => update({ repeat: { ...state.action_data.repeat, sequence } })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -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<ServiceAction> {
|
||||
defaultState = () => ({
|
||||
export const ActionCallServiceState: OptionManager<ServiceAction> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'action',
|
||||
action: 'service',
|
||||
action_data: {
|
||||
@@ -12,16 +12,17 @@ export default class ActionCallServiceState extends BaseOptionManager<ServiceAct
|
||||
target: "",
|
||||
data: {},
|
||||
}
|
||||
} as ServiceAction)
|
||||
isReady(state: ServiceAction): boolean {
|
||||
const { service, alias, target } = state.action_data;
|
||||
return service !== '' &&
|
||||
alias !== '' &&
|
||||
target !== ''
|
||||
}
|
||||
renderOptionList(state: ServiceAction): JSX.Element {
|
||||
}),
|
||||
isReady: ({
|
||||
action_data: { service, alias, target }
|
||||
}) => (
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import InputBoolean from "components/Inputs/InputBoolean";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { WaitAction } from "types/automations/actions";
|
||||
import { OptionManager, updateActionData } from './OptionManager';
|
||||
|
||||
|
||||
export const ActionWaitState: OptionManager<WaitAction> = {
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.action_data.alias}
|
||||
onChange={alias => update({ alias })}
|
||||
/>
|
||||
<InputText
|
||||
textBoxFor="template"
|
||||
label="Template"
|
||||
value={state.action_data.wait_template}
|
||||
onChange={wait_template => update({ wait_template })}
|
||||
/>
|
||||
{/* <InputNumber
|
||||
label="Timeout"
|
||||
value={state.action_data.timeout ?? 0}
|
||||
onChange={(timeout = 0) => update({ timeout })}
|
||||
/> */}
|
||||
<InputBoolean
|
||||
label="Continue on Timeout"
|
||||
value={state.action_data.continue_on_timeout ?? false}
|
||||
onChange={continue_on_timeout => update({ continue_on_timeout })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -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<LogicCondition> {
|
||||
defaultState = () => ({
|
||||
export const ConditionLogicState: OptionManager<AutomationCondition> = {
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={alias}
|
||||
onChange={a => this.setState({
|
||||
...state, condition_data: {
|
||||
alias: a, conditions
|
||||
}
|
||||
})}
|
||||
/>
|
||||
<InputList
|
||||
label="Condition"
|
||||
current={state.condition}
|
||||
options={['and', 'or', 'not']}
|
||||
onChange={(condition: any) => this.setState({ ...state, condition })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Sequence"
|
||||
value={conditions}
|
||||
onChange={c => this.setState({
|
||||
...state, condition_data: {
|
||||
alias, conditions: c
|
||||
}
|
||||
})}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}),
|
||||
isReady: _ => true,
|
||||
renderOptionList: (state, setState) => <div className="state-manager-options">
|
||||
<ConditionEditor
|
||||
condition={state}
|
||||
onUpdate={setState}
|
||||
onDelete={() => { }}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
@@ -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<NumericCondition> {
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={alias}
|
||||
onChange={alias => updateConditionData(this.setState)(state, {
|
||||
alias
|
||||
})}
|
||||
/>
|
||||
<InputText
|
||||
textBoxFor="entity_id"
|
||||
label="Entity ID"
|
||||
value={entity_id}
|
||||
onChange={entity_id => updateConditionData(this.setState)(state, {
|
||||
entity_id
|
||||
})}
|
||||
/>
|
||||
<InputText
|
||||
label="Template"
|
||||
value={value_template ?? ""}
|
||||
onChange={value_template => updateConditionData(this.setState)(state, {
|
||||
value_template
|
||||
})}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,10 @@
|
||||
import { BaseOptionManager } from "./BaseOptionManager";
|
||||
import { OptionManager } from "./OptionManager";
|
||||
|
||||
|
||||
export default class Generic extends BaseOptionManager<any> {
|
||||
defaultState = () => ({})
|
||||
isReady(state: any): boolean {
|
||||
return true;
|
||||
}
|
||||
renderOptionList(state: any): JSX.Element {
|
||||
return <div className="state-manager-options">
|
||||
<b>THIS IS NOT SUPPORTED YET!</b>
|
||||
</div>
|
||||
}
|
||||
export const Generic: OptionManager<any> = {
|
||||
defaultState: () => ({}),
|
||||
isReady: _ => true,
|
||||
renderOptionList: () => <div className="state-manager-options">
|
||||
<b>THIS IS NOT SUPPORTED YET!</b>
|
||||
</div>
|
||||
}
|
||||
+4
-6
@@ -1,11 +1,9 @@
|
||||
import { AutomationAction } from 'types/automations/actions';
|
||||
import { AutomationCondition } from 'types/automations/conditions';
|
||||
export abstract class BaseOptionManager<T> {
|
||||
abstract defaultState(): T ;
|
||||
abstract isReady(s: T): boolean;
|
||||
abstract renderOptionList(s: T): JSX.Element;
|
||||
|
||||
constructor(public setState: (s: T) => void) { }
|
||||
export interface OptionManager<T> {
|
||||
defaultState(): T ;
|
||||
isReady(s: T): boolean;
|
||||
renderOptionList(s: T, set: (s:T) => void): JSX.Element;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<FireEventScript> {
|
||||
defaultState = () => ({
|
||||
alias: "Fire Event",
|
||||
event: "",
|
||||
event_data: {},
|
||||
})
|
||||
isReady(state: FireEventScript): boolean {
|
||||
return state.alias !== '' &&
|
||||
state.event !== ""
|
||||
}
|
||||
renderOptionList(state: FireEventScript): JSX.Element {
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
onChange={alias => this.setState({ ...state, alias })}
|
||||
/>
|
||||
<InputText
|
||||
textBoxFor="event"
|
||||
label="Event"
|
||||
value={state.event}
|
||||
onChange={event => this.setState({ ...state, event })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Event Data"
|
||||
value={state.event_data}
|
||||
onChange={event_data => this.setState({ ...state, event_data })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -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<RepeatScript> {
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
onChange={alias => this.setState({ ...state, alias })}
|
||||
/>
|
||||
<InputNumber
|
||||
label="How many times?"
|
||||
value={state?.repeat?.count}
|
||||
onChange={count => this.setState({ ...state, repeat: { ...state.repeat, count } })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Sequence"
|
||||
value={state?.repeat?.sequence ?? {}}
|
||||
onChange={sequence => this.setState({ ...state, repeat: { ...state.repeat, sequence } })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -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<WaitScript> {
|
||||
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 <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
onChange={alias => this.setState({ ...state, alias })}
|
||||
/>
|
||||
<InputText
|
||||
textBoxFor="template"
|
||||
label="Template"
|
||||
value={state.wait_template}
|
||||
onChange={wait_template => this.setState({ ...state, wait_template })}
|
||||
/>
|
||||
<InputNumber
|
||||
label="Timeout"
|
||||
value={state.timeout ?? 0}
|
||||
onChange={timeout => this.setState({ ...state, timeout })}
|
||||
/>
|
||||
<InputBoolean
|
||||
label="Continue on Timeout"
|
||||
value={state.continue_on_timeout ?? false}
|
||||
onChange={continue_on_timeout => this.setState({ ...state, continue_on_timeout })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -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 = <T extends AutomationNodeTypes>(
|
||||
nodeType: T,
|
||||
nodeSubtype: AutomationNodeSubtype<T>,
|
||||
) : any => {
|
||||
) : OptionManager<any> => {
|
||||
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
|
||||
}
|
||||
@@ -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<any> = 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<typeof nodeType>[] {
|
||||
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 = <T extends AutomationNodeTypes>(nodeType: T): AutomationNodeSubtype<T>[] => {
|
||||
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 [];
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
@@ -34,12 +34,12 @@ export const NodeEditor: FC<Props> = ({
|
||||
options={allowedTypes}
|
||||
onChange={state.setNodeType}
|
||||
/> : <span>{state.nodeType}</span>}
|
||||
<InputList
|
||||
{state.nodeType !== 'condition' ? <InputList
|
||||
label={`${state.nodeType} type`}
|
||||
current={state.subType as any}
|
||||
options={state.subTypes}
|
||||
onChange={state.setSubType}
|
||||
/>
|
||||
/> : <></>}
|
||||
{state.renderOptionList()}
|
||||
</div>
|
||||
<div className={classes.footer}>
|
||||
|
||||
@@ -44,6 +44,7 @@ export const useNodeEditorStyles = createAppUseStyles<{
|
||||
},
|
||||
body: {
|
||||
overflow: 'auto',
|
||||
resize: 'auto',
|
||||
flexGrow: 1
|
||||
},
|
||||
footer: {
|
||||
|
||||
Reference in new issue
Block a user