removed $smType for conditions and trigger

This commit is contained in:
ari committed 2022-05-09 21:41:49 -04:00
1 parent ebd99c80a3
commit 47bf511bbf
41 files changed
+410 -723

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
import { AutomationNode, AutomationNodeSubtype, AutomationNodeTypes } from "types/automations";
export const getNodeType = (node: AutomationNode): AutomationNodeTypes => {
if ('condition' in node) {
return 'condition'
} else if ('platform' in node) {
return 'trigger'
} else {
return 'action'
}
}
export const getSubTypeList = <T extends AutomationNodeTypes>(nodeType: T): AutomationNodeSubtype<T>[] => {
switch (nodeType) {
case 'action':
return [
"service",
"repeat",
"wait",
"device",
"choose",
] as any
case "condition":
return [
'or',
'and',
'not',
'numeric_state',
'state',
'template',
'time',
'trigger',
'zone',
] as any
case 'trigger':
return [
'event',
'homeassistant',
'mqtt',
'numeric_state',
'state',
'tag',
'template',
'time',
'time_pattern',
'webhook',
'zone',
'device',
] as any
default:
return [];
}
}
+15 -8
View File
@@ -2,41 +2,48 @@ import { AutomationCondition, LogicCondition, NumericCondition, StateCondition,
import { AutomationData } from 'types/automations';
export const getConditionDefaultValues = (condition: AutomationCondition['condition']): AutomationCondition['condition_data'] => {
export const getConditionDefaultValues = (condition: AutomationCondition['condition']): AutomationCondition => {
if (
(condition === 'or') ||
(condition === 'and') ||
(condition === 'not')
) {
return {
condition,
conditions: []
} as LogicCondition['condition_data']
} as LogicCondition
} else if (condition === 'template') {
return {
condition,
value_template: ""
} as TemplateCondition['condition_data']
} as TemplateCondition
} else if (condition === 'numeric_state') {
return {
condition,
entity_id: []
} as NumericCondition['condition_data']
} as NumericCondition
} else if (condition === 'state') {
return {
condition,
entity_id: [],
state: [],
} as StateCondition['condition_data']
} as StateCondition
} else if (condition === 'time') {
return {
} as TimeCondition['condition_data']
condition,
} as TimeCondition
} else if (condition === 'trigger') {
return {
condition,
id: ""
} as TriggerCondition['condition_data']
} as TriggerCondition
} else if (condition === 'zone') {
return {
condition,
zone: "",
entity_id: [],
state: [],
} as ZoneCondition['condition_data']
} as ZoneCondition
}
throw new Error("NOT IMPLEMENTED!")
}
+32 -65
View File
@@ -1,11 +1,11 @@
import {convertTimeToString, getDescriptionFromAutomationNode} from "./formatting";
import { convertTimeToString, getDescriptionFromAutomationNode } from "./formatting";
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')
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', () => {
@@ -116,100 +116,67 @@ test('get choose action description', () => {
test('get and/or/not condition description', () => {
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'and',
condition_data: {
conditions: [],
}
conditions: [],
})).toEqual("Logic")
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'or',
condition_data: {
conditions: [],
}
conditions: [],
})).toEqual("Logic")
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'not',
condition_data: {
conditions: [],
}
conditions: [],
})).toEqual("Logic")
})
test('get Numeric State condition description', () => {
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'numeric_state',
condition_data: {
entity_id: 'sensor.temperature',
}
entity_id: 'sensor.temperature',
})).toEqual("sensor.temperature?>?<")
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'numeric_state',
condition_data: {
entity_id: ""
}
entity_id: ""
})).toEqual("Numeric State Condition")
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: "numeric_state",
condition_data: {
entity_id: 'sensor.temperature',
above: '17',
}
entity_id: 'sensor.temperature',
above: '17',
})).toEqual("sensor.temperature > 17")
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'numeric_state',
condition_data: {
entity_id: 'sensor.temperature',
below: '5',
}
entity_id: 'sensor.temperature',
below: '5',
})).toEqual("sensor.temperature < 5")
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'numeric_state',
condition_data: {
entity_id: 'sensor.temperature',
above: '17',
below: '20',
}
})).toEqual("17 < sensor.temperature < 20")
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'numeric_state',
condition_data: {
entity_id: 'sensor.temperature',
above: '17',
below: '20',
value_template: "{{ float(state.state) + 2 }}"
}
above: '17',
below: '20',
})).toEqual("17 < sensor.temperature < 20")
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'numeric_state',
condition_data: {
entity_id: [
"sensor.kitchen_temperature",
"sensor.living_room_temperature"
],
above: '17',
below: '20',
value_template: "{{ float(state.state) + 2 }}"
}
entity_id: 'sensor.temperature',
above: '17',
below: '20',
value_template: "{{ float(state.state) + 2 }}"
})).toEqual("17 < sensor.temperature < 20")
expect(getDescriptionFromAutomationNode({
condition: 'numeric_state',
entity_id: [
"sensor.kitchen_temperature",
"sensor.living_room_temperature"
],
above: '17',
below: '20',
value_template: "{{ float(state.state) + 2 }}"
})).toEqual("17 < ... < 20")
})
test('get State condition description', () => {
expect(getDescriptionFromAutomationNode({
$smType: 'condition',
condition: 'state',
condition_data: {
entity_id: 'switch.kitchen_light',
state: "on"
}
entity_id: 'switch.kitchen_light',
state: "on"
})).toEqual("switch.kitchen_light is 'on'")
})
+47 -123
View File
@@ -3,74 +3,73 @@ import { AutomationTime } from "types/automations/common";
export const convertTimeToString = ({
hours=0,
minutes=0,
seconds=0,
milliseconds=0,
}: AutomationTime) => [hours,minutes,seconds,milliseconds].map(n => String(n).padStart(2, '0')).join(':')
hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0,
}: AutomationTime) => [hours, minutes, seconds, milliseconds].map(n => String(n).padStart(2, '0')).join(':')
export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(node: AutomationNode<N>): string => {
if (node.$smType) {
if ('$smType' in node) {
switch (node['$smType']) {
case "action":
if (node.action_data.alias) {
return node.action_data.alias;
}
switch(node.action) {
switch (node.action) {
case 'service':
return node.action_data.service
case 'repeat':
return 'Repeat ' + node.action_data.repeat.count;
case 'wait':
let out= 'Wait on ' + node.action_data.wait_template;
let out = 'Wait on ' + node.action_data.wait_template;
if (node.action_data.timeout) {
out += (' for ' + convertTimeToString(node.action_data.timeout))
}
return out;
case 'event':
return `Trigger ${node.action_data.event}`
return `Trigger ${node.action_data.event}`
case 'device':
return `${node.action_data.type} on ${node.action_data.device_id}`
case 'choose':
return "Choose"
default:
return 'n/a'
return "Choose"
}
case "condition":
if (node.condition_data.alias) {
return node.condition_data.alias;
}
if(
(node.condition === 'and') ||
(node.condition === 'or') ||
(node.condition === 'not')
) {
return "Logic"
} else {
switch(node.condition) {
case "numeric_state":
if(node.condition_data.entity_id) {
const center = prettyEntityId(node.condition_data.entity_id);
if(node.condition_data.above) {
if(node.condition_data.below) {
return `${node.condition_data.above} < ${center} < ${node.condition_data.below}`
}
return `${center} > ${node.condition_data.above}`
}
if(node.condition_data.below) {
return `${center} < ${node.condition_data.below}`
}
return `${center}?>?<`
}
return 'n/a'
} else if ('condition' in node) {
if (node.alias) {
return node.alias;
}
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 = prettyEntityId(node.entity_id);
if (node.above) {
if (node.below) {
return `${node.above} < ${center} < ${node.below}`
}
return "Numeric State Condition"
case "state":
return `${node.condition_data.entity_id} is '${node.condition_data.state}'`
case 'template':
return node.condition_data.value_template
default:
return 'n/a'
return `${center} > ${node.above}`
}
if (node.below) {
return `${center} < ${node.below}`
}
return `${center}?>?<`
}
}
return "Numeric State Condition"
case "state":
return `${node.entity_id} is '${node.state}'`
case 'template':
return node.value_template
default:
return 'n/a'
}
}
} else {
if (node.alias) {
@@ -78,89 +77,14 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
}
return node.platform
}
return 'n/a'
}
export const prettyEntityId = (entityId: string| string[]) => {
export const prettyEntityId = (entityId: string | string[]) => {
if (typeof entityId === 'string') {
return entityId
} else if(entityId.length === 1) {
} else if (entityId.length === 1) {
return entityId[0]
} else {
} else {
return "..."
}
}
// export const determineNodeTypes = (action: AutomationNode<any>) : [AutomationNodeType, NodeSubType] => {
// if ('condition' in action) {
// switch (action.condition) {
// case 'and':
// return ['condition', 'Logic']
// case 'or':
// return ['condition', 'Logic']
// case 'not':
// return ['condition', 'Logic']
// case 'numeric_state':
// return ['condition', 'Numeric State']
// case 'state':
// return ['condition', 'State']
// case 'template':
// return ['condition', 'Template']
// case 'trigger':
// return ['condition', 'Trigger']
// case 'zone':
// return ['condition', 'Zone']
// default:
// return ['condition', 'Generic']
// }
// }
// if ('platform' in action) {
// switch (action.platform) {
// case 'event':
// return ['trigger', 'Event']
// case 'homeassistant':
// return ['trigger', 'Home Assistant']
// case 'mqtt':
// return ['trigger', 'MQTT']
// case 'numeric_state':
// return ['trigger', 'Numeric State']
// case 'state':
// return ['trigger', 'State']
// case 'tag':
// return ['trigger', 'Tag']
// case 'template':
// return ['trigger', 'Template']
// case 'time':
// return ['trigger', 'Time']
// case 'time_pattern':
// return ['trigger', 'Time Pattern']
// case 'webhook':
// return ['trigger', 'Webhook']
// case 'zone':
// return ['trigger', 'Zone']
// default:
// return ['trigger', 'Generic']
// }
// }
// if ('service' in action) {
// return ["action", "Service"]
// }
// if ('repeat' in action) {
// return ["action", "Repeat"]
// }
// if ('wait_template' in action) {
// return ["action", "Wait"]
// }
// if ('event' in action) {
// return ["action", "Event"]
// }
// if (('type' in action) && ('device_id' in action)) {
// return ["action", "Device"]
// }
// if ('choose' in action) {
// return ["action", "Choose"]
// }
// throw Error(`Could not determine node type ${JSON.stringify(action)}`)
// }
+11 -22
View File
@@ -14,39 +14,28 @@ export const createMockAuto = (tags: Record<string, string> = {}): AutomationDat
tags,
trigger: [
{
"$smType": undefined,
"platform": "numeric_state",
"entity_id": "test",
},
{
"$smType": undefined,
"platform": "homeassistant",
"event": "start",
}
],
sequence: [
{
$smType: "condition",
condition: 'and',
condition_data: {
conditions: [
{
$smType: 'condition',
condition: 'numeric_state',
condition_data: {
entity_id: 'sensor.temperature_kitchen',
below: '15',
}
},
{
$smType: "condition",
condition: 'template',
condition_data: {
value_template: 'states(switch.kitchen_light) == "on"'
}
}
]
}
conditions: [
{
condition: 'numeric_state',
entity_id: 'sensor.temperature_kitchen',
below: '15',
},
{
condition: 'template',
value_template: 'states(switch.kitchen_light) == "on"'
}
]
},
{
$smType: "action",