initial graph view

This commit is contained in:
Ari Sosnovsky committed 2021-11-12 15:58:44 -05:00
1 parent 12d086a5c0
commit 6d4fe7749a
8 files changed
+130 -47

No files matched your search

+17 -13
View File
@@ -1,22 +1,26 @@
import { AutomationTime } from "./common"; import { AutomationTime } from "./common";
export interface AutomationTriggerEvent { interface AutomationTriggerBase {
alias?: string;
}
export interface AutomationTriggerEvent extends AutomationTriggerBase {
platform: 'event'; platform: 'event';
event_type: string | string[]; event_type: string | string[];
event_data?: any; event_data?: any;
context?: any; context?: any;
} }
export interface AutomationTriggerHA { export interface AutomationTriggerHA extends AutomationTriggerBase {
platform: 'homeassistant'; platform: 'homeassistant';
event: 'start' | 'shutdown'; event: 'start' | 'shutdown';
} }
export interface AutomationTriggerMQTT { export interface AutomationTriggerMQTT extends AutomationTriggerBase {
platform: 'mqtt'; platform: 'mqtt';
topic: string; topic: string;
payload: string; payload: string;
value_template: string; value_template: string;
} }
export interface AutomationTriggerNumericState { export interface AutomationTriggerNumericState extends AutomationTriggerBase {
platform: 'numeric_state'; platform: 'numeric_state';
entity_id: string | string[]; entity_id: string | string[];
attribute?: string; attribute?: string;
@@ -25,7 +29,7 @@ export interface AutomationTriggerNumericState {
below?: string; below?: string;
for?: AutomationTime; for?: AutomationTime;
} }
export interface AutomationTriggerState { export interface AutomationTriggerState extends AutomationTriggerBase {
platform: 'state'; platform: 'state';
entity_id: string | string[]; entity_id: string | string[];
attribute?: string; attribute?: string;
@@ -33,44 +37,44 @@ export interface AutomationTriggerState {
to?: string | string[]; to?: string | string[];
for?: AutomationTime; for?: AutomationTime;
} }
export interface AutomationTriggerTag { export interface AutomationTriggerTag extends AutomationTriggerBase {
platform: 'tag'; platform: 'tag';
tag_id: string | string[]; tag_id: string | string[];
device_id: string | string[]; device_id: string | string[];
} }
export interface AutomationTriggerTemplate { export interface AutomationTriggerTemplate extends AutomationTriggerBase {
platform: 'template'; platform: 'template';
value_template: string; value_template: string;
for?: AutomationTime; for?: AutomationTime;
} }
export interface AutomationTriggerTime { export interface AutomationTriggerTime extends AutomationTriggerBase {
platform: 'time'; platform: 'time';
at: string | string[]; at: string | string[];
} }
export interface AutomationTriggerTimePattern { export interface AutomationTriggerTimePattern extends AutomationTriggerBase {
platform: 'time_pattern'; platform: 'time_pattern';
hours?: string; hours?: string;
minutes?: string; minutes?: string;
seconds?: string; seconds?: string;
} }
export interface AutomationTriggerWebhook { export interface AutomationTriggerWebhook extends AutomationTriggerBase {
platform: 'webhook'; platform: 'webhook';
webhook_id: string; webhook_id: string;
} }
export interface AutomationTriggerZone { export interface AutomationTriggerZone extends AutomationTriggerBase {
platform: 'zone'; platform: 'zone';
entity_id: string; entity_id: string;
zone: string; zone: string;
event: 'enter' | 'leave'; event: 'enter' | 'leave';
} }
export interface AutomationTriggerDevice { export interface AutomationTriggerDevice extends AutomationTriggerBase {
platform: 'device'; platform: 'device';
device_id: string; device_id: string;
domain: string; domain: string;
type: string; type: string;
subtype: string; subtype: string;
} }
export type AutomationTriggerGeneric = { export interface AutomationTriggerGeneric extends AutomationTriggerBase {
platform: string; platform: string;
} }
export type AutomationTrigger = export type AutomationTrigger =
+5
View File
@@ -66,6 +66,11 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
} }
} }
} }
} else {
if (node.alias) {
return node.alias;
}
return node.platform
} }
return 'n/a' return 'n/a'
} }
@@ -1,4 +1,3 @@
import { NODE_HEIGHT, NODE_WIDTH } from "./constants"
import { EdgeDirection, Point } from "./types" import { EdgeDirection, Point } from "./types"
@@ -8,14 +7,17 @@ export interface EdgeProp {
p2: Point, p2: Point,
direction: EdgeDirection, direction: EdgeDirection,
} }
export default ({p1, p2, direction}: EdgeProp) => { export default ({
const [x1,y1] = [p1[0] + NODE_WIDTH, p1[1]+NODE_HEIGHT/2] p1: [x1,y1],
const [x2,y2] = [p2[0], p2[1]+NODE_HEIGHT/2] p2: [x2,y2],
direction,
}: EdgeProp) => {
const [_,my] = [(x1+x2)/2, (y1+y2)/2] const [_,my] = [(x1+x2)/2, (y1+y2)/2]
const theta = Math.atan2(y2 -y1, x2-x1) -Math.PI / 2 const theta = Math.atan2(y2 -y1, x2-x1) - Math.PI / 2
const cx1 = x1 + NODE_WIDTH console.log({theta}, Math.sin(theta))
const cx1 = x1 - 45 * Math.sin(theta)
const cy1 = my - 30 * Math.cos(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) const cy2 = my + 30 * Math.cos(theta)
return <path className="edge" return <path className="edge"
@@ -18,15 +18,15 @@ export default ({
onXClick=() => {}, onXClick=() => {},
onOpenClick=() => {}, onOpenClick=() => {},
isOpen=false, isOpen=false,
nodeType, color,
}: NodeProp) => <foreignObject x={x} y={y} width={width} height={height}> }: NodeProp) => <foreignObject x={x} y={y} width={width} height={height}>
<div className="automation-dag--node"> <div className="automation-editor--node">
<div className={`automation-dag--node-inner ${nodeType}`}> <div className={`automation-editor--node-inner top-color ${color}`}>
<div className="automation-dag--node-inner-edge left" onClick={onXClick}> <div className="automation-editor--node-inner-edge left" onClick={onXClick}>
<div className="automation-node-side-button">X</div> <div className="automation-node-side-button">X</div>
</div> </div>
<div className="automation-dag--node-inner-text">{text}</div> <div className="automation-editor--node-inner-text">{text}</div>
<div className="automation-dag--node-inner-edge right" onClick={onOpenClick}> <div className="automation-editor--node-inner-edge right" onClick={onOpenClick}>
<div className="automation-node-side-button">{isOpen ? "▲" : "▼"}</div> <div className="automation-node-side-button">{isOpen ? "▲" : "▼"}</div>
</div> </div>
</div> </div>
+52 -7
View File
@@ -1,6 +1,13 @@
import { AutomationData } from "~/automations/types"; 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 useWindowSize from "~/utils/useWindowSize";
import AutoInfoBox from "./AutoInfoBox"; 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 { interface Props {
automation: AutomationData; automation: AutomationData;
@@ -11,11 +18,47 @@ export default function AutomationEditor({
onUpdate, onUpdate,
}: Props) { }: Props) {
// state
const {ratioWbh} = useWindowSize(); const {ratioWbh} = useWindowSize();
// alias
const nHDF = 1.5 // node horizontal distance factor
const graphHeight = 300; const graphHeight = 300;
const graphWidth = Math.round(graphHeight * ratioWbh); 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 <>
<DAGNode key={`node.${i}`} loc={nodeLoc} text={getDescriptionFromAutomationNode(trigger)} color="red"/>
<DAGEdge
key={`edge.${i}`}
p1={[NODE_WIDTH , nodeLoc[1] + 0.5 * NODE_HEIGHT]}
p2={pointTrigToCond}
direction="1->2"
/>
</>
}
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 <>
<DAGNode key={`node.${i}`} loc={nodeLoc} text={getDescriptionFromAutomationNode(condition)} color="blue"/>
<DAGEdge
key={`edge.${i}`}
p1={lastNodeLoc}
p2={[nodeLoc[0], nodeLoc[1] + NODE_HEIGHT/2]}
direction="1->2"
/>
</>
}
// render
return <div className="automation-editor" style={{ return <div className="automation-editor" style={{
flexDirection: ratioWbh >= 0.75 ? 'row' : 'column' flexDirection: ratioWbh >= 0.75 ? 'row' : 'column'
}}> }}>
@@ -35,13 +78,15 @@ export default function AutomationEditor({
<path d="M 0 0 L 10 5 L 0 10 z" /> <path d="M 0 0 L 10 5 L 0 10 z" />
</marker> </marker>
</defs> </defs>
{/* <rect x="1" y="1" height={GRAPH_HEIGHT-1} width={GRAPH_WIDTH} stroke="white" fill="none"/> */} <g className="triggers">
{/* <g className="automation-dag-edges"> {automation.trigger.map(renderTrigger)}
</g>
<g className="conditions">
{automation.condition.map(renderMidCondition)}
</g>
<circle className="blue" cx={pointTrigToCond[0]} cy={pointTrigToCond[1]} r={2}/>
<g className="actions">
</g> </g>
<g className="automation-dag-nodes">
</g> */}
</svg> </svg>
</div> </div>
</div> </div>
+1 -4
View File
@@ -1,11 +1,8 @@
import { AutomationNodeTypes } from "../../automations/types";
export type Point = [number, number]; export type Point = [number, number];
export interface Node { export interface Node {
loc: Point; loc: Point;
text: JSX.Element | string; text: JSX.Element | string;
nodeType: AutomationNodeTypes; color: 'red' | 'blue' | 'green';
} }
export interface Edge { export interface Edge {
from: string; from: string;
+33 -10
View File
@@ -11,19 +11,42 @@ export function Home() {
mode: 'single', mode: 'single',
}, },
trigger: [ trigger: [
{ {
platform: 'time', platform: 'time',
at: '10:00:00' at: '10:00:00'
} },
{
platform: "device",
device_id: "34749",
domain: "zha",
type: "remote_button_double_press",
subtype: "remote_button_double_press"
}
], ],
condition: [ condition: [
{ {
$smType: "condition", $smType: "condition",
condition: 'template', condition: 'template',
condition_data: { condition_data: {
value_template: 'states(switch.kitchen_light) == "on"' 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: [ action: [
{ {
@@ -14,6 +14,13 @@
width: 99%; width: 99%;
padding: 0; padding: 0;
margin: 0; margin: 0;
circle {
fill: adjust-color($color: $secondary, $green: 125, $blue: 125);
&.blue {
fill: adjust-color($color: $secondary, $blue: 125);
}
}
} }
} }