initial graph view
This commit is contained in:
1 parent
12d086a5c0
commit
6d4fe7749a
8 files changed
+131
-48
No files matched your search
@@ -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 =
|
||||
|
||||
@@ -66,6 +66,11 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (node.alias) {
|
||||
return node.alias;
|
||||
}
|
||||
return node.platform
|
||||
}
|
||||
return 'n/a'
|
||||
}
|
||||
|
||||
@@ -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 <path className="edge"
|
||||
|
||||
@@ -18,15 +18,15 @@ export default ({
|
||||
onXClick=() => {},
|
||||
onOpenClick=() => {},
|
||||
isOpen=false,
|
||||
nodeType,
|
||||
color,
|
||||
}: NodeProp) => <foreignObject x={x} y={y} width={width} height={height}>
|
||||
<div className="automation-dag--node">
|
||||
<div className={`automation-dag--node-inner ${nodeType}`}>
|
||||
<div className="automation-dag--node-inner-edge left" onClick={onXClick}>
|
||||
<div className="automation-editor--node">
|
||||
<div className={`automation-editor--node-inner top-color ${color}`}>
|
||||
<div className="automation-editor--node-inner-edge left" onClick={onXClick}>
|
||||
<div className="automation-node-side-button">X</div>
|
||||
</div>
|
||||
<div className="automation-dag--node-inner-text">{text}</div>
|
||||
<div className="automation-dag--node-inner-edge right" onClick={onOpenClick}>
|
||||
<div className="automation-editor--node-inner-text">{text}</div>
|
||||
<div className="automation-editor--node-inner-edge right" onClick={onOpenClick}>
|
||||
<div className="automation-node-side-button">{isOpen ? "▲" : "▼"}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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 <>
|
||||
<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={{
|
||||
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" />
|
||||
</marker>
|
||||
</defs>
|
||||
{/* <rect x="1" y="1" height={GRAPH_HEIGHT-1} width={GRAPH_WIDTH} stroke="white" fill="none"/> */}
|
||||
{/* <g className="automation-dag-edges">
|
||||
|
||||
</g>
|
||||
<g className="automation-dag-nodes">
|
||||
|
||||
</g> */}
|
||||
<g className="triggers">
|
||||
{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>
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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;
|
||||
|
||||
+33
-10
@@ -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: [
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user