no more $smType in webapp
This commit is contained in:
1 parent
429dfd5b50
commit
c118ccc99b
20 files changed
+327
-502
No files matched your search
@@ -84,18 +84,14 @@ Simple.args = {
|
||||
]
|
||||
},
|
||||
{
|
||||
$smType: "action",
|
||||
action: "service",
|
||||
action_data: {
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -73,18 +73,14 @@ Simple.args = {
|
||||
]
|
||||
},
|
||||
{
|
||||
$smType: "action",
|
||||
action: "service",
|
||||
action_data: {
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -120,128 +116,92 @@ Complex.args = {
|
||||
]
|
||||
},
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "service",
|
||||
"action_data": {
|
||||
"alias": "Start Music In Kitchen",
|
||||
"service": "media_player.play_media",
|
||||
"target": {
|
||||
"entity_id": "media_player.kitchen_dot"
|
||||
},
|
||||
"data": {
|
||||
"media_content_id": "Good Morning",
|
||||
"media_content_type": "SPOTIFY"
|
||||
}
|
||||
"alias": "Start Music In Kitchen",
|
||||
"service": "media_player.play_media",
|
||||
"target": {
|
||||
"entity_id": "media_player.kitchen_dot"
|
||||
},
|
||||
"data": {
|
||||
"media_content_id": "Good Morning",
|
||||
"media_content_type": "SPOTIFY"
|
||||
}
|
||||
},
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "lights.bathroom",
|
||||
"state": "off",
|
||||
}
|
||||
],
|
||||
"sequence": [
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "service",
|
||||
"action_data": {
|
||||
"service": "light.turn_on",
|
||||
"target": {
|
||||
"entity_id": "lights.bathroom"
|
||||
},
|
||||
"data": {}
|
||||
}
|
||||
},
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "service",
|
||||
"action_data": {
|
||||
"alias": "Turn off bedroom",
|
||||
"service": "light.turn_off",
|
||||
"target": {
|
||||
"entity_id": "lights.bedroom"
|
||||
},
|
||||
"data": {}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "lights.bedroom",
|
||||
"state": "off",
|
||||
}
|
||||
],
|
||||
"sequence": [
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "service",
|
||||
"action_data": {
|
||||
"alias": "Turn on bedroom",
|
||||
"service": "light.turn_on",
|
||||
"target": {
|
||||
"entity_id": "lights.bedroom"
|
||||
},
|
||||
"data": {}
|
||||
}
|
||||
},
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [],
|
||||
"sequence": [
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [],
|
||||
"default": [],
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"default": [
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [],
|
||||
"default": [],
|
||||
}
|
||||
}
|
||||
],
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"default": [
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "service",
|
||||
"action_data": {
|
||||
"service": "light.turn_off",
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "lights.bathroom",
|
||||
"state": "off",
|
||||
}
|
||||
],
|
||||
"sequence": [
|
||||
{
|
||||
"service": "light.turn_on",
|
||||
"target": {
|
||||
"entity_id": "lights.bathroom"
|
||||
},
|
||||
"data": {}
|
||||
},
|
||||
{
|
||||
"alias": "Turn off bedroom",
|
||||
"service": "light.turn_off",
|
||||
"target": {
|
||||
"entity_id": "lights.bedroom"
|
||||
},
|
||||
"data": {}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "lights.bedroom",
|
||||
"state": "off",
|
||||
}
|
||||
],
|
||||
"sequence": [
|
||||
{
|
||||
"alias": "Turn on bedroom",
|
||||
"service": "light.turn_on",
|
||||
"target": {
|
||||
"entity_id": "lights.bedroom"
|
||||
},
|
||||
"data": {}
|
||||
},
|
||||
{
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [],
|
||||
"sequence": [
|
||||
{
|
||||
"choose": [],
|
||||
"default": [],
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"default": [
|
||||
{
|
||||
"choose": [],
|
||||
"default": [],
|
||||
}
|
||||
],
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"default": [
|
||||
{
|
||||
"service": "light.turn_off",
|
||||
"target": {
|
||||
"entity_id": "lights.bathroom"
|
||||
},
|
||||
"data": {}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"condition": "and",
|
||||
|
||||
@@ -35,8 +35,8 @@ export const sequenceToFlow = (
|
||||
};
|
||||
}
|
||||
// draw node
|
||||
if ('$smType' in node && node.$smType === 'action') {
|
||||
if (node.action === 'choose') {
|
||||
if (getNodeType(node) === 'action') {
|
||||
if ('choose' in node) {
|
||||
position = addChooseNode(flowData, node, position, nodeId, dims, updater, i)
|
||||
} else {
|
||||
addSingleNode(flowData, node, position, nodeId, dims, {
|
||||
@@ -127,7 +127,7 @@ const addChooseNode = (
|
||||
});
|
||||
let lastPos: XYPosition = position;
|
||||
// conditions
|
||||
node.action_data.choose.forEach(({ sequence, conditions }, j) => {
|
||||
node.choose.forEach(({ sequence, conditions }, j) => {
|
||||
const sequenceId = `${nodeId}.${j}`;
|
||||
// edit/delete circle
|
||||
const circle = makeFlowCircle(`${sequenceId}>cond`, {
|
||||
@@ -158,7 +158,7 @@ const addChooseNode = (
|
||||
lastPos = xyApply(lastPos, { x: lastPos.x, y: lastPos.y + dims.nodeHeight * dims.distanceFactor }, Math.max)
|
||||
}
|
||||
})
|
||||
const totalChildren = node.action_data.choose.length;
|
||||
const totalChildren = node.choose.length;
|
||||
|
||||
// add button
|
||||
const addCirlce = makeFlowCircle(
|
||||
@@ -170,16 +170,13 @@ const addChooseNode = (
|
||||
size: dims.circleSize * dims.distanceFactor,
|
||||
onAdd: () => updater.updateNode(i, {
|
||||
...node,
|
||||
action_data: {
|
||||
...node.action_data,
|
||||
choose: [
|
||||
...node.action_data.choose,
|
||||
{
|
||||
sequence: [],
|
||||
conditions: [],
|
||||
}
|
||||
]
|
||||
}
|
||||
choose: [
|
||||
...node.choose,
|
||||
{
|
||||
sequence: [],
|
||||
conditions: [],
|
||||
}
|
||||
]
|
||||
}),
|
||||
disableSource: true,
|
||||
}
|
||||
@@ -194,7 +191,7 @@ const addChooseNode = (
|
||||
}, { size: dims.circleSize * dims.distanceFactor })
|
||||
flowData.nodes.push(elseCircle)
|
||||
addEdge(flowData, nodeId, elseCircle.id, true)
|
||||
const lastPoint = sequenceToFlow(flowData, node.action_data.default, elseCircle.id, {
|
||||
const lastPoint = sequenceToFlow(flowData, node.default, elseCircle.id, {
|
||||
...dims,
|
||||
padding: {
|
||||
x: position.x - dims.nodeWidth,
|
||||
|
||||
@@ -27,44 +27,36 @@ const mockSequenceUpdater = (defaultState: AutomationSequenceNode[] = []) => {
|
||||
test("sequence updater updates nodes", () => {
|
||||
const mock = mockSequenceUpdater([
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [],
|
||||
"sequence": [],
|
||||
}
|
||||
],
|
||||
"default": [],
|
||||
}
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [],
|
||||
"sequence": [],
|
||||
}
|
||||
],
|
||||
"default": [],
|
||||
}
|
||||
]);
|
||||
expect(mock.state.length).toBe(1)
|
||||
expect((mock.state[0] as ChooseAction).action_data.alias).toBe(undefined)
|
||||
expect((mock.state[0] as ChooseAction).alias).toBe(undefined)
|
||||
mock.updater.updateNode(0, {
|
||||
...mock.state[0],
|
||||
action_data: {
|
||||
...(mock.state[0] as ChooseAction).action_data,
|
||||
...(mock.state[0] as ChooseAction),
|
||||
alias: "Hello"
|
||||
}
|
||||
} as any)
|
||||
expect((mock.state[0] as ChooseAction).action_data.alias).toBe("Hello")
|
||||
expect((mock.state[0] as ChooseAction).alias).toBe("Hello")
|
||||
})
|
||||
test('update conditions for node', () => {
|
||||
const mock = mockSequenceUpdater([
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [],
|
||||
"sequence": [],
|
||||
}
|
||||
],
|
||||
"default": [],
|
||||
}
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [],
|
||||
"sequence": [],
|
||||
}
|
||||
],
|
||||
"default": [],
|
||||
}
|
||||
]);
|
||||
mock.updater.makeOnEditConditionsForChooseNode(0, 0)();
|
||||
@@ -77,6 +69,6 @@ test('update conditions for node', () => {
|
||||
"conditions": [],
|
||||
}
|
||||
] as any)
|
||||
expect((mock.state[0] as ChooseAction).action_data.choose[0].conditions.length).toBe(1)
|
||||
expect((mock.state[0] as ChooseAction).action_data.choose[0].conditions[0].condition).toBe("and")
|
||||
expect((mock.state[0] as ChooseAction).choose[0].conditions.length).toBe(1)
|
||||
expect((mock.state[0] as ChooseAction).choose[0].conditions[0].condition).toBe("and")
|
||||
})
|
||||
@@ -23,12 +23,8 @@ export const makeSequenceUpdater = (
|
||||
single: true,
|
||||
saveBtnCreateText: true,
|
||||
node: {
|
||||
$smType: 'action',
|
||||
action: 'choose',
|
||||
action_data: {
|
||||
choose: [],
|
||||
default: [],
|
||||
}
|
||||
choose: [],
|
||||
default: [],
|
||||
},
|
||||
update: n => onChange([
|
||||
...sequence,
|
||||
@@ -44,44 +40,38 @@ export const makeSequenceUpdater = (
|
||||
...sequence.slice(i + 1),
|
||||
])
|
||||
},
|
||||
updateChooseActionData(i: number, data: ChooseAction['action_data']) {
|
||||
updateChooseActionData(i: number, data: ChooseAction) {
|
||||
const node = sequence[i] as ChooseAction;
|
||||
return this.updateNode(i, {
|
||||
...node,
|
||||
action_data: data,
|
||||
...data,
|
||||
})
|
||||
},
|
||||
makeChildUpdaterForChooseAction(i: number, j: number | null) {
|
||||
const node = sequence[i];
|
||||
if (('$smType' in node) && (node.$smType === 'action') && (node.action === 'choose')) {
|
||||
if ('choose' in node) {
|
||||
if (j !== null) {
|
||||
return makeSequenceUpdater(
|
||||
node.action_data.choose[j].sequence,
|
||||
node.choose[j].sequence,
|
||||
seq => this.updateNode(i, {
|
||||
...node,
|
||||
action_data: {
|
||||
...node.action_data,
|
||||
choose: [
|
||||
...node.action_data.choose.slice(0, j),
|
||||
{
|
||||
...node.action_data.choose[j],
|
||||
sequence: seq,
|
||||
},
|
||||
...node.action_data.choose.slice(j + 1),
|
||||
]
|
||||
}
|
||||
choose: [
|
||||
...node.choose.slice(0, j),
|
||||
{
|
||||
...node.choose[j],
|
||||
sequence: seq,
|
||||
},
|
||||
...node.choose.slice(j + 1),
|
||||
]
|
||||
}),
|
||||
openModal,
|
||||
)
|
||||
} else {
|
||||
return makeSequenceUpdater(
|
||||
node.action_data.default,
|
||||
node.default,
|
||||
seq => this.updateNode(i, {
|
||||
...node,
|
||||
action_data: {
|
||||
...node.action_data,
|
||||
default: seq
|
||||
}
|
||||
default: seq
|
||||
}),
|
||||
openModal,
|
||||
)
|
||||
@@ -108,21 +98,18 @@ export const makeSequenceUpdater = (
|
||||
const node = sequence[i] as ChooseAction;
|
||||
return openModal({
|
||||
single: false,
|
||||
node: node.action_data.choose[j].conditions,
|
||||
node: node.choose[j].conditions,
|
||||
allowedTypes: ['condition'],
|
||||
update: conditions => this.updateNode(i, {
|
||||
...node,
|
||||
action_data: {
|
||||
...node.action_data,
|
||||
choose: [
|
||||
...node.action_data.choose.slice(0, j),
|
||||
{
|
||||
sequence: node.action_data.choose[j].sequence,
|
||||
conditions: conditions as AutomationCondition[],
|
||||
},
|
||||
...node.action_data.choose.slice(j + 1),
|
||||
]
|
||||
}
|
||||
choose: [
|
||||
...node.choose.slice(0, j),
|
||||
{
|
||||
sequence: node.choose[j].sequence,
|
||||
conditions: conditions as AutomationCondition[],
|
||||
},
|
||||
...node.choose.slice(j + 1),
|
||||
]
|
||||
}),
|
||||
})
|
||||
}
|
||||
@@ -136,11 +123,8 @@ export const createOnRemoveForChooseNode = (
|
||||
j: number
|
||||
) => () => update({
|
||||
...action,
|
||||
action_data: {
|
||||
...action.action_data,
|
||||
choose: [
|
||||
...action.action_data.choose.slice(0, j),
|
||||
...action.action_data.choose.slice(j + 1),
|
||||
]
|
||||
}
|
||||
choose: [
|
||||
...action.choose.slice(0, j),
|
||||
...action.choose.slice(j + 1),
|
||||
]
|
||||
})
|
||||
@@ -9,13 +9,9 @@ const makeDefault = (nodeType: keyof AutomationNodeMapping): AutomationNode => {
|
||||
switch (nodeType) {
|
||||
case 'action':
|
||||
return {
|
||||
$smType: 'action',
|
||||
action: "service",
|
||||
action_data: {
|
||||
'data': {},
|
||||
'service': '',
|
||||
'target': {},
|
||||
}
|
||||
'data': {},
|
||||
'service': '',
|
||||
'target': {},
|
||||
}
|
||||
case 'condition':
|
||||
return {
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -2,54 +2,57 @@ import { AutomationSequenceNode } from ".";
|
||||
import { AutomationTime } from "./common";
|
||||
import { AutomationCondition } from "./conditions";
|
||||
|
||||
interface AutomationActionNodeBase<Name extends String,Data extends Object> {
|
||||
$smType: 'action';
|
||||
action: Name;
|
||||
action_data: Data & {
|
||||
alias?: string;
|
||||
variables?: Record<string, any>;
|
||||
};
|
||||
type AutomationActionNodeBase<Data extends Object> = Data & {
|
||||
alias?: string;
|
||||
variables?: Record<string, any>;
|
||||
}
|
||||
|
||||
export interface ServiceAction extends AutomationActionNodeBase<"service", {
|
||||
export type ServiceAction = AutomationActionNodeBase<{
|
||||
service: string;
|
||||
target: any;
|
||||
data: any;
|
||||
}>{}
|
||||
export interface RepeatAction extends AutomationActionNodeBase<"repeat", {
|
||||
}>
|
||||
export type RepeatAction = AutomationActionNodeBase<{
|
||||
repeat: {
|
||||
count: number;
|
||||
sequence: AutomationAction[];
|
||||
}
|
||||
}>{}
|
||||
export interface WaitAction extends AutomationActionNodeBase<"wait", {
|
||||
}>
|
||||
export type WaitAction = AutomationActionNodeBase<{
|
||||
wait_template: string;
|
||||
timeout?: AutomationTime;
|
||||
continue_on_timeout?: boolean;
|
||||
}>{}
|
||||
export interface FireEventAction extends AutomationActionNodeBase<"event", {
|
||||
}>
|
||||
export type FireEventAction = AutomationActionNodeBase<{
|
||||
event: string;
|
||||
event_data: any;
|
||||
}>{}
|
||||
export interface DeviceAction extends AutomationActionNodeBase<"device", {
|
||||
}>
|
||||
export type DeviceAction = AutomationActionNodeBase<{
|
||||
type: string;
|
||||
device_id: string;
|
||||
entity_id?: string;
|
||||
domain: string;
|
||||
}>{}
|
||||
export interface ChooseAction extends AutomationActionNodeBase<"choose", {
|
||||
}>
|
||||
export type ChooseAction = AutomationActionNodeBase<{
|
||||
choose: Array<{
|
||||
conditions: AutomationCondition[];
|
||||
sequence: AutomationSequenceNode[];
|
||||
}>;
|
||||
default: AutomationSequenceNode[];
|
||||
}>{}
|
||||
export type AutomationAction =
|
||||
}>
|
||||
export type AutomationAction =
|
||||
| ServiceAction
|
||||
| RepeatAction
|
||||
| WaitAction
|
||||
| FireEventAction
|
||||
| DeviceAction
|
||||
| ChooseAction
|
||||
;
|
||||
export type ActionType = AutomationAction['action'];
|
||||
;
|
||||
export type ActionType =
|
||||
| 'service'
|
||||
| 'repeat'
|
||||
| 'wait'
|
||||
| 'event'
|
||||
| 'device'
|
||||
| 'choose'
|
||||
;
|
||||
@@ -10,18 +10,14 @@ test('converting time to a string', () => {
|
||||
|
||||
test('using alias as description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: "action",
|
||||
action: "service",
|
||||
action_data: {
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
})).toEqual("Start Music In Kitchen")
|
||||
})
|
||||
@@ -29,30 +25,22 @@ test('using alias as description', () => {
|
||||
|
||||
test('get service action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: "action",
|
||||
action: "service",
|
||||
action_data: {
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
})).toEqual("media_player.play_media")
|
||||
})
|
||||
|
||||
test('get repeat action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: "action",
|
||||
action: "repeat",
|
||||
action_data: {
|
||||
repeat: {
|
||||
count: 10,
|
||||
sequence: []
|
||||
}
|
||||
repeat: {
|
||||
count: 10,
|
||||
sequence: []
|
||||
}
|
||||
})).toEqual("Repeat 10")
|
||||
})
|
||||
@@ -60,57 +48,35 @@ test('get repeat action description', () => {
|
||||
|
||||
test('get wait action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: "action",
|
||||
action: "wait",
|
||||
action_data: {
|
||||
wait_template: "states(switch.kitchen_light) == 'on'"
|
||||
}
|
||||
wait_template: "states(switch.kitchen_light) == 'on'"
|
||||
})).toEqual("Wait on states(switch.kitchen_light) == 'on'")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: "action",
|
||||
action: "wait",
|
||||
action_data: {
|
||||
wait_template: "states(switch.kitchen_light) == 'on'",
|
||||
// timeout: '00:01:00',
|
||||
timeout: {
|
||||
'minutes': 1,
|
||||
}
|
||||
wait_template: "states(switch.kitchen_light) == 'on'",
|
||||
timeout: {
|
||||
'minutes': 1,
|
||||
}
|
||||
})).toEqual("Wait on states(switch.kitchen_light) == 'on' for 00:01:00:00")
|
||||
})
|
||||
|
||||
test('get fire event action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'action',
|
||||
action: 'event',
|
||||
action_data: {
|
||||
event: 'test_event',
|
||||
event_data: {},
|
||||
}
|
||||
event: 'test_event',
|
||||
event_data: {},
|
||||
})).toEqual("Trigger test_event")
|
||||
})
|
||||
|
||||
test('get device action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'action',
|
||||
action: 'device',
|
||||
action_data: {
|
||||
device_id: "12310das01231",
|
||||
domain: "zwave_js",
|
||||
type: "set_value"
|
||||
}
|
||||
|
||||
device_id: "12310das01231",
|
||||
domain: "zwave_js",
|
||||
type: "set_value"
|
||||
})).toEqual("set_value on 12310das01231")
|
||||
})
|
||||
|
||||
test('get choose action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'action',
|
||||
action: 'choose',
|
||||
action_data: {
|
||||
choose: [],
|
||||
default: [],
|
||||
}
|
||||
choose: [],
|
||||
default: [],
|
||||
})).toEqual("Choose")
|
||||
})
|
||||
|
||||
|
||||
@@ -10,36 +10,13 @@ export const convertTimeToString = ({
|
||||
}: AutomationTime) => [hours, minutes, seconds, milliseconds].map(n => String(n).padStart(2, '0')).join(':')
|
||||
|
||||
export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(node: AutomationNode<N>): string => {
|
||||
if ('$smType' in node) {
|
||||
switch (node['$smType']) {
|
||||
case "action":
|
||||
if (node.action_data.alias) {
|
||||
return node.action_data.alias;
|
||||
}
|
||||
switch (node.action) {
|
||||
case 'service':
|
||||
return node.action_data.service
|
||||
case 'repeat':
|
||||
return 'Repeat ' + node.action_data.repeat.count;
|
||||
case 'wait':
|
||||
let out = 'Wait on ' + node.action_data.wait_template;
|
||||
if (node.action_data.timeout) {
|
||||
out += (' for ' + convertTimeToString(node.action_data.timeout))
|
||||
}
|
||||
return out;
|
||||
case 'event':
|
||||
return `Trigger ${node.action_data.event}`
|
||||
case 'device':
|
||||
return `${node.action_data.type} on ${node.action_data.device_id}`
|
||||
case 'choose':
|
||||
return "Choose"
|
||||
}
|
||||
}
|
||||
return 'n/a'
|
||||
} else if ('condition' in node) {
|
||||
if (node.alias) {
|
||||
return node.alias;
|
||||
}
|
||||
if (node.alias) {
|
||||
return node.alias;
|
||||
}
|
||||
if ('platform' in node) {
|
||||
return node.platform
|
||||
}
|
||||
if ('condition' in node) {
|
||||
if (
|
||||
(node.condition === 'and') ||
|
||||
(node.condition === 'or') ||
|
||||
@@ -71,12 +48,30 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
|
||||
return 'n/a'
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (node.alias) {
|
||||
return node.alias;
|
||||
}
|
||||
return node.platform
|
||||
}
|
||||
if ('service' in node) {
|
||||
return node.service
|
||||
}
|
||||
if ('repeat' in node) {
|
||||
return 'Repeat ' + node.repeat.count
|
||||
}
|
||||
if ('wait_template' in node) {
|
||||
let out = 'Wait on ' + node.wait_template;
|
||||
if (node.timeout) {
|
||||
out += (' for ' + convertTimeToString(node.timeout))
|
||||
}
|
||||
return out;
|
||||
}
|
||||
if ('event' in node) {
|
||||
return `Trigger ${node.event}`
|
||||
}
|
||||
if ('device_id' in node) {
|
||||
return `${node.type} on ${node.device_id}`
|
||||
}
|
||||
if ('choose' in node) {
|
||||
return "Choose"
|
||||
}
|
||||
return 'n/a'
|
||||
}
|
||||
|
||||
export const prettyEntityId = (entityId: string | string[]) => {
|
||||
|
||||
@@ -38,18 +38,14 @@ export const createMockAuto = (tags: Record<string, string> = {}): AutomationDat
|
||||
]
|
||||
},
|
||||
{
|
||||
$smType: "action",
|
||||
action: "service",
|
||||
action_data: {
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
Reference in new issue
Block a user