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

+32 -20
View File
@@ -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'")
})
+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(' ')