This commit is contained in:
ari committed 2022-06-06 11:27:25 -04:00
1 parent c934a4afcb
commit ac1ee86cc7
8 files changed
+98 -50

No files matched your search

+19 -16
View File
@@ -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 = <N extends AutomationNodeTypes>(node: AutomationNode<N>): string => {
export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
node: AutomationNode<N>,
namer: Namer,
): string => {
if (('alias' in node) && node.alias) {
return node.alias;
}
@@ -40,9 +48,9 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
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 = <N extends AutomationNodeTypes>(
}
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 = <N extends AutomationNodeTypes>(
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 = <N extends AutomationNodeTypes>(
}
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 = <N extends AutomationNodeTypes>(
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 = <N extends AutomationNodeTypes>(
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(' ')