updated data type
This commit is contained in:
1 parent
7127c3c0b9
commit
9b50f73b62
26 files changed
+590
-927
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import { AutomationSequenceNode } from ".";
|
||||
import { AutomationTime } from "./common";
|
||||
import { AutomationCondition } from "./conditions";
|
||||
|
||||
interface AutomationActionNodeBase<Name extends String,Data extends Object> {
|
||||
$smType: 'action';
|
||||
action: Name;
|
||||
action_data: Data & {
|
||||
alias?: string;
|
||||
variables?: Record<string, any>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ServiceAction extends AutomationActionNodeBase<"service", {
|
||||
service: string;
|
||||
target: any;
|
||||
data: any;
|
||||
}>{}
|
||||
export interface RepeatAction extends AutomationActionNodeBase<"repeat", {
|
||||
repeat: {
|
||||
count: number;
|
||||
sequence: AutomationAction[];
|
||||
}
|
||||
}>{}
|
||||
export interface WaitAction extends AutomationActionNodeBase<"wait", {
|
||||
wait_template: string;
|
||||
timeout?: AutomationTime;
|
||||
continue_on_timeout?: boolean;
|
||||
}>{}
|
||||
export interface FireEventAction extends AutomationActionNodeBase<"event", {
|
||||
event: string;
|
||||
event_data: any;
|
||||
}>{}
|
||||
export interface DeviceAction extends AutomationActionNodeBase<"device", {
|
||||
type: string;
|
||||
device_id: string;
|
||||
entity_id?: string;
|
||||
domain: string;
|
||||
}>{}
|
||||
export interface ChooseAction extends AutomationActionNodeBase<"choose", {
|
||||
choose: Array<{
|
||||
conditions: AutomationCondition[];
|
||||
sequence: AutomationSequenceNode[];
|
||||
}>;
|
||||
default: AutomationAction[];
|
||||
}>{}
|
||||
export type AutomationAction =
|
||||
| ServiceAction
|
||||
| RepeatAction
|
||||
| WaitAction
|
||||
| FireEventAction
|
||||
| DeviceAction
|
||||
| ChooseAction
|
||||
;
|
||||
export const ActionTypes = [
|
||||
'Service',
|
||||
'Repeat',
|
||||
'Wait',
|
||||
'Event',
|
||||
'Device',
|
||||
'Choose'
|
||||
] as const;
|
||||
export type ActionType = typeof ActionTypes[number];
|
||||
@@ -0,0 +1,6 @@
|
||||
export interface AutomationTime {
|
||||
hours?: number;
|
||||
minutes?: number;
|
||||
seconds?: number;
|
||||
milliseconds?: number;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { AutomationTime } from "./common";
|
||||
|
||||
interface AutomationConditionNodeBase<Name extends String,Data extends Object> {
|
||||
$smType: 'condition';
|
||||
condition: Name;
|
||||
condition_data: Data & {
|
||||
alias?: string;
|
||||
};
|
||||
}
|
||||
export interface LogicCondition extends AutomationConditionNodeBase<'and' | 'or' | 'not',{
|
||||
conditions: AutomationCondition[];
|
||||
}> {}
|
||||
export interface NumericCondition extends AutomationConditionNodeBase<'numeric_state', {
|
||||
entity_id: string | string[];
|
||||
above?: string;
|
||||
below?: string;
|
||||
attribute?: string;
|
||||
value_template?: string;
|
||||
}> {}
|
||||
export interface StateCondition extends AutomationConditionNodeBase<'state',{
|
||||
entity_id: string | string[];
|
||||
state: string | string[];
|
||||
for?: AutomationTime;
|
||||
attribute?: string;
|
||||
}>{}
|
||||
export interface TemplateCondition extends AutomationConditionNodeBase<'template',{
|
||||
value_template: string;
|
||||
}> {}
|
||||
export interface TimeCondition extends AutomationConditionNodeBase<'time', {
|
||||
after?: AutomationTime;
|
||||
before?: AutomationTime;
|
||||
weekday?: Array<'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun'>;
|
||||
}> {}
|
||||
export interface TriggerCondition extends AutomationConditionNodeBase <'trigger', {
|
||||
id: string | string[];
|
||||
}>{}
|
||||
export interface ZoneCondition extends AutomationConditionNodeBase <'zone', {
|
||||
zone: string;
|
||||
entity_id: string | string[];
|
||||
state: string | string[];
|
||||
}>{}
|
||||
export type AutomationCondition =
|
||||
| LogicCondition
|
||||
| NumericCondition
|
||||
| StateCondition
|
||||
| TemplateCondition
|
||||
| TimeCondition
|
||||
| TriggerCondition
|
||||
| ZoneCondition
|
||||
;
|
||||
export const conditionTypes = [
|
||||
'Logic',
|
||||
'Numeric State',
|
||||
'State',
|
||||
'Template',
|
||||
'Time',
|
||||
'Trigger',
|
||||
'Zone',
|
||||
] as const;
|
||||
export type ConditionType = typeof conditionTypes[number];
|
||||
@@ -0,0 +1,27 @@
|
||||
import { AutomationAction } from "./actions";
|
||||
import { AutomationCondition } from "./conditions";
|
||||
import { AutomationTrigger } from "./triggers";
|
||||
|
||||
|
||||
export interface AutomationMetadata {
|
||||
id: string;
|
||||
alias: string;
|
||||
description: string;
|
||||
trigger_variables?: Record<string, string>;
|
||||
mode: "single" | "restart" | "queued" | "parallel";
|
||||
}
|
||||
export interface AutomationData {
|
||||
metadata: AutomationMetadata;
|
||||
trigger: AutomationTrigger[];
|
||||
condition: AutomationCondition[];
|
||||
action: AutomationSequenceNode[];
|
||||
}
|
||||
export type AutomationSequenceNode = AutomationAction | AutomationCondition;
|
||||
type AutomationNodeMapping = {
|
||||
"trigger": AutomationTrigger;
|
||||
"action": AutomationAction;
|
||||
"condition": AutomationCondition;
|
||||
"sequence": AutomationSequenceNode;
|
||||
}
|
||||
export type AutomationNodeTypes = keyof AutomationNodeMapping;
|
||||
export type AutomationNode<N extends AutomationNodeTypes = any> = AutomationNodeMapping[N];
|
||||
@@ -0,0 +1,106 @@
|
||||
import { AutomationTime } from "./common";
|
||||
|
||||
export interface AutomationTriggerEvent {
|
||||
platform: 'event';
|
||||
event_type: string;
|
||||
event_data?: any;
|
||||
context?: any;
|
||||
}
|
||||
export interface AutomationTriggerHA {
|
||||
platform: 'homeassistant';
|
||||
event: 'start' | 'shutdown';
|
||||
}
|
||||
export interface AutomationTriggerMQTT {
|
||||
platform: 'mqtt';
|
||||
topic: string;
|
||||
payload: string;
|
||||
value_template: string;
|
||||
}
|
||||
export interface AutomationTriggerNumericState {
|
||||
platform: 'numeric_state';
|
||||
entity_id: string | string[];
|
||||
attribute?: string;
|
||||
value_template?: string;
|
||||
above?: string;
|
||||
below?: string;
|
||||
for?: AutomationTime | string;
|
||||
}
|
||||
export interface AutomationTriggerState {
|
||||
platform: 'state';
|
||||
entity_id: string | string[];
|
||||
attribute?: string;
|
||||
from?: string;
|
||||
to?: string;
|
||||
for?: AutomationTime | string;
|
||||
}
|
||||
export interface AutomationTriggerTag {
|
||||
platform: 'tag';
|
||||
tag_id: string | string[];
|
||||
device_id: string | string[];
|
||||
}
|
||||
export interface AutomationTriggerTemplate {
|
||||
platform: 'template';
|
||||
value_template: string;
|
||||
for?: AutomationTime | string;
|
||||
}
|
||||
export interface AutomationTriggerTime {
|
||||
platform: 'time';
|
||||
at: string | string[];
|
||||
}
|
||||
export interface AutomationTriggerTimePattern {
|
||||
platform: 'time_pattern';
|
||||
hours?: string;
|
||||
minutes?: string;
|
||||
seconds?: string;
|
||||
}
|
||||
export interface AutomationTriggerWebhook {
|
||||
platform: 'webhook';
|
||||
webhook_id: string;
|
||||
}
|
||||
export interface AutomationTriggerZone {
|
||||
platform: 'zone';
|
||||
entity_id: string;
|
||||
zone: string;
|
||||
event: 'enter' | 'leave';
|
||||
}
|
||||
export interface AutomationTriggerDevice {
|
||||
platform: 'device';
|
||||
device_id: string;
|
||||
domain: string;
|
||||
type: string;
|
||||
subtype: string;
|
||||
}
|
||||
export type AutomationTriggerGeneric = {
|
||||
platform: string;
|
||||
}
|
||||
export type AutomationTrigger =
|
||||
| AutomationTriggerEvent
|
||||
| AutomationTriggerHA
|
||||
| AutomationTriggerMQTT
|
||||
| AutomationTriggerNumericState
|
||||
| AutomationTriggerState
|
||||
| AutomationTriggerTag
|
||||
| AutomationTriggerTemplate
|
||||
| AutomationTriggerTime
|
||||
| AutomationTriggerTimePattern
|
||||
| AutomationTriggerWebhook
|
||||
| AutomationTriggerZone
|
||||
| AutomationTriggerDevice
|
||||
| AutomationTriggerGeneric
|
||||
;
|
||||
export const triggerTypes = [
|
||||
'Event',
|
||||
'Home Assistant',
|
||||
'MQTT',
|
||||
'Numeric State',
|
||||
'State',
|
||||
'Tag',
|
||||
'Template',
|
||||
'Time',
|
||||
'Time Pattern',
|
||||
'Webhook',
|
||||
'Zone',
|
||||
'Device',
|
||||
'Generic',
|
||||
] as const;
|
||||
export type TriggerType = typeof triggerTypes[number];
|
||||
@@ -0,0 +1,215 @@
|
||||
|
||||
import {convertTimeToString, getDescriptionFromAutomationNode} from "./utils";
|
||||
|
||||
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({
|
||||
$smType: "action",
|
||||
action: "service",
|
||||
action_data: {
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
}
|
||||
})).toEqual("Start Music In Kitchen")
|
||||
})
|
||||
|
||||
|
||||
test('get service action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: "action",
|
||||
action: "service",
|
||||
action_data: {
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
}
|
||||
})).toEqual("media_player.play_media")
|
||||
})
|
||||
|
||||
test('get repeat action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: "action",
|
||||
action: "repeat",
|
||||
action_data: {
|
||||
repeat: {
|
||||
count: 10,
|
||||
sequence: []
|
||||
}
|
||||
}
|
||||
})).toEqual("Repeat 10")
|
||||
})
|
||||
|
||||
|
||||
test('get wait action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: "action",
|
||||
action: "wait",
|
||||
action_data: {
|
||||
wait_template: "states(switch.kitchen_light) == 'on'"
|
||||
}
|
||||
})).toEqual("Wait on states(switch.kitchen_light) == 'on'")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: "action",
|
||||
action: "wait",
|
||||
action_data: {
|
||||
wait_template: "states(switch.kitchen_light) == 'on'",
|
||||
// timeout: '00:01:00',
|
||||
timeout: {
|
||||
'minutes': 1,
|
||||
}
|
||||
}
|
||||
})).toEqual("Wait on states(switch.kitchen_light) == 'on' for 00:01:00:00")
|
||||
})
|
||||
|
||||
test('get fire event action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'action',
|
||||
action: 'event',
|
||||
action_data: {
|
||||
event: 'test_event',
|
||||
event_data: {},
|
||||
}
|
||||
})).toEqual("Trigger test_event")
|
||||
})
|
||||
|
||||
test('get device action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'action',
|
||||
action: 'device',
|
||||
action_data: {
|
||||
device_id: "12310das01231",
|
||||
domain: "zwave_js",
|
||||
type: "set_value"
|
||||
}
|
||||
|
||||
})).toEqual("set_value on 12310das01231")
|
||||
})
|
||||
|
||||
test('get choose action description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'action',
|
||||
action: 'choose',
|
||||
action_data: {
|
||||
choose: [],
|
||||
default: [],
|
||||
}
|
||||
})).toEqual("Choose")
|
||||
})
|
||||
|
||||
test('get and/or/not condition description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'condition',
|
||||
condition: 'and',
|
||||
condition_data: {
|
||||
conditions: [],
|
||||
}
|
||||
})).toEqual("Logic")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'condition',
|
||||
condition: 'or',
|
||||
condition_data: {
|
||||
conditions: [],
|
||||
}
|
||||
})).toEqual("Logic")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'condition',
|
||||
condition: 'not',
|
||||
condition_data: {
|
||||
conditions: [],
|
||||
}
|
||||
})).toEqual("Logic")
|
||||
})
|
||||
|
||||
test('get Numeric State condition description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'condition',
|
||||
condition: 'numeric_state',
|
||||
condition_data: {
|
||||
entity_id: 'sensor.temperature',
|
||||
}
|
||||
})).toEqual("sensor.temperature?>?<")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'condition',
|
||||
condition: 'numeric_state',
|
||||
condition_data: {
|
||||
entity_id: ""
|
||||
}
|
||||
})).toEqual("Numeric State Condition")
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'condition',
|
||||
condition: "numeric_state",
|
||||
condition_data: {
|
||||
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',
|
||||
}
|
||||
})).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 }}"
|
||||
}
|
||||
})).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 }}"
|
||||
}
|
||||
})).toEqual("17 < ... < 20")
|
||||
})
|
||||
|
||||
test('get State condition description', () => {
|
||||
expect(getDescriptionFromAutomationNode({
|
||||
$smType: 'condition',
|
||||
condition: 'state',
|
||||
condition_data: {
|
||||
entity_id: 'switch.kitchen_light',
|
||||
state: "on"
|
||||
}
|
||||
})).toEqual("switch.kitchen_light is 'on'")
|
||||
})
|
||||
@@ -0,0 +1,145 @@
|
||||
import { AutomationNode, AutomationNodeTypes } from "./types";
|
||||
import { AutomationTime } from "./types/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(':')
|
||||
|
||||
export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(node: AutomationNode<N>): string => {
|
||||
if ('$smType' in node) {
|
||||
switch (node['$smType']) {
|
||||
case "action":
|
||||
if (node.action_data.alias) {
|
||||
return node.action_data.alias;
|
||||
}
|
||||
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;
|
||||
if (node.action_data.timeout) {
|
||||
out += (' for ' + convertTimeToString(node.action_data.timeout))
|
||||
}
|
||||
return out;
|
||||
case 'event':
|
||||
return `Trigger ${node.action_data.event}`
|
||||
case 'device':
|
||||
return `${node.action_data.type} on ${node.action_data.device_id}`
|
||||
case 'choose':
|
||||
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 = typeof node.condition_data.entity_id === 'string' ? 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 "Numeric State Condition"
|
||||
case "state":
|
||||
return `${node.condition_data.entity_id} is '${node.condition_data.state}'`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 'n/a'
|
||||
}
|
||||
|
||||
// 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)}`)
|
||||
// }
|
||||
Reference in new issue
Block a user