diff --git a/webapp/src/components/DAGSvgs/DAGEdge.tsx b/webapp/src/components/DAGSvgs/DAGEdge.tsx index f220f01..eec0d81 100644 --- a/webapp/src/components/DAGSvgs/DAGEdge.tsx +++ b/webapp/src/components/DAGSvgs/DAGEdge.tsx @@ -27,10 +27,6 @@ export const DAGEdge = ({ cx1 = x1 + 2 * l1 * Math.cos(theta) cy2 = y2 + 0.5 * l2 * Math.sin(theta) cx2 = x2 - 2 * l2 * Math.cos(theta) - } else if (theta === Math.PI) { - cy1 *= 1.5 - cx1 = x1 - 2 * l1 * Math.cos(theta) - cy2 *= 0.9 } else if ((theta > Math.PI / 2) && (theta < Math.PI)) { cy2 += l2 * Math.sin(theta) cx2 += 10 * l2 * Math.cos(theta) diff --git a/webapp/src/components/DAGSvgs/DAGNode.stories.tsx b/webapp/src/components/DAGSvgs/DAGNode.stories.tsx index 855db59..ad0a626 100644 --- a/webapp/src/components/DAGSvgs/DAGNode.stories.tsx +++ b/webapp/src/components/DAGSvgs/DAGNode.stories.tsx @@ -25,19 +25,29 @@ export const Basic: ComponentStory = args => -export const WithEdge: ComponentStory = args => = args => -WithEdge.args = { - ...(WithEdge.args ?? {}), +WithFromEdge.args = { + ...(WithFromEdge.args ?? {}), edge: { color: 'white', - loc: [ NODE_WIDTH * 4, NODE_HEIGHT * 2 ] + loc: [NODE_WIDTH * 4, NODE_HEIGHT * 2], + direction: '1->2', } } +export const WithToEdge: ComponentStory = args => + + export const WithManyEdges: ComponentStory = args => diff --git a/webapp/src/components/DAGSvgs/DAGNode.tsx b/webapp/src/components/DAGSvgs/DAGNode.tsx index 464b576..51ce14e 100644 --- a/webapp/src/components/DAGSvgs/DAGNode.tsx +++ b/webapp/src/components/DAGSvgs/DAGNode.tsx @@ -1,7 +1,7 @@ import Color from "chroma-js"; import { PencilIcon } from "components/Icons" import { useNodeStyles } from "./styles" -import { Node, Point } from "types/graphs"; +import { Edge, Node, Point } from "types/graphs"; import { DAGEdge } from "./DAGEdge"; @@ -11,9 +11,11 @@ export interface NodeProp extends Node { width: number; onXClick?: () => void; onOpenClick?: () => void; + debug?: boolean; edge?: { color: string; loc: Point; + direction: Edge['direction']; } } export const DAGNode = ({ @@ -25,6 +27,7 @@ export const DAGNode = ({ onOpenClick = () => { }, color, edge, + debug=false, }: NodeProp) => { const { classes, theme } = useNodeStyles({ color, @@ -51,11 +54,22 @@ export const DAGNode = ({ - {edge && } + { + edge && (edge.direction === '1->2' ? + : + ) + } } diff --git a/webapp/src/components/SequenceNodes/index.stories.tsx b/webapp/src/components/SequenceNodes/index.stories.tsx new file mode 100644 index 0000000..4d2e719 --- /dev/null +++ b/webapp/src/components/SequenceNodes/index.stories.tsx @@ -0,0 +1,68 @@ +import React from "react"; +import { ComponentMeta, ComponentStory } from '@storybook/react'; +import { SequenceNodes } from './index'; +import { SVGBoard } from '../DAGSvgs/Board'; +import { NODE_HEIGHT, NODE_WIDTH } from '../DAGSvgs/constants'; + + +export default { + title: 'DAGSvgs/SequenceNodes', + component: SequenceNodes, + parameters: { actions: { argTypesRegex: '^on.*' } }, + args: { + nodeHeight: NODE_HEIGHT, + nodeWidth: NODE_WIDTH, + distanceFactor: 1.5, + startLoc: [50,50] + } +} as ComponentMeta + +export const Simple: ComponentStory = args => + + + +Simple.args = { + ...Simple.args, + sequence: [ + { + $smType: "condition", + condition: 'and', + condition_data: { + conditions: [ + { + $smType: 'condition', + condition: 'numeric_state', + condition_data: { + entity_id: 'sensor.temperature_kitchen', + below: '15', + } + }, + { + $smType: "condition", + condition: 'template', + condition_data: { + value_template: 'states(switch.kitchen_light) == "on"' + } + } + ] + } + }, + { + $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", + } + } + } + ] +} diff --git a/webapp/src/components/SequenceNodes/index.tsx b/webapp/src/components/SequenceNodes/index.tsx new file mode 100644 index 0000000..4a17b38 --- /dev/null +++ b/webapp/src/components/SequenceNodes/index.tsx @@ -0,0 +1,33 @@ +import { FC } from "react"; +import { useTheme } from "styles/theme"; +import { Point } from "types/graphs"; +import { AutomationSequenceNode } from '../../types/automations/index'; +import { computeNodesEdges } from './location'; + + +export const SequenceNodes: FC<{ + startLoc: Point, + sequence: AutomationSequenceNode[], + nodeHeight: number, + nodeWidth: number, + distanceFactor: number, +}> = ({ + startLoc, + sequence, + nodeHeight, + nodeWidth, + distanceFactor, +}) => { + const theme = useTheme(); + + return <> + {Array.from(computeNodesEdges( + startLoc, + nodeWidth, + nodeHeight, + distanceFactor, + sequence, + theme.secondaryAccent, + ))} + +} diff --git a/webapp/src/components/SequenceNodes/location.tsx b/webapp/src/components/SequenceNodes/location.tsx new file mode 100644 index 0000000..489738b --- /dev/null +++ b/webapp/src/components/SequenceNodes/location.tsx @@ -0,0 +1,40 @@ +import { DAGEdge } from "components/DAGSvgs/DAGEdge"; +import { AutomationSequenceNode } from "types/automations"; +import { Point } from "types/graphs"; +import { DAGNode } from '../DAGSvgs/DAGNode'; + + +export function* computeNodesEdges( + startPoint: Point, + nodeWidth: number, + nodeHeight: number, + distanceFactor: number, + sequence: AutomationSequenceNode[], + edgeColor: string, +): Generator { + let lastStartPoint: Point = [...startPoint]; + let lastNodeLoc: Point | null = null; + for (let i = 0; i < sequence.length; i++) { + const node = sequence[i]; + const nodeLoc: Point = lastStartPoint = [lastStartPoint[0] + nodeWidth * distanceFactor, lastStartPoint[1]]; + // const curretEdgeLoc: Point = [nodeLoc[0], nodeLoc[1] + nodeHeight / 2]; + // const nextAddLoc: Point | null = i < sequence.length ? null : [nodeLoc[0] + nodeWidth * distanceFactor, nodeLoc[1] + nodeHeight / 2]; + yield <> + + + lastNodeLoc = [...nodeLoc] + } + return [startPoint, lastStartPoint] +} diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts new file mode 100644 index 0000000..ba1180e --- /dev/null +++ b/webapp/src/types/automations/actions.ts @@ -0,0 +1,63 @@ +import { AutomationSequenceNode } from "."; +import { AutomationTime } from "./common"; +import { AutomationCondition } from "./conditions"; + +interface AutomationActionNodeBase { + $smType: 'action'; + action: Name; + action_data: Data & { + alias?: string; + variables?: Record; + }; +} + +export interface ServiceAction extends AutomationActionNodeBase<"service", { + service: string; + target: any; + data: any; +}>{} +export interface RepeatAction extends AutomationActionNodeBase<"repeat", { + repeat: { + count: number; + sequence: AutomationAction[]; + } +}>{} +export interface WaitAction extends AutomationActionNodeBase<"wait", { + wait_template: string; + timeout?: AutomationTime; + continue_on_timeout?: boolean; +}>{} +export interface FireEventAction extends AutomationActionNodeBase<"event", { + event: string; + event_data: any; +}>{} +export interface DeviceAction extends AutomationActionNodeBase<"device", { + type: string; + device_id: string; + entity_id?: string; + domain: string; +}>{} +export interface ChooseAction extends AutomationActionNodeBase<"choose", { + choose: Array<{ + conditions: AutomationCondition[]; + sequence: AutomationSequenceNode[]; + }>; + default: AutomationAction[]; +}>{} +export type AutomationAction = + | ServiceAction + | RepeatAction + | WaitAction + | FireEventAction + | DeviceAction + | ChooseAction +; +export const ActionTypes = [ + 'Service', + 'Repeat', + 'Wait', + 'Event', + 'Device', + 'Choose' +] as const; +export type ActionType = typeof ActionTypes[number]; \ No newline at end of file diff --git a/webapp/src/types/automations/common.ts b/webapp/src/types/automations/common.ts new file mode 100644 index 0000000..da7fa54 --- /dev/null +++ b/webapp/src/types/automations/common.ts @@ -0,0 +1,6 @@ +export interface AutomationTime { + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; +} diff --git a/webapp/src/types/automations/conditions.ts b/webapp/src/types/automations/conditions.ts new file mode 100644 index 0000000..26b7e5c --- /dev/null +++ b/webapp/src/types/automations/conditions.ts @@ -0,0 +1,50 @@ +import { AutomationTime } from "./common"; + +interface AutomationConditionNodeBase { + $smType: 'condition'; + condition: Name; + condition_data: Data & { + alias?: string; + }; +} +export interface LogicCondition extends AutomationConditionNodeBase<'and' | 'or' | 'not',{ + conditions: AutomationCondition[]; +}> {} +export interface NumericCondition extends AutomationConditionNodeBase<'numeric_state', { + entity_id: string | string[]; + above?: string; + below?: string; + attribute?: string; + value_template?: string; +}> {} +export interface StateCondition extends AutomationConditionNodeBase<'state',{ + entity_id: string | string[]; + state: string | string[]; + for?: AutomationTime; + attribute?: string; +}>{} +export interface TemplateCondition extends AutomationConditionNodeBase<'template',{ + value_template: string; +}> {} +export interface TimeCondition extends AutomationConditionNodeBase<'time', { + after?: AutomationTime; + before?: AutomationTime; + weekday?: Array<'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun'>; +}> {} +export interface TriggerCondition extends AutomationConditionNodeBase <'trigger', { + id: string | string[]; +}>{} +export interface ZoneCondition extends AutomationConditionNodeBase <'zone', { + zone: string; + entity_id: string | string[]; + state: string | string[]; +}>{} +export type AutomationCondition = + | LogicCondition + | NumericCondition + | StateCondition + | TemplateCondition + | TimeCondition + | TriggerCondition + | ZoneCondition +; diff --git a/webapp/src/types/automations/index.ts b/webapp/src/types/automations/index.ts new file mode 100644 index 0000000..f804c44 --- /dev/null +++ b/webapp/src/types/automations/index.ts @@ -0,0 +1,26 @@ +import { AutomationAction } from "./actions"; +import { AutomationCondition } from "./conditions"; +import { AutomationTrigger } from "./triggers"; + + +export interface AutomationMetadata { + id: string; + alias: string; + description: string; + trigger_variables?: Record; + mode: "single" | "restart" | "queued" | "parallel"; +} +export interface AutomationData { + metadata: AutomationMetadata; + trigger: AutomationTrigger[]; + sequence: AutomationSequenceNode[]; +} +export type AutomationSequenceNode = AutomationAction | AutomationCondition; +type AutomationNodeMapping = { + "trigger": AutomationTrigger; + "action": AutomationAction; + "condition": AutomationCondition; + "sequence": AutomationSequenceNode; +} +export type AutomationNodeTypes = keyof AutomationNodeMapping; +export type AutomationNode = AutomationNodeMapping[N]; \ No newline at end of file diff --git a/webapp/src/types/automations/triggers.ts b/webapp/src/types/automations/triggers.ts new file mode 100644 index 0000000..9ab5ccf --- /dev/null +++ b/webapp/src/types/automations/triggers.ts @@ -0,0 +1,110 @@ +import { AutomationTime } from "./common"; + +interface AutomationTriggerBase { + alias?: string; +} + +export interface AutomationTriggerEvent extends AutomationTriggerBase { + platform: 'event'; + event_type: string | string[]; + event_data?: any; + context?: any; +} +export interface AutomationTriggerHA extends AutomationTriggerBase { + platform: 'homeassistant'; + event: 'start' | 'shutdown'; +} +export interface AutomationTriggerMQTT extends AutomationTriggerBase { + platform: 'mqtt'; + topic: string; + payload: string; + value_template: string; +} +export interface AutomationTriggerNumericState extends AutomationTriggerBase { + platform: 'numeric_state'; + entity_id: string | string[]; + attribute?: string; + value_template?: string; + above?: string; + below?: string; + for?: AutomationTime; +} +export interface AutomationTriggerState extends AutomationTriggerBase { + platform: 'state'; + entity_id: string | string[]; + attribute?: string; + from?: string | string[]; + to?: string | string[]; + for?: AutomationTime; +} +export interface AutomationTriggerTag extends AutomationTriggerBase { + platform: 'tag'; + tag_id: string | string[]; + device_id: string | string[]; +} +export interface AutomationTriggerTemplate extends AutomationTriggerBase { + platform: 'template'; + value_template: string; + for?: AutomationTime; +} +export interface AutomationTriggerTime extends AutomationTriggerBase { + platform: 'time'; + at: string | string[]; +} +export interface AutomationTriggerTimePattern extends AutomationTriggerBase { + platform: 'time_pattern'; + hours?: string; + minutes?: string; + seconds?: string; +} +export interface AutomationTriggerWebhook extends AutomationTriggerBase { + platform: 'webhook'; + webhook_id: string; +} +export interface AutomationTriggerZone extends AutomationTriggerBase { + platform: 'zone'; + entity_id: string; + zone: string; + event: 'enter' | 'leave'; +} +export interface AutomationTriggerDevice extends AutomationTriggerBase { + platform: 'device'; + device_id: string; + domain: string; + type: string; + subtype: string; +} +export interface AutomationTriggerGeneric extends AutomationTriggerBase { + platform: string; +} +export type AutomationTrigger = + | AutomationTriggerEvent + | AutomationTriggerHA + | AutomationTriggerMQTT + | AutomationTriggerNumericState + | AutomationTriggerState + | AutomationTriggerTag + | AutomationTriggerTemplate + | AutomationTriggerTime + | AutomationTriggerTimePattern + | AutomationTriggerWebhook + | AutomationTriggerZone + | AutomationTriggerDevice + | AutomationTriggerGeneric +; +export const triggerTypes = [ + 'Event', + 'Home Assistant', + 'MQTT', + 'Numeric State', + 'State', + 'Tag', + 'Template', + 'Time', + 'Time Pattern', + 'Webhook', + 'Zone', + 'Device', + 'Generic', +] as const; +export type TriggerType = typeof triggerTypes[number]; \ No newline at end of file