device action builder

This commit is contained in:
ari committed 2022-06-17 13:37:25 -04:00
1 parent 6be6005531
commit 029df7932e
16 files changed
+725 -434

No files matched your search

+237 -136
View File
@@ -1,170 +1,271 @@
import { convertTimeToString, getDescriptionFromAutomationNode, Namer, prettyName } from "./formatting";
import {
convertTimeToString,
getDescriptionFromAutomationNode,
Namer,
prettyName,
} from "./formatting";
const dummyNamer: Namer = {
getDeviceName(device_id) {
return device_id
},
getServiceName(service) {
return service
},
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')
expect(convertTimeToString({ hours: 1, minutes: 5, seconds: 24 })).toEqual('01:05:24:00')
expect(convertTimeToString({ hours: 1, milliseconds: 100 })).toEqual('01:00:00:100')
})
getDeviceName(device_id) {
return device_id;
},
getServiceName(service) {
return service;
},
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");
expect(convertTimeToString({ hours: 1, minutes: 5, seconds: 24 })).toEqual(
"01:05:24:00"
);
expect(convertTimeToString({ hours: 1, milliseconds: 100 })).toEqual(
"01:00:00:100"
);
});
test('using alias as description', () => {
expect(getDescriptionFromAutomationNode({
test("using alias as description", () => {
expect(
getDescriptionFromAutomationNode(
{
alias: "Start Music In Kitchen",
service: 'media_player.play_media',
service: "media_player.play_media",
target: {
entity_id: "media_player.kitchen_dot"
entity_id: "media_player.kitchen_dot",
},
data: {
media_content_id: "Good Morning",
media_content_type: "SPOTIFY",
}
}, dummyNamer)).toEqual("Start Music In Kitchen")
})
media_content_id: "Good Morning",
media_content_type: "SPOTIFY",
},
},
dummyNamer
)
).toEqual("Start Music In Kitchen");
});
test('get service action description', () => {
expect(getDescriptionFromAutomationNode({
service: 'media_player.play_media',
test("get service action description", () => {
expect(
getDescriptionFromAutomationNode(
{
service: "media_player.play_media",
target: {
entity_id: "media_player.kitchen_dot"
entity_id: "media_player.kitchen_dot",
},
data: {
media_content_id: "Good Morning",
media_content_type: "SPOTIFY",
}
}, dummyNamer)).toEqual("media_player.play_media")
})
media_content_id: "Good Morning",
media_content_type: "SPOTIFY",
},
},
dummyNamer
)
).toEqual("media_player.play_media");
});
test('get repeat action description', () => {
expect(getDescriptionFromAutomationNode({
test("get repeat action description", () => {
expect(
getDescriptionFromAutomationNode(
{
repeat: {
count: 10,
sequence: []
}
}, dummyNamer)).toEqual("Repeat 10")
})
count: 10,
sequence: [],
},
},
dummyNamer
)
).toEqual("Repeat 10");
});
test('get wait action description', () => {
expect(getDescriptionFromAutomationNode({
wait_template: "states(switch.kitchen_light) == 'on'"
}, dummyNamer)).toEqual("Wait on states(switch.kitchen_light) == 'on'")
expect(getDescriptionFromAutomationNode({
test("get wait action description", () => {
expect(
getDescriptionFromAutomationNode(
{
wait_template: "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,
}
}, dummyNamer)).toEqual("Wait on states(switch.kitchen_light) == 'on' for 00:01:00:00")
})
minutes: 1,
},
},
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',
test("get fire event action description", () => {
expect(
getDescriptionFromAutomationNode(
{
event: "test_event",
event_data: {},
}, dummyNamer)).toEqual("Trigger test_event")
})
},
dummyNamer
)
).toEqual("Trigger test_event");
});
test('get device action description', () => {
expect(getDescriptionFromAutomationNode({
test("get device action description", () => {
expect(
getDescriptionFromAutomationNode(
{
device_id: "12310das01231",
domain: "zwave_js",
type: "set_value"
}, dummyNamer)).toEqual("set_value on 12310das01231")
})
type: "set_value",
},
dummyNamer
)
).toEqual("set_value on 12310das01231");
});
test('get choose action description', () => {
expect(getDescriptionFromAutomationNode({
test("get choose action description", () => {
expect(
getDescriptionFromAutomationNode(
{
choose: [],
default: [],
}, dummyNamer)).toEqual("Choose")
})
},
dummyNamer
)
).toEqual("Choose");
});
test('get and/or/not condition description', () => {
expect(getDescriptionFromAutomationNode({
condition: 'and',
test("get and/or/not condition description", () => {
expect(
getDescriptionFromAutomationNode(
{
condition: "and",
conditions: [],
}, dummyNamer)).toEqual("Logic")
expect(getDescriptionFromAutomationNode({
condition: 'or',
},
dummyNamer
)
).toEqual("Logic");
expect(
getDescriptionFromAutomationNode(
{
condition: "or",
conditions: [],
}, dummyNamer)).toEqual("Logic")
expect(getDescriptionFromAutomationNode({
condition: 'not',
},
dummyNamer
)
).toEqual("Logic");
expect(
getDescriptionFromAutomationNode(
{
condition: "not",
conditions: [],
}, dummyNamer)).toEqual("Logic")
})
},
dummyNamer
)
).toEqual("Logic");
});
test('get Numeric State condition description', () => {
expect(getDescriptionFromAutomationNode({
condition: 'numeric_state',
entity_id: 'sensor.temperature',
}, dummyNamer)).toEqual("sensor.temperature?>?<")
expect(getDescriptionFromAutomationNode({
condition: 'numeric_state',
entity_id: ""
}, dummyNamer)).toEqual("Numeric State Condition")
expect(getDescriptionFromAutomationNode({
test("get Numeric State condition description", () => {
expect(
getDescriptionFromAutomationNode(
{
condition: "numeric_state",
entity_id: "sensor.temperature",
},
dummyNamer
)
).toEqual("sensor.temperature?>?<");
expect(
getDescriptionFromAutomationNode(
{
condition: "numeric_state",
entity_id: "",
},
dummyNamer
)
).toEqual("Numeric State Condition");
expect(
getDescriptionFromAutomationNode(
{
condition: "numeric_state",
entity_id: "sensor.temperature",
above: "17",
},
dummyNamer
)
).toEqual("sensor.temperature > 17");
expect(
getDescriptionFromAutomationNode(
{
condition: "numeric_state",
entity_id: "sensor.temperature",
below: "5",
},
dummyNamer
)
).toEqual("sensor.temperature < 5");
expect(
getDescriptionFromAutomationNode(
{
condition: "numeric_state",
entity_id: "sensor.temperature",
above: "17",
below: "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 }}",
},
dummyNamer
)
).toEqual("17 < sensor.temperature < 20");
expect(
getDescriptionFromAutomationNode(
{
condition: "numeric_state",
entity_id: 'sensor.temperature',
above: '17',
}, dummyNamer)).toEqual("sensor.temperature > 17")
expect(getDescriptionFromAutomationNode({
condition: 'numeric_state',
entity_id: 'sensor.temperature',
below: '5',
}, dummyNamer)).toEqual("sensor.temperature < 5")
expect(getDescriptionFromAutomationNode({
condition: 'numeric_state',
entity_id: 'sensor.temperature',
above: '17',
below: '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 }}"
}, dummyNamer)).toEqual("17 < sensor.temperature < 20")
expect(getDescriptionFromAutomationNode({
condition: 'numeric_state',
entity_id: [
"sensor.kitchen_temperature",
"sensor.living_room_temperature"
"sensor.kitchen_temperature",
"sensor.living_room_temperature",
],
above: '17',
below: '20',
value_template: "{{ float(state.state) + 2 }}"
}, dummyNamer)).toEqual("17 < ... < 20")
})
above: "17",
below: "20",
value_template: "{{ float(state.state) + 2 }}",
},
dummyNamer
)
).toEqual("17 < ... < 20");
});
test('get State condition description', () => {
expect(getDescriptionFromAutomationNode({
condition: 'state',
entity_id: 'switch.kitchen_light',
state: "on"
}, dummyNamer)).toEqual("switch.kitchen_light is 'on'")
})
test("get State condition description", () => {
expect(
getDescriptionFromAutomationNode(
{
condition: "state",
entity_id: "switch.kitchen_light",
state: "on",
},
dummyNamer
)
).toEqual("switch.kitchen_light is 'on'");
});
test('pretty naming', () => {
expect(prettyName('sensor')).toEqual("Sensor")
expect(prettyName('sensor.phone')).toEqual("Sensor Phone")
expect(prettyName('device_tracker')).toEqual("Device Tracker")
})
test("pretty naming", () => {
expect(prettyName("sensor")).toEqual("Sensor");
expect(prettyName("sensor.phone")).toEqual("Sensor Phone");
expect(prettyName("device_tracker")).toEqual("Device Tracker");
expect(prettyName("Baby Name")).toEqual("Baby Name");
});
+125 -119
View File
@@ -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(" ");
};