device action builder
This commit is contained in:
1 parent
6be6005531
commit
029df7932e
16 files changed
+725
-434
No files matched your search
+125
-119
@@ -1,135 +1,141 @@
|
||||
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;
|
||||
getServiceName(service: string): string;
|
||||
getEntityName(entity_id: string | string[], maxEntities?: number): string;
|
||||
getDeviceName(device_id: string): string;
|
||||
getServiceName(service: string): string;
|
||||
}
|
||||
|
||||
export const convertTimeToString = (t: AutomationTime) => {
|
||||
if (typeof t === 'string') {
|
||||
return t
|
||||
} else {
|
||||
const {
|
||||
hours = 0,
|
||||
minutes = 0,
|
||||
seconds = 0,
|
||||
milliseconds = 0,
|
||||
} = t
|
||||
return [hours, minutes, seconds, milliseconds].map(n => String(n).padStart(2, '0')).join(':')
|
||||
}
|
||||
}
|
||||
if (typeof t === "string") {
|
||||
return t;
|
||||
} else {
|
||||
const { hours = 0, minutes = 0, seconds = 0, milliseconds = 0 } = t;
|
||||
return [hours, minutes, seconds, milliseconds]
|
||||
.map((n) => String(n).padStart(2, "0"))
|
||||
.join(":");
|
||||
}
|
||||
};
|
||||
export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
|
||||
node: AutomationNode<N>,
|
||||
namer: Namer,
|
||||
node: AutomationNode<N>,
|
||||
namer: Namer
|
||||
): string => {
|
||||
if (('alias' in node) && node.alias) {
|
||||
return node.alias;
|
||||
}
|
||||
if ("alias" in node && node.alias) {
|
||||
return node.alias;
|
||||
}
|
||||
|
||||
if ('platform' in node) {
|
||||
if (node.id) {
|
||||
return node.id ?? '';
|
||||
}
|
||||
if (node.platform === 'time') {
|
||||
return node.at
|
||||
}
|
||||
if (node.platform === 'homeassistant') {
|
||||
if (node.event === 'start') {
|
||||
return 'HA is starting'
|
||||
} else {
|
||||
return 'HA is shutting down'
|
||||
if ("platform" in node) {
|
||||
if (node.id) {
|
||||
return node.id ?? "";
|
||||
}
|
||||
if (node.platform === "time") {
|
||||
return node.at;
|
||||
}
|
||||
if (node.platform === "homeassistant") {
|
||||
if (node.event === "start") {
|
||||
return "HA is starting";
|
||||
} else {
|
||||
return "HA is shutting down";
|
||||
}
|
||||
}
|
||||
if (node.platform === "tag") {
|
||||
return `Tag = ${node.tag_id}`;
|
||||
}
|
||||
if (node.platform === "state") {
|
||||
let out = "";
|
||||
if (typeof node.entity_id === "string") {
|
||||
out = namer.getEntityName(node.entity_id);
|
||||
} else {
|
||||
out = namer.getEntityName(node.entity_id[0]) + " and more";
|
||||
}
|
||||
if (node.from) {
|
||||
out += ` from ${node.from}`;
|
||||
}
|
||||
if (node.to) {
|
||||
out += ` to ${node.to}`;
|
||||
}
|
||||
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) {
|
||||
if (
|
||||
node.condition === "and" ||
|
||||
node.condition === "or" ||
|
||||
node.condition === "not"
|
||||
) {
|
||||
return "Logic";
|
||||
} else {
|
||||
switch (node.condition) {
|
||||
case "numeric_state":
|
||||
if (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}`;
|
||||
}
|
||||
return `${center} > ${node.above}`;
|
||||
}
|
||||
}
|
||||
if (node.platform === 'tag') {
|
||||
return `Tag = ${node.tag_id}`
|
||||
}
|
||||
if (node.platform === 'state') {
|
||||
let out = "";
|
||||
if (typeof node.entity_id === 'string') {
|
||||
out = namer.getEntityName(node.entity_id)
|
||||
} else {
|
||||
out = namer.getEntityName(node.entity_id[0]) + " and more"
|
||||
if (node.below) {
|
||||
return `${center} < ${node.below}`;
|
||||
}
|
||||
if (node.from) {
|
||||
out += ` from ${node.from}`
|
||||
}
|
||||
if (node.to) {
|
||||
out += ` to ${node.to}`
|
||||
}
|
||||
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
|
||||
return `${center}?>?<`;
|
||||
}
|
||||
return "Numeric State Condition";
|
||||
case "state":
|
||||
return `${namer.getEntityName(node.entity_id)} is '${node.state}'`;
|
||||
case "template":
|
||||
return node.value_template;
|
||||
default:
|
||||
return "n/a";
|
||||
}
|
||||
}
|
||||
if ('condition' in node) {
|
||||
if (
|
||||
(node.condition === 'and') ||
|
||||
(node.condition === 'or') ||
|
||||
(node.condition === 'not')
|
||||
) {
|
||||
return "Logic"
|
||||
} else {
|
||||
switch (node.condition) {
|
||||
case "numeric_state":
|
||||
if (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}`
|
||||
}
|
||||
return `${center} > ${node.above}`
|
||||
}
|
||||
if (node.below) {
|
||||
return `${center} < ${node.below}`
|
||||
}
|
||||
return `${center}?>?<`
|
||||
}
|
||||
return "Numeric State Condition"
|
||||
case "state":
|
||||
return `${namer.getEntityName(node.entity_id)} is '${node.state}'`
|
||||
case 'template':
|
||||
return node.value_template
|
||||
default:
|
||||
return 'n/a'
|
||||
}
|
||||
}
|
||||
}
|
||||
if ("service" in node) {
|
||||
return namer.getServiceName(node.service);
|
||||
}
|
||||
if ("repeat" in node) {
|
||||
return "Repeat " + node.repeat.count;
|
||||
}
|
||||
if ("delay" in node) {
|
||||
return `Wait for ${convertTimeToString(node.delay)}`;
|
||||
}
|
||||
if ("wait_template" in node) {
|
||||
let out = "Wait on " + node.wait_template;
|
||||
if (node.timeout) {
|
||||
out += " for " + convertTimeToString(node.timeout);
|
||||
}
|
||||
if ('service' in node) {
|
||||
return namer.getServiceName(node.service)
|
||||
}
|
||||
if ('repeat' in node) {
|
||||
return 'Repeat ' + node.repeat.count
|
||||
}
|
||||
if ('delay' in node) {
|
||||
return `Wait for ${convertTimeToString(node.delay)}`
|
||||
}
|
||||
if ('wait_template' in node) {
|
||||
let out = 'Wait on ' + node.wait_template;
|
||||
if (node.timeout) {
|
||||
out += (' for ' + convertTimeToString(node.timeout))
|
||||
}
|
||||
return out;
|
||||
}
|
||||
if ('event' in node) {
|
||||
return `Trigger ${node.event}`
|
||||
}
|
||||
if ('device_id' in node) {
|
||||
return `${node.type ?? ""} on ${node.device_id ? namer.getDeviceName(node.device_id) : ''}`
|
||||
}
|
||||
if ('choose' in node) {
|
||||
return "Choose"
|
||||
}
|
||||
return 'n/a'
|
||||
}
|
||||
return out;
|
||||
}
|
||||
if ("event" in node) {
|
||||
return `Trigger ${node.event}`;
|
||||
}
|
||||
if ("device_id" in node) {
|
||||
return `${node.type ?? ""} on ${
|
||||
node.device_id ? namer.getDeviceName(node.device_id) : ""
|
||||
}`;
|
||||
}
|
||||
if ("choose" in node) {
|
||||
return "Choose";
|
||||
}
|
||||
return "n/a";
|
||||
};
|
||||
|
||||
export const prettyName = (n: string) => {
|
||||
const clean = n.replaceAll(/[.-_]/g, ' ');
|
||||
return clean.split(' ').map(x => x.slice(0, 1).toUpperCase() + x.slice(1)).join(' ')
|
||||
}
|
||||
const clean = n.replaceAll(/[.\-_]/g, " ");
|
||||
return clean
|
||||
.split(" ")
|
||||
.map((x) => x.slice(0, 1).toUpperCase() + x.slice(1))
|
||||
.join(" ");
|
||||
};
|
||||
Reference in new issue
Block a user