added condition editor for device
This commit is contained in:
1 parent
1ce934fd54
commit
618a62affe
9 files changed
+387
-256
No files matched your search
@@ -46,8 +46,9 @@ export const getSubTypeList = <T extends AutomationNodeTypes>(
|
||||
"or",
|
||||
"and",
|
||||
"not",
|
||||
"numeric_state",
|
||||
"state",
|
||||
"device",
|
||||
"numeric_state",
|
||||
"template",
|
||||
"time",
|
||||
"trigger",
|
||||
@@ -125,7 +126,7 @@ export const getNodeSubType = <T extends AutomationNodeTypes>(
|
||||
}
|
||||
};
|
||||
|
||||
const TriggerValidators: Record<TriggerType, any> = {
|
||||
export const TriggerValidators: Record<TriggerType, any> = {
|
||||
homeassistant: v.triggers.AutomationTriggerHA,
|
||||
device: v.triggers.AutomationTriggerDevice,
|
||||
event: v.triggers.AutomationTriggerEvent,
|
||||
@@ -140,7 +141,7 @@ const TriggerValidators: Record<TriggerType, any> = {
|
||||
zone: v.triggers.AutomationTriggerZone,
|
||||
};
|
||||
|
||||
const ActionValidators: Record<ActionType, any> = {
|
||||
export const ActionValidators: Record<ActionType, any> = {
|
||||
choose: v.actions.ChooseAction,
|
||||
device: v.actions.DeviceAction,
|
||||
event: v.actions.FireEventAction,
|
||||
@@ -149,10 +150,11 @@ const ActionValidators: Record<ActionType, any> = {
|
||||
wait: st.union([v.actions.DelayAction, v.actions.WaitAction]),
|
||||
};
|
||||
|
||||
const ConditionValidators: Record<ConditionType, any> = {
|
||||
export const ConditionValidators: Record<ConditionType, any> = {
|
||||
and: v.conditions.LogicCondition,
|
||||
or: v.conditions.LogicCondition,
|
||||
not: v.conditions.LogicCondition,
|
||||
device: v.conditions.DeviceCondition,
|
||||
numeric_state: v.conditions.NumericCondition,
|
||||
state: v.conditions.StateCondition,
|
||||
template: v.conditions.TemplateCondition,
|
||||
@@ -161,7 +163,7 @@ const ConditionValidators: Record<ConditionType, any> = {
|
||||
zone: v.conditions.ZoneCondition,
|
||||
};
|
||||
|
||||
const AllValidators: Record<AutomationNodeTypes, any> = {
|
||||
export const AllValidators: Record<AutomationNodeTypes, any> = {
|
||||
action: ActionValidators,
|
||||
condition: ConditionValidators,
|
||||
trigger: TriggerValidators,
|
||||
|
||||
@@ -1,62 +1,77 @@
|
||||
import { AutomationCondition, LogicCondition, NumericCondition, StateCondition, TemplateCondition, TimeCondition, TriggerCondition, ZoneCondition } from "types/automations/conditions";
|
||||
import { AutomationData } from 'types/automations';
|
||||
import {
|
||||
AutomationCondition,
|
||||
LogicCondition,
|
||||
NumericCondition,
|
||||
StateCondition,
|
||||
TemplateCondition,
|
||||
TimeCondition,
|
||||
TriggerCondition,
|
||||
ZoneCondition,
|
||||
} from "types/automations/conditions";
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
|
||||
export const getConditionDefaultValues = (condition: AutomationCondition['condition']): AutomationCondition => {
|
||||
if (
|
||||
(condition === 'or') ||
|
||||
(condition === 'and') ||
|
||||
(condition === 'not')
|
||||
) {
|
||||
return {
|
||||
condition,
|
||||
conditions: []
|
||||
} as LogicCondition
|
||||
} else if (condition === 'template') {
|
||||
return {
|
||||
condition,
|
||||
value_template: ""
|
||||
} as TemplateCondition
|
||||
} else if (condition === 'numeric_state') {
|
||||
return {
|
||||
condition,
|
||||
entity_id: []
|
||||
} as NumericCondition
|
||||
} else if (condition === 'state') {
|
||||
return {
|
||||
condition,
|
||||
entity_id: [],
|
||||
state: [],
|
||||
} as StateCondition
|
||||
} else if (condition === 'time') {
|
||||
return {
|
||||
condition,
|
||||
} as TimeCondition
|
||||
} else if (condition === 'trigger') {
|
||||
return {
|
||||
condition,
|
||||
id: ""
|
||||
} as TriggerCondition
|
||||
} else if (condition === 'zone') {
|
||||
return {
|
||||
condition,
|
||||
zone: "",
|
||||
entity_id: [],
|
||||
state: [],
|
||||
} as ZoneCondition
|
||||
}
|
||||
throw new Error("NOT IMPLEMENTED!")
|
||||
}
|
||||
export const getConditionDefaultValues = (
|
||||
condition: AutomationCondition["condition"]
|
||||
): AutomationCondition => {
|
||||
if (condition === "or" || condition === "and" || condition === "not") {
|
||||
return {
|
||||
condition,
|
||||
conditions: [],
|
||||
} as LogicCondition;
|
||||
} else if (condition === "template") {
|
||||
return {
|
||||
condition,
|
||||
value_template: "",
|
||||
} as TemplateCondition;
|
||||
} else if (condition === "numeric_state") {
|
||||
return {
|
||||
condition,
|
||||
entity_id: [],
|
||||
} as NumericCondition;
|
||||
} else if (condition === "state") {
|
||||
return {
|
||||
condition,
|
||||
entity_id: [],
|
||||
state: [],
|
||||
} as StateCondition;
|
||||
} else if (condition === "time") {
|
||||
return {
|
||||
condition,
|
||||
} as TimeCondition;
|
||||
} else if (condition === "trigger") {
|
||||
return {
|
||||
condition,
|
||||
id: "",
|
||||
} as TriggerCondition;
|
||||
} else if (condition === "zone") {
|
||||
return {
|
||||
condition,
|
||||
zone: "",
|
||||
entity_id: [],
|
||||
state: [],
|
||||
} as ZoneCondition;
|
||||
} else if (condition === "device") {
|
||||
return {
|
||||
condition,
|
||||
device_id: "",
|
||||
domain: "",
|
||||
type: "",
|
||||
};
|
||||
}
|
||||
return {
|
||||
condition,
|
||||
} as any;
|
||||
};
|
||||
|
||||
export const defaultAutomation = (id: string): AutomationData => ({
|
||||
metadata: {
|
||||
id,
|
||||
alias: "New Automation",
|
||||
description: "",
|
||||
mode: "single",
|
||||
},
|
||||
tags: {},
|
||||
trigger: [],
|
||||
sequence: [],
|
||||
condition: [],
|
||||
})
|
||||
metadata: {
|
||||
id,
|
||||
alias: "New Automation",
|
||||
description: "",
|
||||
mode: "single",
|
||||
},
|
||||
tags: {},
|
||||
trigger: [],
|
||||
sequence: [],
|
||||
condition: [],
|
||||
});
|
||||
@@ -1,5 +1,8 @@
|
||||
import { AutomationNode, AutomationNodeTypes } from "types/automations";
|
||||
import { AutomationTime } from "types/automations/common";
|
||||
import {
|
||||
AutomationDeviceState,
|
||||
AutomationTime,
|
||||
} from "types/automations/common";
|
||||
|
||||
export interface Namer {
|
||||
getEntityName(entity_id: string | string[], maxEntities?: number): string;
|
||||
@@ -106,8 +109,10 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
|
||||
return `${namer.getEntityName(node.entity_id)} is '${node.state}'`;
|
||||
case "template":
|
||||
return node.value_template;
|
||||
case "device":
|
||||
return getDescriptionForDeviceType(node, namer, true);
|
||||
default:
|
||||
return "n/a";
|
||||
return JSON.stringify(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,19 +136,7 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
|
||||
return `Trigger ${node.event}`;
|
||||
}
|
||||
if ("device_id" in node) {
|
||||
let out = "";
|
||||
if (node.type) {
|
||||
out += `${prettyName(node.type ?? "")} `;
|
||||
}
|
||||
if (node.subtype) {
|
||||
out += node.subtype + " ";
|
||||
}
|
||||
if (node.entity_id) {
|
||||
out += namer.getEntityName(node.entity_id);
|
||||
} else if (node.device_id) {
|
||||
out += namer.getDeviceName(node.device_id);
|
||||
}
|
||||
return out;
|
||||
return getDescriptionForDeviceType(node, namer);
|
||||
}
|
||||
if ("choose" in node) {
|
||||
return "Choose";
|
||||
@@ -151,6 +144,32 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
|
||||
return "n/a";
|
||||
};
|
||||
|
||||
const getDescriptionForDeviceType = (
|
||||
node: AutomationDeviceState,
|
||||
namer: Namer,
|
||||
actionToEnd: boolean = false
|
||||
) => {
|
||||
let out = "";
|
||||
let actionPort = "";
|
||||
if (node.type) {
|
||||
actionPort += `${prettyName(node.type ?? "")} `;
|
||||
}
|
||||
if (node.subtype) {
|
||||
actionPort += node.subtype;
|
||||
}
|
||||
actionPort = actionPort.trim();
|
||||
if (node.entity_id) {
|
||||
out += namer.getEntityName(node.entity_id);
|
||||
} else if (node.device_id) {
|
||||
out += namer.getDeviceName(node.device_id);
|
||||
}
|
||||
out = out.trim();
|
||||
if (actionToEnd) {
|
||||
return out + " " + actionPort;
|
||||
}
|
||||
return actionPort + " " + out;
|
||||
};
|
||||
|
||||
export const prettyName = (n: string) => {
|
||||
const clean = n.replaceAll(/[.\-_]/g, " ");
|
||||
return clean
|
||||
|
||||
Reference in new issue
Block a user