more name stuff + .gitignore
This commit is contained in:
1 parent
76dca488ee
commit
ea9dd2a8a3
26 files changed
+595
-162
No files matched your search
@@ -0,0 +1,4 @@
|
||||
web/node_modules
|
||||
web/.parcel-cache
|
||||
web/dist
|
||||
api/venv
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"name": "vscode-jest-tests",
|
||||
"request": "launch",
|
||||
"console": "integratedTerminal",
|
||||
"internalConsoleOptions": "neverOpen",
|
||||
"disableOptimisticBPs": true,
|
||||
"program": "${workspaceFolder}/node_modules/.bin/jest",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"args": [
|
||||
"--runInBand",
|
||||
"--watchAll=false"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Vendored
+2
-1
@@ -1,3 +1,4 @@
|
||||
{
|
||||
"typescript.tsdk": "node_modules/typescript/lib"
|
||||
"typescript.tsdk": "node_modules/typescript/lib",
|
||||
"jest.jestCommandLine": "node_modules/.bin/jest"
|
||||
}
|
||||
@@ -21,7 +21,7 @@ export default class ConditionLogicState extends BaseState<LogicCondition> {
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
value={state.alias ?? ""}
|
||||
onChange={alias => this.setState({...state, alias})}
|
||||
/>
|
||||
<InputList
|
||||
|
||||
@@ -26,7 +26,7 @@ export default class ConditionNumStateState extends BaseState<NumericCondition>
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
value={state.alias ?? ""}
|
||||
onChange={alias => this.setState({...state, alias})}
|
||||
/>
|
||||
<InputText
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import InputText from "../../components/InputText";
|
||||
import { ServiceScript } from "../types/script";
|
||||
import { BaseState } from "./BaseState";
|
||||
|
||||
|
||||
@@ -7,13 +6,13 @@ export default class Generic extends BaseState<{alias: string}> {
|
||||
defaultState = () => ({
|
||||
alias: "Unknown",
|
||||
})
|
||||
isReady(state: ServiceScript): boolean {
|
||||
isReady(state: any): boolean {
|
||||
return true;
|
||||
}
|
||||
renderOptionList(state: ServiceScript): JSX.Element {
|
||||
renderOptionList(state: any): JSX.Element {
|
||||
return <div className="state-manager-options">
|
||||
<InputText label="Description"
|
||||
value={state.alias}
|
||||
value={state.alias??""}
|
||||
onChange={alias => this.setState({...state, alias})}
|
||||
/>
|
||||
<br/>
|
||||
|
||||
@@ -19,7 +19,7 @@ export default class ScriptChooseState extends BaseState<ChooseScript> {
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
value={state.alias ?? ""}
|
||||
onChange={alias => this.setState({...state, alias})}
|
||||
/>
|
||||
<InputYaml
|
||||
|
||||
@@ -25,7 +25,7 @@ export default class ScriptDeviceState extends BaseState<DeviceScript> {
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
value={state.alias ?? ""}
|
||||
onChange={alias => this.setState({...state, alias})}
|
||||
/>
|
||||
<InputText
|
||||
|
||||
@@ -20,7 +20,7 @@ export default class ScriptEventState extends BaseState<FireEventScript> {
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
value={state.alias ?? ""}
|
||||
onChange={alias => this.setState({...state, alias})}
|
||||
/>
|
||||
<InputText
|
||||
|
||||
@@ -21,7 +21,7 @@ export default class ScriptRepeatState extends BaseState<RepeatScript> {
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
value={state.alias ?? ""}
|
||||
onChange={alias => this.setState({...state, alias})}
|
||||
/>
|
||||
<InputNumber
|
||||
|
||||
@@ -20,7 +20,7 @@ export default class ScriptCallServiceState extends BaseState<ServiceScript> {
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
value={state.alias ?? ""}
|
||||
onChange={alias => this.setState({...state, alias})}
|
||||
/>
|
||||
<InputText
|
||||
|
||||
@@ -20,7 +20,7 @@ export default class ScriptWaitState extends BaseState<WaitScript> {
|
||||
return <div className="state-manager-options">
|
||||
<InputText
|
||||
label="Description"
|
||||
value={state.alias}
|
||||
value={state.alias ?? ""}
|
||||
onChange={alias => this.setState({...state, alias})}
|
||||
/>
|
||||
<InputText
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { AutomationTime } from "./common";
|
||||
|
||||
interface BaseCondition {
|
||||
alias: string;
|
||||
alias?: string;
|
||||
};
|
||||
export interface LogicCondition extends BaseCondition {
|
||||
condition: 'and' | 'or' | 'not';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { AutomationCondition } from "./condition";
|
||||
|
||||
interface BaseScript {
|
||||
alias: string;
|
||||
alias?: string;
|
||||
variables?: Record<string, any>;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ export interface FireEventScript extends BaseScript {
|
||||
export interface DeviceScript extends BaseScript {
|
||||
type: string;
|
||||
device_id: string;
|
||||
entity_id: string;
|
||||
entity_id?: string;
|
||||
domain: string;
|
||||
}
|
||||
export interface ChooseScript extends BaseScript {
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
|
||||
import {getDescriptionFromAutomationNode} from "./utils";
|
||||
|
||||
test('using alias as description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
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")
|
||||
})
|
||||
|
||||
|
||||
test('get service action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
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({
|
||||
repeat: {
|
||||
count: 10,
|
||||
sequence: []
|
||||
}
|
||||
})).toEqual("Repeat 10")
|
||||
})
|
||||
|
||||
|
||||
test('get wait action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
wait_template: "states(switch.kitchen_light) == 'on'"
|
||||
})).toEqual("Wait on states(switch.kitchen_light) == 'on'")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
wait_template: "states(switch.kitchen_light) == 'on'",
|
||||
timeout: '00:01:00'
|
||||
})).toEqual("Wait on states(switch.kitchen_light) == 'on' for 00:01:00")
|
||||
})
|
||||
|
||||
test('get fire event action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
event: 'test_event',
|
||||
event_data: {},
|
||||
})).toEqual("Trigger test_event")
|
||||
})
|
||||
|
||||
test('get device action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
device_id: "12310das01231",
|
||||
domain: "zwave_js",
|
||||
type: "set_value"
|
||||
|
||||
})).toEqual("set_value on 12310das01231")
|
||||
})
|
||||
|
||||
test('get choose action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
choose: [],
|
||||
default: [],
|
||||
})).toEqual("Choose")
|
||||
})
|
||||
|
||||
test('get and/or/not condition description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'and',
|
||||
conditions: [],
|
||||
})).toEqual("Logic")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'or',
|
||||
conditions: [],
|
||||
})).toEqual("Logic")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'not',
|
||||
conditions: [],
|
||||
})).toEqual("Logic")
|
||||
})
|
||||
|
||||
test('get Numeric State condition description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'numeric_state',
|
||||
entity_id: 'sensor.temperature',
|
||||
})).toEqual("sensor.temperature?>?<")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'numeric_state',
|
||||
})).toEqual("Numeric State Condition")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'numeric_state',
|
||||
entity_id: 'sensor.temperature',
|
||||
above: 17,
|
||||
})).toEqual("sensor.temperature > 17")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'numeric_state',
|
||||
entity_id: 'sensor.temperature',
|
||||
below: 5,
|
||||
})).toEqual("sensor.temperature < 5")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'numeric_state',
|
||||
entity_id: 'sensor.temperature',
|
||||
above: 17,
|
||||
below: 20,
|
||||
})).toEqual("17 < sensor.temperature < 20")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'numeric_state',
|
||||
entity_id: 'sensor.temperature',
|
||||
above: 17,
|
||||
below: 20,
|
||||
value_template: "{{ float(state.state) + 2 }}"
|
||||
})).toEqual("17 < sensor.temperature < 20")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
condition: 'numeric_state',
|
||||
entity_id: [
|
||||
"sensor.kitchen_temperature",
|
||||
"sensor.living_room_temperature"
|
||||
],
|
||||
above: 17,
|
||||
below: 20,
|
||||
value_template: "{{ float(state.state) + 2 }}"
|
||||
})).toEqual("17 < ... < 20")
|
||||
})
|
||||
|
||||
// test('get State condition description', () => {
|
||||
// expect(getDescriptionFromAutomationNode({
|
||||
// condition: 'state',
|
||||
// entity_id: 'sensor.temperature',
|
||||
// })).toEqual("sensor.temperature?>?<")
|
||||
// })
|
||||
@@ -1,27 +1,58 @@
|
||||
import { AutomationNode, AutomationNodeType, NodeSubType } from "./types";
|
||||
|
||||
|
||||
export const getNameFromAction = (action: AutomationNode<any>): string => {
|
||||
export const getDescriptionFromAutomationNode = (action: AutomationNode<any>): string => {
|
||||
if(typeof action === 'string') {
|
||||
return action;
|
||||
}
|
||||
if ('alias' in action && action.alias){
|
||||
return action.alias;
|
||||
}
|
||||
if ('condition' in action) {
|
||||
if(
|
||||
(action.condition === 'and') ||
|
||||
(action.condition === 'or') ||
|
||||
(action.condition === 'not')
|
||||
) {
|
||||
return "Logic"
|
||||
} else {
|
||||
switch(action.condition) {
|
||||
case "numeric_state":
|
||||
if(action.entity_id) {
|
||||
const center = typeof action.entity_id === 'string' ? action.entity_id : '...'
|
||||
if(action.above) {
|
||||
if(action.below) {
|
||||
return `${action.above} < ${center} < ${action.below}`
|
||||
}
|
||||
return `${center} > ${action.above}`
|
||||
}
|
||||
if(action.below) {
|
||||
return `${center} < ${action.below}`
|
||||
}
|
||||
return `${center}?>?<`
|
||||
}
|
||||
return "Numeric State Condition"
|
||||
}
|
||||
}
|
||||
}
|
||||
if ('service' in action) {
|
||||
return action.service
|
||||
}
|
||||
if ('repeat' in action) {
|
||||
return 'Repeat'
|
||||
return 'Repeat ' + action.repeat.count;
|
||||
}
|
||||
if ('wait_template' in action) {
|
||||
return 'Wait'
|
||||
let out= 'Wait on ' + action.wait_template;
|
||||
if (action.timeout) {
|
||||
out += (' for ' + action.timeout)
|
||||
}
|
||||
return out;
|
||||
}
|
||||
if ('event' in action) {
|
||||
return action.event
|
||||
return `Trigger ${action.event}`
|
||||
}
|
||||
if ('type' in action) {
|
||||
return 'Device ' +action.type
|
||||
return `${action.type} on ${action.device_id}`
|
||||
}
|
||||
if ('choose' in action) {
|
||||
return 'Choose'
|
||||
|
||||
@@ -9,7 +9,7 @@ export interface NodeProp extends Node {
|
||||
isOpen?: boolean;
|
||||
onXClick?: () => void;
|
||||
onOpenClick?: () => void;
|
||||
topColor?: 'red' | 'green' | 'blue' | 'none';
|
||||
color?: Node['color'];
|
||||
}
|
||||
export default ({
|
||||
loc: [x,y],
|
||||
@@ -19,10 +19,10 @@ export default ({
|
||||
onXClick=() => {},
|
||||
onOpenClick=() => {},
|
||||
isOpen=false,
|
||||
topColor='none',
|
||||
color='none',
|
||||
}: NodeProp) => <foreignObject x={x} y={y} width={width} height={height}>
|
||||
<div className="automation-dag--node">
|
||||
<div className={`automation-dag--node-inner ${topColor !== 'none' ? `top-color ${topColor}`: ''}`}>
|
||||
<div className={`automation-dag--node-inner ${color !== 'none' ? `top-color ${color}`: ''}`}>
|
||||
<div className="automation-dag--node-inner-edge left" onClick={onXClick}>
|
||||
<div className="automation-node-side-button">X</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
|
||||
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
|
||||
import { DAG } from "./types";
|
||||
import {clip, computeDAG} from "./dagFuncs";
|
||||
|
||||
test('it clips overflowing values', () => {
|
||||
expect(clip([GRAPH_WIDTH, GRAPH_HEIGHT])).toEqual([
|
||||
GRAPH_WIDTH-NODE_WIDTH,
|
||||
GRAPH_HEIGHT-NODE_HEIGHT
|
||||
])
|
||||
expect(clip([-10, -10])).toEqual([
|
||||
0, 0
|
||||
])
|
||||
expect(clip([0, 0])).toEqual([
|
||||
0, 0
|
||||
])
|
||||
})
|
||||
|
||||
test('simple empty input dag', () => {
|
||||
expect(computeDAG({
|
||||
id: "root",
|
||||
alias: "root",
|
||||
description: 'root',
|
||||
mode: 'single',
|
||||
trigger: [],
|
||||
condition: [],
|
||||
action: [
|
||||
],
|
||||
})).toEqual({
|
||||
nodes: {},
|
||||
edges: [
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
test('simple single node type input dag', () => {
|
||||
expect(computeDAG({
|
||||
id: "root",
|
||||
alias: "root",
|
||||
description: 'root',
|
||||
mode: 'single',
|
||||
trigger: [
|
||||
{
|
||||
platform: 'time',
|
||||
at: '10:00:00'
|
||||
}
|
||||
],
|
||||
condition: [
|
||||
{
|
||||
condition: 'template',
|
||||
value_template: 'states(switch.kitchen_light) == "on"'
|
||||
}
|
||||
],
|
||||
action: [
|
||||
{
|
||||
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({
|
||||
nodes: {
|
||||
"trigger.0": {
|
||||
text: '',
|
||||
// text: "At 10:00:00",
|
||||
loc: [0,5],
|
||||
color: 'green',
|
||||
},
|
||||
"condition.0": {
|
||||
loc: [NODE_WIDTH*1.5, 5],
|
||||
text: '',
|
||||
color: 'blue',
|
||||
// text: 'states(switch.kitchen_light) == "on"'
|
||||
},
|
||||
"action.0": {
|
||||
loc: [NODE_WIDTH*3, 5],
|
||||
text: '',
|
||||
color: 'red',
|
||||
// text: 'Start Music In Kitchen',
|
||||
}
|
||||
},
|
||||
edges: [
|
||||
{ from: 'trigger.0', to: 'condition.0', direction: '1->2' },
|
||||
{ from: 'condition.0', to: 'action.0', direction: '1->2' },
|
||||
],
|
||||
} as DAG)
|
||||
})
|
||||
|
||||
|
||||
test('simple dag with no triggers', () => {
|
||||
expect(computeDAG({
|
||||
id: "root",
|
||||
alias: "root",
|
||||
description: 'root',
|
||||
mode: 'single',
|
||||
trigger: [
|
||||
],
|
||||
condition: [
|
||||
{
|
||||
condition: 'template',
|
||||
value_template: 'states(switch.kitchen_light) == "on"'
|
||||
}
|
||||
],
|
||||
action: [
|
||||
{
|
||||
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({
|
||||
nodes: {
|
||||
"condition.0": {
|
||||
loc: [NODE_WIDTH*1.5, 5],
|
||||
text: '',
|
||||
color: 'blue',
|
||||
// text: 'states(switch.kitchen_light) == "on"'
|
||||
},
|
||||
"action.0": {
|
||||
loc: [NODE_WIDTH*3, 5],
|
||||
text: '',
|
||||
color: 'red',
|
||||
// text: 'Start Music In Kitchen',
|
||||
}
|
||||
},
|
||||
edges: [
|
||||
{ from: 'condition.0', to: 'action.0', direction: '1->2' },
|
||||
],
|
||||
} as DAG)
|
||||
})
|
||||
|
||||
|
||||
test('simple dag with no conditions', () => {
|
||||
expect(computeDAG({
|
||||
id: "root",
|
||||
alias: "root",
|
||||
description: 'root',
|
||||
mode: 'single',
|
||||
trigger: [
|
||||
{
|
||||
platform: 'time',
|
||||
at: '10:00:00'
|
||||
}
|
||||
],
|
||||
condition: [],
|
||||
action: [
|
||||
{
|
||||
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({
|
||||
nodes: {
|
||||
"trigger.0": {
|
||||
text: '',
|
||||
// text: "At 10:00:00",
|
||||
loc: [0,5],
|
||||
color: 'green',
|
||||
},
|
||||
"action.0": {
|
||||
loc: [NODE_WIDTH*3, 5],
|
||||
text: '',
|
||||
color: 'red',
|
||||
// text: 'Start Music In Kitchen',
|
||||
}
|
||||
},
|
||||
edges: [
|
||||
{ from: 'trigger.0', to: 'action.0', direction: '1->2' },
|
||||
],
|
||||
} as DAG)
|
||||
})
|
||||
|
||||
|
||||
test('simple dag with no actions', () => {
|
||||
expect(computeDAG({
|
||||
id: "root",
|
||||
alias: "root",
|
||||
description: 'root',
|
||||
mode: 'single',
|
||||
trigger: [
|
||||
{
|
||||
platform: 'time',
|
||||
at: '10:00:00'
|
||||
}
|
||||
],
|
||||
condition: [
|
||||
{
|
||||
condition: 'template',
|
||||
value_template: 'states(switch.kitchen_light) == "on"'
|
||||
}
|
||||
],
|
||||
action: [
|
||||
],
|
||||
})).toEqual({
|
||||
nodes: {
|
||||
"trigger.0": {
|
||||
text: '',
|
||||
// text: "At 10:00:00",
|
||||
loc: [0,5],
|
||||
color: 'green',
|
||||
},
|
||||
"condition.0": {
|
||||
loc: [NODE_WIDTH*1.5, 5],
|
||||
text: '',
|
||||
color: 'blue',
|
||||
// text: 'states(switch.kitchen_light) == "on"'
|
||||
},
|
||||
},
|
||||
edges: [
|
||||
{ from: 'trigger.0', to: 'condition.0', direction: '1->2' }
|
||||
],
|
||||
} as DAG)
|
||||
})
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
|
||||
import { Automation } from "../../automation/types";
|
||||
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
|
||||
import {DAG, Node, Point, NormalizedDag} from "./types";
|
||||
|
||||
|
||||
export const clip = (p: Point) : Point => {
|
||||
const [x,y] = p.map(c => Math.max(0, c));
|
||||
return [Math.min(GRAPH_WIDTH-NODE_WIDTH, x), Math.min(GRAPH_HEIGHT-NODE_HEIGHT, y)]
|
||||
}
|
||||
export const normalizeDAG = (dag: DAG): NormalizedDag => {
|
||||
// let overflow: bo
|
||||
const nodes = Object.keys(dag.nodes).map<[string, Node]>(nodeId => [nodeId, {
|
||||
...dag.nodes[nodeId],
|
||||
loc: clip(dag.nodes[nodeId].loc),
|
||||
}]).reduce((all, [nodeId, next]) => ({
|
||||
[nodeId]: next,
|
||||
...all,
|
||||
}),{});
|
||||
return {
|
||||
nodes,
|
||||
edges: dag.edges,
|
||||
}
|
||||
}
|
||||
export const computeDAG = (automation: Automation) : DAG => {
|
||||
const nodes: DAG['nodes'] = {};
|
||||
const edges: DAG['edges'] = [];
|
||||
|
||||
const triggerIds: string[] = [];
|
||||
automation.trigger.forEach((t,i) => {
|
||||
nodes[`trigger.${i}`] = {
|
||||
loc: [0, 5+NODE_HEIGHT*i*1.5],
|
||||
text: "",
|
||||
color: 'green',
|
||||
}
|
||||
triggerIds.push(`trigger.${i}`);
|
||||
})
|
||||
|
||||
let lastConditionId: string | undefined = undefined;
|
||||
automation.condition.forEach((c,i) => {
|
||||
const cid = `condition.${i}`;
|
||||
nodes[cid] = {
|
||||
loc: [NODE_WIDTH*1.5, 5+NODE_HEIGHT*i*1.5],
|
||||
text: "",
|
||||
color: 'blue',
|
||||
}
|
||||
lastConditionId = cid;
|
||||
if (i === 0) {
|
||||
triggerIds.forEach(tid => edges.push({
|
||||
from: tid, to: cid, direction: '1->2',
|
||||
}));
|
||||
} else {
|
||||
edges.push({
|
||||
from: lastConditionId, to: cid, direction: '1->2',
|
||||
})
|
||||
}
|
||||
});
|
||||
if (!lastConditionId) {
|
||||
if(triggerIds.length > 0) {
|
||||
lastConditionId = triggerIds[triggerIds.length-1]
|
||||
}
|
||||
}
|
||||
|
||||
const actionIds: string[] = [];
|
||||
automation.action.forEach((c,i) => {
|
||||
const aid = `action.${i}`;
|
||||
nodes[aid] = {
|
||||
loc: [NODE_WIDTH*3, 5+NODE_HEIGHT*i*1.5],
|
||||
text: "",
|
||||
color: 'red',
|
||||
}
|
||||
actionIds.push(aid);
|
||||
if (i === 0) {
|
||||
if (lastConditionId) {
|
||||
edges.push({
|
||||
from: lastConditionId, to: aid, direction: '1->2',
|
||||
});
|
||||
}
|
||||
} else {
|
||||
edges.push({
|
||||
from: `action.${i-1}`, to: aid, direction: '1->2',
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
nodes, edges
|
||||
}
|
||||
}
|
||||
@@ -3,26 +3,18 @@ import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_WIDTH } from "./constants";
|
||||
import DAGEdge from "./DAGEdge";
|
||||
import DAGNode from "./DAGNode";
|
||||
import { DAG, Node } from "./types";
|
||||
import { normalizeDAG } from "./utils";
|
||||
import { normalizeDAG } from "./dagFuncs";
|
||||
export interface Props extends DAG {
|
||||
|
||||
onDelete: (nodeId: string) => void;
|
||||
onOpenNode: (nodeId: string) => void;
|
||||
}
|
||||
export default function AutomationDAG({
|
||||
onDelete,
|
||||
onOpenNode,
|
||||
...dag
|
||||
}: Props) {
|
||||
// state
|
||||
const [{nodes, edges}, _setDAG] = useState(normalizeDAG(dag));
|
||||
|
||||
// set state
|
||||
const setDAG = (dag: DAG) => _setDAG(normalizeDAG(dag));
|
||||
const removeNode = (nodeId: string) => () => {
|
||||
const newNodes = {...nodes}
|
||||
delete newNodes[nodeId];
|
||||
_setDAG({
|
||||
nodes: newNodes,
|
||||
edges: edges.filter(e => e.from !== nodeId && e.to !== nodeId),
|
||||
})
|
||||
}
|
||||
const {nodes, edges} = normalizeDAG(dag);
|
||||
// render
|
||||
return <div className="automation-dag" >
|
||||
<svg
|
||||
@@ -48,7 +40,12 @@ export default function AutomationDAG({
|
||||
</g>
|
||||
<g className="automation-dag-nodes">
|
||||
{Object.keys(nodes).map((nodeId) =>
|
||||
<DAGNode key={nodeId} {...nodes[nodeId]} onXClick={removeNode(nodeId)}/>
|
||||
<DAGNode
|
||||
key={nodeId}
|
||||
{...nodes[nodeId]}
|
||||
onXClick={() => onDelete(nodeId)}
|
||||
onOpenClick={() => onOpenNode(nodeId)}
|
||||
/>
|
||||
)}
|
||||
</g>
|
||||
</svg>
|
||||
|
||||
@@ -4,6 +4,7 @@ export type Point = [number, number];
|
||||
export interface Node {
|
||||
loc: Point;
|
||||
text: JSX.Element | string;
|
||||
color?: 'red' | 'green' | 'blue' | 'none';
|
||||
}
|
||||
export interface Edge {
|
||||
from: string;
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
|
||||
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
|
||||
import {clip, computeDAG} from "./utils";
|
||||
|
||||
test('it clips overflowing values', () => {
|
||||
expect(clip([GRAPH_WIDTH, GRAPH_HEIGHT])).toEqual([
|
||||
GRAPH_WIDTH-NODE_WIDTH,
|
||||
GRAPH_HEIGHT-NODE_HEIGHT
|
||||
])
|
||||
expect(clip([-10, -10])).toEqual([
|
||||
0, 0
|
||||
])
|
||||
expect(clip([0, 0])).toEqual([
|
||||
0, 0
|
||||
])
|
||||
})
|
||||
|
||||
test('simple empty input dag', () => {
|
||||
expect(computeDAG({
|
||||
id: "root",
|
||||
alias: "root",
|
||||
description: 'root',
|
||||
mode: 'single',
|
||||
trigger: [],
|
||||
condition: [],
|
||||
action: [
|
||||
],
|
||||
})).toEqual({
|
||||
nodes: {},
|
||||
edges: [
|
||||
],
|
||||
})
|
||||
})
|
||||
@@ -1,58 +0,0 @@
|
||||
|
||||
import { Automation } from "../../automation/types";
|
||||
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
|
||||
import {DAG, Node, Point, NormalizedDag} from "./types";
|
||||
|
||||
|
||||
export const clip = (p: Point) : Point => {
|
||||
const [x,y] = p.map(c => Math.max(0, c));
|
||||
return [Math.min(GRAPH_WIDTH-NODE_WIDTH, x), Math.min(GRAPH_HEIGHT-NODE_HEIGHT, y)]
|
||||
}
|
||||
export const normalizeDAG = (dag: DAG): NormalizedDag => {
|
||||
// let overflow: bo
|
||||
const nodes = Object.keys(dag.nodes).map<[string, Node]>(nodeId => [nodeId, {
|
||||
...dag.nodes[nodeId],
|
||||
loc: clip(dag.nodes[nodeId].loc),
|
||||
}]).reduce((all, [nodeId, next]) => ({
|
||||
[nodeId]: next,
|
||||
...all,
|
||||
}),{});
|
||||
// dag.edges.forEach(edge => {
|
||||
// const n1 = nodes[edge.from];
|
||||
// const n2 = nodes[edge.to];
|
||||
// n2.loc[0] = Math.max(n2.loc[0], n1.loc[0] + 1.25*NODE_WIDTH)
|
||||
// })
|
||||
|
||||
return {
|
||||
nodes,
|
||||
edges: dag.edges,
|
||||
}
|
||||
}
|
||||
export const computeDAG = (automation: Automation) : DAG => {
|
||||
const nodes: DAG['nodes'] = {};
|
||||
const edges: DAG['edges'] = [];
|
||||
|
||||
automation.action
|
||||
|
||||
// let currentNode: InputDagNode | null = rootNode;
|
||||
// let currLoc: Point = [0,0];
|
||||
// let idPath = '';
|
||||
// // let
|
||||
// while (currentNode) {
|
||||
// const n = {
|
||||
// text: currentNode.display,
|
||||
// loc: currLoc,
|
||||
// }
|
||||
// if(idPath) {
|
||||
// nodes[idPath + "." + currentNode.name] = n
|
||||
// edges
|
||||
// } else {
|
||||
// nodes[currentNode.name] = n
|
||||
// }
|
||||
// currentNode = currentNode.next[0]
|
||||
// }
|
||||
|
||||
return {
|
||||
nodes, edges
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import StateManager from "../../automation/StateManager";
|
||||
import { AutomationAction } from "../../automation/types";
|
||||
import { determineNodeTypes, getNameFromAction } from "../../automation/utils";
|
||||
import { determineNodeTypes, getDescriptionFromAutomationNode } from "../../automation/utils";
|
||||
import AutomationNode from "../AutomationNode";
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ export default function ScriptNode({
|
||||
const [nodeType, nodeSubType] = determineNodeTypes(data);
|
||||
const stateManager = new StateManager(nodeType, nodeSubType, data);
|
||||
|
||||
return <AutomationNode title={getNameFromAction(data)} onXClick={onDelete}>
|
||||
return <AutomationNode title={getDescriptionFromAutomationNode(data)} onXClick={onDelete}>
|
||||
{stateManager.renderOptionList()}
|
||||
</AutomationNode>
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useState } from "react";
|
||||
import * as types from "../../automation/types";
|
||||
import { getNameFromAction } from "../../automation/utils";
|
||||
import { getDescriptionFromAutomationNode } from "../../automation/utils";
|
||||
import AutomationNode from "../AutomationNode";
|
||||
import NewNodeModal from "./NewNodeModal";
|
||||
import ScriptNode from "./ScriptNode";
|
||||
|
||||
+42
-29
@@ -1,40 +1,53 @@
|
||||
import { useState } from "react";
|
||||
import AutomationDAG from "../components/AutomationDAG";
|
||||
import { DAG } from "../components/AutomationDAG/types";
|
||||
import { computeDAG } from "../components/AutomationDAG/dagFuncs";
|
||||
// import AutomationEditor from "../components/AutomationEditor";
|
||||
// import sample from "../sample/auto_01.yaml";
|
||||
|
||||
export function Home() {
|
||||
const [dag, setDag] = useState<DAG>(computeDAG({
|
||||
id: "root",
|
||||
alias: "root",
|
||||
description: 'root',
|
||||
mode: 'single',
|
||||
trigger: [
|
||||
{
|
||||
platform: 'time',
|
||||
at: '10:00:00'
|
||||
}
|
||||
],
|
||||
condition: [
|
||||
{
|
||||
condition: 'template',
|
||||
value_template: 'states(switch.kitchen_light) == "on"'
|
||||
}
|
||||
],
|
||||
action: [
|
||||
{
|
||||
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",
|
||||
}
|
||||
}
|
||||
],
|
||||
}))
|
||||
return (
|
||||
<div id="page--home" className="page">
|
||||
<AutomationDAG
|
||||
nodes={{
|
||||
0: {text: '1', loc:[-10,-50]},
|
||||
1: {text: '2', loc:[0,50]},
|
||||
2: {text: '3', loc:[-10,200]},
|
||||
3: {text: '4', loc:[100,200]},
|
||||
4: {text: '6', loc:[200,200]},
|
||||
}}
|
||||
edges={[
|
||||
{
|
||||
from: "0", to: "1",
|
||||
direction: '1->2',
|
||||
},
|
||||
{
|
||||
from: "0", to: "2",
|
||||
direction: '1->2',
|
||||
},
|
||||
{
|
||||
from: "2", to: "3",
|
||||
direction: '1->2',
|
||||
},
|
||||
{
|
||||
from: "1", to: "4",
|
||||
direction: '1->2',
|
||||
},
|
||||
{
|
||||
from: "0", to: "3",
|
||||
direction: '1->2',
|
||||
},
|
||||
]}
|
||||
{...dag}
|
||||
onDelete={nodeId => {
|
||||
const nodes = {...dag.nodes};
|
||||
delete nodes[nodeId];
|
||||
const edges = dag.edges.filter(e => (e.from !== nodeId) && (e.to !== nodeId));
|
||||
setDag({nodes, edges});
|
||||
}}
|
||||
onOpenNode={nodeId => console.log({nodeId})}
|
||||
/>
|
||||
{/* <div
|
||||
style={{
|
||||
|
||||
Reference in new issue
Block a user