no more $smType in webapp

This commit is contained in:
ari committed 2022-05-13 17:26:44 -04:00
1 parent 429dfd5b50
commit c118ccc99b
20 files changed
+327 -502

No files matched your search

@@ -5,15 +5,11 @@ import { OptionManager, updateActionData } from './OptionManager';
export const ActionChooseState: OptionManager<ChooseAction> = {
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<ChooseAction> = {
return <>
<InputText
label="Description"
value={state.action_data.alias ?? ""}
value={state.alias ?? ""}
onChange={alias => update({ alias })}
/>
</>
@@ -5,24 +5,18 @@ import { OptionManager, updateActionData } from './OptionManager';
export const ActionDeviceState: OptionManager<DeviceAction> = {
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<DeviceAction> = {
return <>
<InputText
label="Description"
value={state.action_data.alias ?? ""}
value={state.alias ?? ""}
onChange={alias => update({ alias })}
/>
<InputText
textBoxFor="device_id"
label="Device"
value={state.action_data.device_id}
value={state.device_id}
onChange={device_id => update({ device_id })}
/>
<InputText
textBoxFor="type"
label="Action"
value={state.action_data.type}
value={state.type}
onChange={type => update({ type })}
/>
<InputText
textBoxFor="entity_id"
label="Entity ID"
value={state.action_data.entity_id ?? ""}
value={state.entity_id ?? ""}
onChange={entity_id => update({ entity_id, domain: entity_id.split('.')[0] })}
/>
</>
@@ -6,15 +6,11 @@ import { OptionManager, updateActionData } from "./OptionManager";
export const ActionEventState: OptionManager<FireEventAction> = {
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<FireEventAction> = {
return <>
<InputText
label="Description"
value={state.action_data.alias ?? ""}
value={state.alias ?? ""}
onChange={alias => update({ alias })}
/>
<InputText
textBoxFor="event"
label="Event"
value={state.action_data.event}
value={state.event}
onChange={event => update({ event })}
/>
<InputYaml
label="Event Data"
value={state.action_data.event_data}
value={state.event_data}
onChange={event_data => update({ event_data })}
/>
</>
@@ -7,20 +7,16 @@ import { OptionManager, updateActionData } from './OptionManager';
export const ActionRepeatState: OptionManager<RepeatAction> = {
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<RepeatAction> = {
return <>
<InputText
label="Description"
value={state.action_data.alias ?? ""}
value={state.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 } })}
value={state.repeat.count}
onChange={(count = 1) => update({ repeat: { ...state.repeat, count } })}
/>
<InputYaml
label="Sequence"
value={state.action_data.repeat.sequence ?? []}
onChange={sequence => update({ repeat: { ...state.action_data.repeat, sequence } })}
value={state.repeat.sequence ?? []}
onChange={sequence => update({ repeat: { ...state.repeat, sequence } })}
/>
</>
}
@@ -6,23 +6,19 @@ import { OptionManager, updateActionData } from './OptionManager';
export const ActionCallServiceState: OptionManager<ServiceAction> = {
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 <>
<InputText
@@ -6,17 +6,13 @@ 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,
}
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<WaitAction> = {
return <>
<InputText
label="Description"
value={state.action_data.alias ?? ""}
value={state.alias ?? ""}
onChange={alias => update({ alias })}
/>
<InputText
textBoxFor="template"
label="Template"
value={state.action_data.wait_template}
value={state.wait_template}
onChange={wait_template => update({ wait_template })}
/>
{/* <InputNumber
label="Timeout"
value={state.action_data.timeout ?? 0}
value={state.timeout ?? 0}
onChange={(timeout = 0) => update({ timeout })}
/> */}
<InputBoolean
label="Continue on Timeout"
value={state.action_data.continue_on_timeout ?? false}
value={state.continue_on_timeout ?? false}
onChange={continue_on_timeout => update({ continue_on_timeout })}
/>
</>
@@ -21,11 +21,8 @@ export const updateActionData = <T extends AutomationAction>(
state: T,
setState: (s: T) => void
) => (
data: Partial<T['action_data']>,
data: Partial<T>,
) => setState({
...state,
action_data: {
...state.action_data,
...data,
},
...data,
})
@@ -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<typeof nodeType>) {
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())
}
}
}
+14 -3
View File
@@ -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 = <T extends AutomationNodeTypes>(
export const getNodeSubType = <T extends AutomationNodeTypes>(
node: AutomationNode<T>
): AutomationNodeSubtype<T> => {
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!")
}
@@ -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<typeof NodeEditor>