moved old state handler code
This commit is contained in:
1 parent
d0bc01d2f1
commit
54e628481c
19 files changed
+642
-55
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { ChooseAction } from "types/automations/actions";
|
||||
import { BaseOptionManager, updateActionData } from './BaseOptionManager';
|
||||
|
||||
|
||||
export default class ActionChooseState extends BaseOptionManager<ChooseAction> {
|
||||
defaultState = () => ({
|
||||
$smType: 'action',
|
||||
action: 'choose',
|
||||
action_data: {
|
||||
alias: "Choose",
|
||||
default: [],
|
||||
choose: [],
|
||||
}
|
||||
} as ChooseAction)
|
||||
isReady(state: ChooseAction): boolean {
|
||||
return state.action_data.alias !== ''
|
||||
}
|
||||
renderOptionList(state: ChooseAction): JSX.Element {
|
||||
const update = updateActionData(state, this.setState);
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.action_data.alias ?? ""}
|
||||
onChange={alias => update({ alias })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Choose Sequence"
|
||||
value={state.action_data.choose}
|
||||
onChange={choose => update({ choose })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Default Sequence"
|
||||
value={state.action_data.default}
|
||||
onChange={d => update({ default: d })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { DeviceAction } from "types/automations/actions";
|
||||
import { BaseOptionManager, updateActionData } from './BaseOptionManager';
|
||||
|
||||
|
||||
export default class ActionDeviceState extends BaseOptionManager<DeviceAction> {
|
||||
defaultState = () => ({
|
||||
$smType: 'action',
|
||||
action: 'device',
|
||||
action_data: {
|
||||
alias: "Device",
|
||||
type: "",
|
||||
entity_id: "",
|
||||
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);
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.action_data.alias ?? ""}
|
||||
onChange={alias => update({ alias })}
|
||||
/>
|
||||
<InputText
|
||||
textBoxFor="device_id"
|
||||
label="Device"
|
||||
value={state.action_data.device_id}
|
||||
onChange={device_id => update({ device_id })}
|
||||
/>
|
||||
<InputText
|
||||
textBoxFor="type"
|
||||
label="Action"
|
||||
value={state.action_data.type}
|
||||
onChange={type => update({ type })}
|
||||
/>
|
||||
<InputText
|
||||
textBoxFor="entity_id"
|
||||
label="Entity ID"
|
||||
value={state.action_data.entity_id ?? ""}
|
||||
onChange={entity_id => update({ entity_id, domain: entity_id.split('.')[0] })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { ServiceAction } from "types/automations/actions";
|
||||
import { BaseOptionManager, updateActionData } from './BaseOptionManager';
|
||||
|
||||
|
||||
export default class ActionCallServiceState extends BaseOptionManager<ServiceAction> {
|
||||
defaultState = () => ({
|
||||
$smType: 'action',
|
||||
action: 'service',
|
||||
action_data: {
|
||||
service: "",
|
||||
target: "",
|
||||
data: {},
|
||||
}
|
||||
} as ServiceAction)
|
||||
isReady(state: ServiceAction): boolean {
|
||||
const { service, alias, target } = state.action_data;
|
||||
return service !== '' &&
|
||||
alias !== '' &&
|
||||
target !== ''
|
||||
}
|
||||
renderOptionList(state: ServiceAction): JSX.Element {
|
||||
const { service, alias = "", target } = state.action_data;
|
||||
const update = updateActionData(state, this.setState);
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={alias}
|
||||
onChange={alias => update({ alias })}
|
||||
/>
|
||||
<InputText
|
||||
textBoxFor="service"
|
||||
label="Service"
|
||||
value={service}
|
||||
onChange={service => update({ service })}
|
||||
/>
|
||||
<InputText
|
||||
label="Target"
|
||||
value={target}
|
||||
onChange={target => update({ target })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
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 const updateConditionData = <T extends AutomationCondition>(
|
||||
setState: (s: T) => void
|
||||
) => (
|
||||
state: T,
|
||||
data: Partial<T['condition_data']>,
|
||||
) => setState({
|
||||
...state,
|
||||
condition_data: {
|
||||
...state.condition_data,
|
||||
...data,
|
||||
},
|
||||
})
|
||||
|
||||
export const updateActionData = <T extends AutomationAction>(
|
||||
state: T,
|
||||
setState: (s: T) => void
|
||||
) => (
|
||||
data: Partial<T['action_data']>,
|
||||
) => setState({
|
||||
...state,
|
||||
action_data: {
|
||||
...state.action_data,
|
||||
...data,
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,51 @@
|
||||
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";
|
||||
|
||||
|
||||
export default class ConditionLogicState extends BaseOptionManager<LogicCondition> {
|
||||
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>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
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>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { BaseOptionManager } from "./BaseOptionManager";
|
||||
|
||||
|
||||
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>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
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>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
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";
|
||||
|
||||
|
||||
export const getOptionManager = <T extends AutomationNodeTypes>(
|
||||
nodeType: T,
|
||||
nodeSubtype: AutomationNodeSubtype<T>,
|
||||
) : any => {
|
||||
if ( nodeType === 'action') {
|
||||
switch (nodeSubtype) {
|
||||
case 'service':
|
||||
return ActionCallServiceState;
|
||||
// case "Repeat":
|
||||
// return ScriptRepeatState;
|
||||
// case "Wait":
|
||||
// return ScriptWaitState;
|
||||
// case "Event":
|
||||
// return ScriptEventState;
|
||||
case "device":
|
||||
return ActionDeviceState;
|
||||
case "choose":
|
||||
return ActionChooseState;
|
||||
}
|
||||
}
|
||||
// if (nodeType === 'condition') {
|
||||
// switch (nodeSubtype) {
|
||||
// case 'Logic':
|
||||
// return ConditionLogicState
|
||||
// }
|
||||
// }
|
||||
return Generic
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
import { useState } from 'react';
|
||||
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
|
||||
import { AutomationNodeSubtype } from 'types/automations';
|
||||
import { BaseOptionManager } from './BaseOptionManager';
|
||||
import { getOptionManager } from './getOptionManager';
|
||||
|
||||
|
||||
export const useEditorNodeState = (node: AutomationNode) => {
|
||||
const [state, setState] = useState(node);
|
||||
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);
|
||||
return {
|
||||
nodeType,
|
||||
subType,
|
||||
renderOptionList() {
|
||||
return optionManager.renderOptionList(state);
|
||||
},
|
||||
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
|
||||
} as any)
|
||||
} else if (node.$smType === 'condition') {
|
||||
setState({
|
||||
...state,
|
||||
$smType: 'condition',
|
||||
condition: node.condition
|
||||
} as any)
|
||||
} else {
|
||||
setState({
|
||||
...state,
|
||||
$smType: undefined,
|
||||
platform: node.platform
|
||||
} as any)
|
||||
}
|
||||
} else {
|
||||
setState({
|
||||
...state,
|
||||
$smType: newType === 'trigger' ? undefined : newType,
|
||||
} as any)
|
||||
}
|
||||
},
|
||||
setSubType(newSubType: AutomationNodeSubtype<typeof nodeType>) {
|
||||
if (state.$smType === 'action') {
|
||||
setState({
|
||||
...state,
|
||||
action: newSubType,
|
||||
} as any)
|
||||
} else if (state.$smType === 'condition') {
|
||||
setState({
|
||||
...state,
|
||||
condition: newSubType,
|
||||
} as any)
|
||||
} else {
|
||||
setState({
|
||||
...state,
|
||||
platform: newSubType,
|
||||
} as any)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user