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

+86 -126
View File
@@ -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,
+19 -27
View File
@@ -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")
})
+29 -45
View File
@@ -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),
]
})