diff --git a/webapp/src/components/DAGFlow/index.tsx b/webapp/src/components/DAGFlow/index.tsx index c72844f..ab5b0a8 100644 --- a/webapp/src/components/DAGFlow/index.tsx +++ b/webapp/src/components/DAGFlow/index.tsx @@ -16,6 +16,7 @@ import { AutomationCondition } from "types/automations/conditions"; import { makeConditionPoint } from "./flowDataMods"; import { convertToFlowNode } from "./helpers"; import { DAGErrorNode } from "./DAGErrorNode"; +import { useHA } from "haService"; const nodeTypes = { 'dagnode': convertToFlowNode(DAGNode), @@ -57,10 +58,9 @@ export const DAGAutomationFlow: FC = ({ }) => { // state const [modalState, setModalState] = useState(null); - + const { namer, } = useHA(); // modal let modalBody = <> - console.log(modalState) if (modalState) { if (modalState.single) { modalBody = = ({ edges: [], } triggerToFlow(flowData, trigger, condPoint.id, dims, { + namer, onAdd: () => onTriggerUpdate([ ...trigger, { platform: 'event', @@ -136,6 +137,7 @@ export const DAGAutomationFlow: FC = ({ y: dims.padding.y, } }, + namer, makeSequenceUpdater(sequence, onSequenceUpdate, setModalState), ); // render diff --git a/webapp/src/components/DAGFlow/sequenceConvertor.ts b/webapp/src/components/DAGFlow/sequenceConvertor.ts index 68ed966..335bbef 100644 --- a/webapp/src/components/DAGFlow/sequenceConvertor.ts +++ b/webapp/src/components/DAGFlow/sequenceConvertor.ts @@ -1,6 +1,6 @@ import { AutomationSequenceNode } from 'types/automations'; import { DAGAutomationFlowDims, FlowData } from './types'; -import { getDescriptionFromAutomationNode } from 'utils/formatting'; +import { getDescriptionFromAutomationNode, Namer } from 'utils/formatting'; import { XYPosition } from 'react-flow-renderer'; import { ChooseAction } from 'types/automations/actions'; import { makeFlowCircle } from './DAGCircle'; @@ -14,6 +14,7 @@ export const sequenceToFlow = ( sequence: AutomationSequenceNode[], fromPoint: string, dims: DAGAutomationFlowDims, + namer: Namer, updater: SequenceUpdater, prefix: string = "", ) => { @@ -45,7 +46,7 @@ export const sequenceToFlow = ( if (getNodeType(node) === 'action') { if ('choose' in node) { try { - const out = addChooseNode(node, position, nodeId, dims, updater, i); + const out = addChooseNode(node, position, nodeId, dims, namer, updater, i); position = out.lastPos flowData = mergeFlowDatas(flowData, out.flowData); } catch (err) { @@ -55,13 +56,15 @@ export const sequenceToFlow = ( } else { addSingleNode(flowData, node, position, nodeId, dims, { onEditClick: updater.makeOnModalOpenForNode(i, node), - onXClick: () => updater.removeNode(i) + onXClick: () => updater.removeNode(i), + namer, }); } } else { addSingleNode(flowData, node, position, nodeId, dims, { onEditClick: updater.makeOnModalOpenForNode(i, node), - onXClick: () => updater.removeNode(i) + onXClick: () => updater.removeNode(i), + namer, }); } @@ -124,12 +127,13 @@ const addSingleNode = ( opts: { onEditClick: () => void; onXClick: () => void; + namer: Namer; } ) => flowData.nodes.push({ id: nodeId, type: 'dagnode', data: { - label: getDescriptionFromAutomationNode(node), + label: getDescriptionFromAutomationNode(node, opts.namer), height: nodeHeight, width: nodeWidth, color: getNodeType(node) === 'action' ? 'green' : 'blue', @@ -171,6 +175,7 @@ const addChooseNode = ( position: XYPosition, nodeId: string, dims: DAGAutomationFlowDims, + namer: Namer, updater: SequenceUpdater, i: number, ) => { @@ -181,6 +186,7 @@ const addChooseNode = ( addSingleNode(flowData, node, position, nodeId, dims, { onEditClick: updater.makeOnModalOpenForNode(i, node), onXClick: () => updater.removeNode(i), + namer, }); let lastPos: XYPosition = position; // conditions @@ -230,7 +236,7 @@ const addChooseNode = ( x: condNode.position.x - (dims.nodeWidth + dims.conditionWidth), y: condNode.position.y - dims.conditionHeight / 2, }, - }, updater.makeChildUpdaterForChooseAction(i, j), `${sequenceId}.`); + }, namer, updater.makeChildUpdaterForChooseAction(i, j), `${sequenceId}.`); if (lastPoint.position) { lastPos = xyApply(lastPos, lastPoint.position, Math.max) } else { @@ -290,7 +296,7 @@ const addChooseNode = ( x: position.x - dims.nodeWidth, y: elseCircle.position.y - dims.circleSize / 4, }, - }, updater.makeChildUpdaterForChooseAction(i, null), `${nodeId}.${totalChildren}.`) + }, namer, updater.makeChildUpdaterForChooseAction(i, null), `${nodeId}.${totalChildren}.`) if (lastPoint.position) { lastPos = xyApply(lastPos, lastPoint.position, Math.max) } else { diff --git a/webapp/src/components/DAGFlow/triggerConvertor.ts b/webapp/src/components/DAGFlow/triggerConvertor.ts index b419583..c9d7ef9 100644 --- a/webapp/src/components/DAGFlow/triggerConvertor.ts +++ b/webapp/src/components/DAGFlow/triggerConvertor.ts @@ -16,7 +16,6 @@ export const triggerToFlow = ( nodeWidth, padding, conditionHeight, - conditionWidth, circleSize, flipped, } = dims; @@ -27,7 +26,7 @@ export const triggerToFlow = ( id: flowId, type: 'dagnode', data: { - label: getDescriptionFromAutomationNode(t), + label: getDescriptionFromAutomationNode(t, opts.namer), height: nodeHeight, width: nodeWidth, flipped, diff --git a/webapp/src/components/DAGFlow/types.ts b/webapp/src/components/DAGFlow/types.ts index ece7e70..1a8c18b 100644 --- a/webapp/src/components/DAGFlow/types.ts +++ b/webapp/src/components/DAGFlow/types.ts @@ -5,6 +5,7 @@ import { DAGNodeProps } from './DAGNode'; import { AutomationTrigger } from 'types/automations/triggers'; import { MultiNodeEditorProps } from 'components/MultiNodeEditors/types'; import { DAGErrorNodeProps } from './DAGErrorNode'; +import { Namer } from 'utils/formatting'; export type DAGAutomationFlowDims = { @@ -28,6 +29,7 @@ export type TriggerMakerOptions = { onAdd: () => void, onEdit: (i: number) => void, onDelete: (i: number) => void, + namer: Namer, } export type UpdateModalState = ( diff --git a/webapp/src/components/NodeEditor/OptionManager/index.tsx b/webapp/src/components/NodeEditor/OptionManager/index.tsx index 5ad5327..dd247b6 100644 --- a/webapp/src/components/NodeEditor/OptionManager/index.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/index.tsx @@ -6,7 +6,7 @@ import { getNodeSubType, getSubTypeList, validateNode } from 'utils/automations' import InputYaml from 'components/Inputs/InputYaml'; import InputText from 'components/Inputs/InputText'; import { getDescriptionFromAutomationNode } from 'utils/formatting'; -import { useHAEntities } from 'haService'; +import { useHA } from 'haService'; import { Generic } from './Generic'; import { getNodeTypeAndValidate } from '../../../utils/automations'; @@ -19,7 +19,7 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool isModified: false, ...computeInvalidYaml(originalNode, allowedTypes) }); - const entities = useHAEntities(); + const { entities, namer } = useHA(); // aliases const { node: currentNode, isModified, yamlMode, invalidManualYaml } = allState; // effects @@ -88,7 +88,7 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool aliasEditor = setState({ ...allState.node, alias diff --git a/webapp/src/haService/index.ts b/webapp/src/haService/index.ts index b927362..4648496 100644 --- a/webapp/src/haService/index.ts +++ b/webapp/src/haService/index.ts @@ -1,4 +1,5 @@ import { entitiesColl, servicesColl } from "home-assistant-js-websocket" +import { Namer } from "utils/formatting"; import { deviceRegistryColl } from "./DeviceRegistry"; import { entityRegistryColl } from "./EntityRegistry"; import { DeviceRegistryItem } from "./types"; @@ -120,4 +121,27 @@ export const useHAEntityRegistry = () => { const dr = useHassCollection(entityRegistryColl) return dr +} + +export const useHA = () => { + const entities = useHAEntities(); + const devices = useHADeviceRegistry(); + + const namer: Namer = { + getDeviceName(device_id) { + return devices.getLabel(device_id) + }, + getEntityName(entity_id, maxEntities = 1) { + if (Array.isArray(entity_id)) { + return entity_id.slice(0, maxEntities).map(entities.getLabel).join(', ') + } + return entities.getLabel(entity_id) + }, + } + + return { + entities, + devices, + namer, + } } \ No newline at end of file diff --git a/webapp/src/utils/formatting.test.ts b/webapp/src/utils/formatting.test.ts index 1469f32..f6c154d 100644 --- a/webapp/src/utils/formatting.test.ts +++ b/webapp/src/utils/formatting.test.ts @@ -1,6 +1,18 @@ -import { convertTimeToString, getDescriptionFromAutomationNode, prettyName } from "./formatting"; +import { convertTimeToString, getDescriptionFromAutomationNode, Namer, prettyName } from "./formatting"; +const dummyNamer: Namer = { + getDeviceName(device_id) { + return device_id + }, + getEntityName(entity_id, maxEntities = 1) { + if (Array.isArray(entity_id)) { + return entity_id.slice(0, maxEntities).join(' and ') + } else { + return entity_id + } + }, +} test('converting time to a string', () => { expect(convertTimeToString({ hours: 1 })).toEqual('01:00:00:00') expect(convertTimeToString({ hours: 1, minutes: 5 })).toEqual('01:05:00:00') @@ -19,7 +31,7 @@ test('using alias as description', () => { media_content_id: "Good Morning", media_content_type: "SPOTIFY", } - })).toEqual("Start Music In Kitchen") + }, dummyNamer)).toEqual("Start Music In Kitchen") }) @@ -33,7 +45,7 @@ test('get service action description', () => { media_content_id: "Good Morning", media_content_type: "SPOTIFY", } - })).toEqual("media_player.play_media") + }, dummyNamer)).toEqual("media_player.play_media") }) test('get repeat action description', () => { @@ -42,27 +54,27 @@ test('get repeat action description', () => { count: 10, sequence: [] } - })).toEqual("Repeat 10") + }, dummyNamer)).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'") + }, dummyNamer)).toEqual("Wait on states(switch.kitchen_light) == 'on'") expect(getDescriptionFromAutomationNode({ wait_template: "states(switch.kitchen_light) == 'on'", timeout: { 'minutes': 1, } - })).toEqual("Wait on states(switch.kitchen_light) == 'on' for 00:01:00:00") + }, dummyNamer)).toEqual("Wait on states(switch.kitchen_light) == 'on' for 00:01:00:00") }) test('get fire event action description', () => { expect(getDescriptionFromAutomationNode({ event: 'test_event', event_data: {}, - })).toEqual("Trigger test_event") + }, dummyNamer)).toEqual("Trigger test_event") }) test('get device action description', () => { @@ -70,63 +82,63 @@ test('get device action description', () => { device_id: "12310das01231", domain: "zwave_js", type: "set_value" - })).toEqual("set_value on 12310das01231") + }, dummyNamer)).toEqual("set_value on 12310das01231") }) test('get choose action description', () => { expect(getDescriptionFromAutomationNode({ choose: [], default: [], - })).toEqual("Choose") + }, dummyNamer)).toEqual("Choose") }) test('get and/or/not condition description', () => { expect(getDescriptionFromAutomationNode({ condition: 'and', conditions: [], - })).toEqual("Logic") + }, dummyNamer)).toEqual("Logic") expect(getDescriptionFromAutomationNode({ condition: 'or', conditions: [], - })).toEqual("Logic") + }, dummyNamer)).toEqual("Logic") expect(getDescriptionFromAutomationNode({ condition: 'not', conditions: [], - })).toEqual("Logic") + }, dummyNamer)).toEqual("Logic") }) test('get Numeric State condition description', () => { expect(getDescriptionFromAutomationNode({ condition: 'numeric_state', entity_id: 'sensor.temperature', - })).toEqual("sensor.temperature?>?<") + }, dummyNamer)).toEqual("sensor.temperature?>?<") expect(getDescriptionFromAutomationNode({ condition: 'numeric_state', entity_id: "" - })).toEqual("Numeric State Condition") + }, dummyNamer)).toEqual("Numeric State Condition") expect(getDescriptionFromAutomationNode({ condition: "numeric_state", entity_id: 'sensor.temperature', above: '17', - })).toEqual("sensor.temperature > 17") + }, dummyNamer)).toEqual("sensor.temperature > 17") expect(getDescriptionFromAutomationNode({ condition: 'numeric_state', entity_id: 'sensor.temperature', below: '5', - })).toEqual("sensor.temperature < 5") + }, dummyNamer)).toEqual("sensor.temperature < 5") expect(getDescriptionFromAutomationNode({ condition: 'numeric_state', entity_id: 'sensor.temperature', above: '17', below: '20', - })).toEqual("17 < sensor.temperature < 20") + }, dummyNamer)).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") + }, dummyNamer)).toEqual("17 < sensor.temperature < 20") expect(getDescriptionFromAutomationNode({ condition: 'numeric_state', entity_id: [ @@ -136,7 +148,7 @@ test('get Numeric State condition description', () => { above: '17', below: '20', value_template: "{{ float(state.state) + 2 }}" - })).toEqual("17 < ... < 20") + }, dummyNamer)).toEqual("17 < ... < 20") }) test('get State condition description', () => { @@ -144,7 +156,7 @@ test('get State condition description', () => { condition: 'state', entity_id: 'switch.kitchen_light', state: "on" - })).toEqual("switch.kitchen_light is 'on'") + }, dummyNamer)).toEqual("switch.kitchen_light is 'on'") }) diff --git a/webapp/src/utils/formatting.ts b/webapp/src/utils/formatting.ts index 19ddb56..7793379 100644 --- a/webapp/src/utils/formatting.ts +++ b/webapp/src/utils/formatting.ts @@ -2,6 +2,11 @@ import { AutomationNode, AutomationNodeTypes } from "types/automations"; import { AutomationTime } from "types/automations/common"; +export interface Namer { + getEntityName(entity_id: string | string[], maxEntities?: number): string; + getDeviceName(device_id: string): string; +} + export const convertTimeToString = (t: AutomationTime) => { if (typeof t === 'string') { return t @@ -15,7 +20,10 @@ export const convertTimeToString = (t: AutomationTime) => { return [hours, minutes, seconds, milliseconds].map(n => String(n).padStart(2, '0')).join(':') } } -export const getDescriptionFromAutomationNode = (node: AutomationNode): string => { +export const getDescriptionFromAutomationNode = ( + node: AutomationNode, + namer: Namer, +): string => { if (('alias' in node) && node.alias) { return node.alias; } @@ -40,9 +48,9 @@ export const getDescriptionFromAutomationNode = ( if (node.platform === 'state') { let out = ""; if (typeof node.entity_id === 'string') { - out = node.entity_id + out = namer.getEntityName(node.entity_id) } else { - out = node.entity_id[0] + " and more" + out = namer.getEntityName(node.entity_id[0]) + " and more" } if (node.from) { out += ` from ${node.from}` @@ -52,6 +60,9 @@ export const getDescriptionFromAutomationNode = ( } return out } + if (node.platform === 'device') { + return node.device_id ? namer.getDeviceName(node.device_id) : node.entity_id ? namer.getEntityName(node.entity_id) : node.platform + } return node.platform } if ('condition' in node) { @@ -65,7 +76,9 @@ export const getDescriptionFromAutomationNode = ( switch (node.condition) { case "numeric_state": if (node.entity_id) { - const center = prettyEntityId(node.entity_id); + const center = (Array.isArray(node.entity_id) && node.entity_id.length > 1) ? + '...' : + namer.getEntityName(node.entity_id); if (node.above) { if (node.below) { return `${node.above} < ${center} < ${node.below}` @@ -79,7 +92,7 @@ export const getDescriptionFromAutomationNode = ( } return "Numeric State Condition" case "state": - return `${node.entity_id} is '${node.state}'` + return `${namer.getEntityName(node.entity_id)} is '${node.state}'` case 'template': return node.value_template default: @@ -104,7 +117,7 @@ export const getDescriptionFromAutomationNode = ( return `Trigger ${node.event}` } if ('device_id' in node) { - return `${node.type ?? ""} on ${node.device_id ?? ""}` + return `${node.type ?? ""} on ${node.device_id ? namer.getDeviceName(node.device_id) : ''}` } if ('choose' in node) { return "Choose" @@ -112,16 +125,6 @@ export const getDescriptionFromAutomationNode = ( return 'n/a' } -export const prettyEntityId = (entityId: string | string[]) => { - if (typeof entityId === 'string') { - return entityId - } else if (entityId.length === 1) { - return entityId[0] - } else { - return "..." - } -} - export const prettyName = (n: string) => { const clean = n.replaceAll(/[.-_]/g, ' '); return clean.split(' ').map(x => x.slice(0, 1).toUpperCase() + x.slice(1)).join(' ')