diff --git a/web/src/automations/types/triggers.ts b/web/src/automations/types/triggers.ts index 8293dfd..9ab5ccf 100644 --- a/web/src/automations/types/triggers.ts +++ b/web/src/automations/types/triggers.ts @@ -1,22 +1,26 @@ import { AutomationTime } from "./common"; -export interface AutomationTriggerEvent { +interface AutomationTriggerBase { + alias?: string; +} + +export interface AutomationTriggerEvent extends AutomationTriggerBase { platform: 'event'; event_type: string | string[]; event_data?: any; context?: any; } -export interface AutomationTriggerHA { +export interface AutomationTriggerHA extends AutomationTriggerBase { platform: 'homeassistant'; event: 'start' | 'shutdown'; } -export interface AutomationTriggerMQTT { +export interface AutomationTriggerMQTT extends AutomationTriggerBase { platform: 'mqtt'; topic: string; payload: string; value_template: string; } -export interface AutomationTriggerNumericState { +export interface AutomationTriggerNumericState extends AutomationTriggerBase { platform: 'numeric_state'; entity_id: string | string[]; attribute?: string; @@ -25,7 +29,7 @@ export interface AutomationTriggerNumericState { below?: string; for?: AutomationTime; } -export interface AutomationTriggerState { +export interface AutomationTriggerState extends AutomationTriggerBase { platform: 'state'; entity_id: string | string[]; attribute?: string; @@ -33,44 +37,44 @@ export interface AutomationTriggerState { to?: string | string[]; for?: AutomationTime; } -export interface AutomationTriggerTag { +export interface AutomationTriggerTag extends AutomationTriggerBase { platform: 'tag'; tag_id: string | string[]; device_id: string | string[]; } -export interface AutomationTriggerTemplate { +export interface AutomationTriggerTemplate extends AutomationTriggerBase { platform: 'template'; value_template: string; for?: AutomationTime; } -export interface AutomationTriggerTime { +export interface AutomationTriggerTime extends AutomationTriggerBase { platform: 'time'; at: string | string[]; } -export interface AutomationTriggerTimePattern { +export interface AutomationTriggerTimePattern extends AutomationTriggerBase { platform: 'time_pattern'; hours?: string; minutes?: string; seconds?: string; } -export interface AutomationTriggerWebhook { +export interface AutomationTriggerWebhook extends AutomationTriggerBase { platform: 'webhook'; webhook_id: string; } -export interface AutomationTriggerZone { +export interface AutomationTriggerZone extends AutomationTriggerBase { platform: 'zone'; entity_id: string; zone: string; event: 'enter' | 'leave'; } -export interface AutomationTriggerDevice { +export interface AutomationTriggerDevice extends AutomationTriggerBase { platform: 'device'; device_id: string; domain: string; type: string; subtype: string; } -export type AutomationTriggerGeneric = { +export interface AutomationTriggerGeneric extends AutomationTriggerBase { platform: string; } export type AutomationTrigger = diff --git a/web/src/automations/utils.ts b/web/src/automations/utils.ts index 3ab0975..502f3ce 100644 --- a/web/src/automations/utils.ts +++ b/web/src/automations/utils.ts @@ -66,6 +66,11 @@ export const getDescriptionFromAutomationNode = ( } } } + } else { + if (node.alias) { + return node.alias; + } + return node.platform } return 'n/a' } diff --git a/web/src/components/AutomationEditor/DAGEdge.tsx b/web/src/components/AutomationEditor/DAGEdge.tsx index f9b881a..84ee926 100644 --- a/web/src/components/AutomationEditor/DAGEdge.tsx +++ b/web/src/components/AutomationEditor/DAGEdge.tsx @@ -1,4 +1,3 @@ -import { NODE_HEIGHT, NODE_WIDTH } from "./constants" import { EdgeDirection, Point } from "./types" @@ -8,14 +7,17 @@ export interface EdgeProp { p2: Point, direction: EdgeDirection, } -export default ({p1, p2, direction}: EdgeProp) => { - const [x1,y1] = [p1[0] + NODE_WIDTH, p1[1]+NODE_HEIGHT/2] - const [x2,y2] = [p2[0], p2[1]+NODE_HEIGHT/2] +export default ({ + p1: [x1,y1], + p2: [x2,y2], + direction, +}: EdgeProp) => { const [_,my] = [(x1+x2)/2, (y1+y2)/2] - const theta = Math.atan2(y2 -y1, x2-x1) -Math.PI / 2 - const cx1 = x1 + NODE_WIDTH + const theta = Math.atan2(y2 -y1, x2-x1) - Math.PI / 2 + console.log({theta}, Math.sin(theta)) + const cx1 = x1 - 45 * Math.sin(theta) const cy1 = my - 30 * Math.cos(theta) - const cx2 = x2 - NODE_WIDTH + const cx2 = x2 + 45 * Math.sin(theta) const cy2 = my + 30 * Math.cos(theta) return {}, onOpenClick=() => {}, isOpen=false, - nodeType, + color, }: NodeProp) => -
-
-
+
+
+
X
-
{text}
-
+
{text}
+
{isOpen ? "▲" : "▼"}
diff --git a/web/src/components/AutomationEditor/index.tsx b/web/src/components/AutomationEditor/index.tsx index 510914c..28011a6 100644 --- a/web/src/components/AutomationEditor/index.tsx +++ b/web/src/components/AutomationEditor/index.tsx @@ -1,6 +1,13 @@ import { AutomationData } from "~/automations/types"; +import { AutomationCondition } from "~/automations/types/conditions"; +import { AutomationTrigger } from "~/automations/types/triggers"; +import { getDescriptionFromAutomationNode } from "~/automations/utils"; import useWindowSize from "~/utils/useWindowSize"; import AutoInfoBox from "./AutoInfoBox"; +import { NODE_HEIGHT, NODE_WIDTH } from "./constants"; +import DAGEdge from "./DAGEdge"; +import DAGNode from "./DAGNode"; +import { Point } from "./types"; interface Props { automation: AutomationData; @@ -11,11 +18,47 @@ export default function AutomationEditor({ onUpdate, }: Props) { + // state const {ratioWbh} = useWindowSize(); + // alias + const nHDF = 1.5 // node horizontal distance factor const graphHeight = 300; const graphWidth = Math.round(graphHeight * ratioWbh); - console.log({ratioWbh, graphHeight, graphWidth}) + const pointTrigToCond: Point = [NODE_WIDTH*nHDF, NODE_HEIGHT/2] + + // render funcs + const renderTrigger = (trigger: AutomationTrigger, i: number) => { + const nodeLoc: Point = [0 ,i*NODE_HEIGHT*1.25]; + return <> + + + + } + const renderMidCondition = (condition: AutomationCondition, i: number) => { + const nodeLoc: Point = [pointTrigToCond[0] + NODE_WIDTH/2 , 0]; + let lastNodeLoc = pointTrigToCond; + if (i > 0) { + lastNodeLoc = [nodeLoc[0] + NODE_WIDTH*((i - 1)*nHDF+1), nodeLoc[1] + NODE_HEIGHT/2]; + nodeLoc[0] += NODE_WIDTH*i*nHDF + } + return <> + + + + } + + // render return
= 0.75 ? 'row' : 'column' }}> @@ -35,13 +78,15 @@ export default function AutomationEditor({ - {/* */} - {/* - - - - - */} + + {automation.trigger.map(renderTrigger)} + + + {automation.condition.map(renderMidCondition)} + + + +
diff --git a/web/src/components/AutomationEditor/types.ts b/web/src/components/AutomationEditor/types.ts index 8f66ff9..8a6f109 100644 --- a/web/src/components/AutomationEditor/types.ts +++ b/web/src/components/AutomationEditor/types.ts @@ -1,11 +1,8 @@ -import { AutomationNodeTypes } from "../../automations/types"; - - export type Point = [number, number]; export interface Node { loc: Point; text: JSX.Element | string; - nodeType: AutomationNodeTypes; + color: 'red' | 'blue' | 'green'; } export interface Edge { from: string; diff --git a/web/src/pages/Home.tsx b/web/src/pages/Home.tsx index 7e8f94c..c8f0255 100644 --- a/web/src/pages/Home.tsx +++ b/web/src/pages/Home.tsx @@ -11,19 +11,42 @@ export function Home() { mode: 'single', }, trigger: [ - { - platform: 'time', - at: '10:00:00' - } + { + platform: 'time', + at: '10:00:00' + }, + { + platform: "device", + device_id: "34749", + domain: "zha", + type: "remote_button_double_press", + subtype: "remote_button_double_press" + } ], condition: [ { - $smType: "condition", - condition: 'template', - condition_data: { - value_template: 'states(switch.kitchen_light) == "on"' - } - } + $smType: "condition", + condition: 'template', + condition_data: { + value_template: 'states(switch.kitchen_light) == "on"' + } + }, + { + $smType: "condition", + condition: 'and', + condition_data: { + conditions: [ + { + $smType: 'condition', + condition: 'numeric_state', + condition_data: { + entity_id: 'sensor.temperature_kitchen', + below: '15', + } + } + ] + } + } ], action: [ { diff --git a/web/src/styles/components/AutomationDAG.scss b/web/src/styles/components/AutomationDAG.scss index 10b2952..834183a 100644 --- a/web/src/styles/components/AutomationDAG.scss +++ b/web/src/styles/components/AutomationDAG.scss @@ -14,6 +14,13 @@ width: 99%; padding: 0; margin: 0; + + circle { + fill: adjust-color($color: $secondary, $green: 125, $blue: 125); + &.blue { + fill: adjust-color($color: $secondary, $blue: 125); + } + } } }