removed $smType for conditions and trigger
This commit is contained in:
1 parent
49fea3fabd
commit
429dfd5b50
41 files changed
+410
-723
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import "./AutoInfoBox.css";
|
||||
import { AutomationMetadata } from "types/automations";
|
||||
import InputList from "components/Inputs/InputList";
|
||||
import { InputList } from "components/Inputs/InputList";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import InputTextArea from "components/Inputs/InputTextArea";
|
||||
import { FC, useRef, useState } from 'react';
|
||||
|
||||
@@ -60,39 +60,28 @@ Simple.args = {
|
||||
},
|
||||
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",
|
||||
|
||||
@@ -12,48 +12,32 @@ export default {
|
||||
|
||||
export const Basic: ComponentStory<typeof ConditionEditor> = args => {
|
||||
const [state, setState] = useState<AutomationCondition>({
|
||||
"$smType": "condition",
|
||||
"condition": "or",
|
||||
"condition_data": {
|
||||
"conditions": [
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "and",
|
||||
"condition_data": {
|
||||
"conditions": [
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "numeric_state",
|
||||
"condition_data": {
|
||||
"entity_id": [
|
||||
"sensor.kitchen_humidity"
|
||||
],
|
||||
"conditions": [],
|
||||
"above": "40"
|
||||
}
|
||||
},
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "template",
|
||||
"condition_data": {
|
||||
"value_template": "states('switch.kitchen') == 'on'"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "numeric_state",
|
||||
"condition_data": {
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "and",
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": [
|
||||
"sensor.kitchen_temperature"
|
||||
"sensor.kitchen_humidity"
|
||||
],
|
||||
"above": "51"
|
||||
"above": "40"
|
||||
},
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": "states('switch.kitchen') == 'on'"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": [
|
||||
"sensor.kitchen_temperature"
|
||||
],
|
||||
"above": "51"
|
||||
}
|
||||
]
|
||||
})
|
||||
return (
|
||||
<div className="page">
|
||||
|
||||
@@ -3,7 +3,7 @@ import { FC, useState, useEffect } from 'react';
|
||||
import { getConditionDefaultValues } from "utils/defaults";
|
||||
import { AutomationCondition } from "types/automations/conditions";
|
||||
import { CheckMarkIcon, PencilIcon } from "components/Icons";
|
||||
import InputList from "components/Inputs/InputList";
|
||||
import { InputList } from "components/Inputs/InputList";
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
import { getEditor } from "./editorRender";
|
||||
import { useStyles } from "./style";
|
||||
@@ -51,17 +51,10 @@ export const ConditionNode: FC<{
|
||||
) {
|
||||
onUpdate({
|
||||
...condition,
|
||||
$smType: 'condition',
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
conditions: condition.condition_data.conditions.concat({
|
||||
$smType: 'condition',
|
||||
condition: 'or',
|
||||
condition_data: {
|
||||
conditions: []
|
||||
}
|
||||
})
|
||||
}
|
||||
conditions: condition.conditions.concat({
|
||||
condition: 'or',
|
||||
conditions: []
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -69,20 +62,10 @@ export const ConditionNode: FC<{
|
||||
let childrenConditions: JSX.Element;
|
||||
if (effectiveDM) {
|
||||
const Viewer = getViewer(condition);
|
||||
childrenConditions = <Viewer condition={condition} onChange={data => {
|
||||
onUpdate({
|
||||
...data,
|
||||
$smType: 'condition',
|
||||
})
|
||||
}} />
|
||||
childrenConditions = <Viewer condition={condition} onChange={onUpdate} />
|
||||
} else {
|
||||
const Editor = getEditor(condition);
|
||||
childrenConditions = <Editor condition={condition} onChange={data => {
|
||||
onUpdate({
|
||||
...data,
|
||||
$smType: 'condition',
|
||||
})
|
||||
}} />
|
||||
childrenConditions = <Editor condition={condition} onChange={onUpdate} />
|
||||
}
|
||||
// styles
|
||||
let hasChildren = false;
|
||||
@@ -115,13 +98,10 @@ export const ConditionNode: FC<{
|
||||
'trigger',
|
||||
'zone',
|
||||
]}
|
||||
onChange={n => onUpdate({
|
||||
$smType: 'condition',
|
||||
onChange={(n: any) => onUpdate({
|
||||
...getConditionDefaultValues(n),
|
||||
...condition,
|
||||
condition: n,
|
||||
condition_data: {
|
||||
...getConditionDefaultValues(n),
|
||||
...condition.condition_data,
|
||||
}
|
||||
} as any)}
|
||||
/>
|
||||
{effectiveDM && <button className={classes.yamlBtn} onClick={() => setYamlMode(!yamlMode)}>
|
||||
@@ -134,11 +114,8 @@ export const ConditionNode: FC<{
|
||||
<div className={classes.children}>
|
||||
{yamlMode && <InputYaml
|
||||
label=""
|
||||
value={condition.condition_data}
|
||||
onChange={update => onUpdate({
|
||||
...condition,
|
||||
condition_data: update as any
|
||||
})}
|
||||
value={condition}
|
||||
onChange={update => onUpdate(update as any)}
|
||||
resizable
|
||||
/>}
|
||||
{!yamlMode && childrenConditions}
|
||||
|
||||
@@ -46,19 +46,13 @@ export const TemplateEditor: Editor<TemplateCondition> = ({
|
||||
condition,
|
||||
}) => {
|
||||
return <div>
|
||||
<InputText label="Alias" value={condition.condition_data.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
<InputText label="Alias" value={condition.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
alias
|
||||
}
|
||||
alias
|
||||
})} />
|
||||
<InputTextArea label="Template" value={condition.condition_data.value_template} onChange={value_template => onChange({
|
||||
<InputTextArea label="Template" value={condition.value_template} onChange={value_template => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
value_template
|
||||
}
|
||||
value_template
|
||||
})} resizable />
|
||||
</div>
|
||||
}
|
||||
@@ -69,58 +63,40 @@ export const NumericStateEditor: Editor<NumericCondition> = ({
|
||||
condition,
|
||||
}) => {
|
||||
return <div>
|
||||
<InputText label="Alias" value={condition.condition_data.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
<InputText label="Alias" value={condition.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
alias
|
||||
}
|
||||
alias
|
||||
})} />
|
||||
<InputEntity
|
||||
value={condition.condition_data.entity_id}
|
||||
value={condition.entity_id}
|
||||
onChange={entity_id => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
entity_id
|
||||
}
|
||||
entity_id
|
||||
})}
|
||||
/>
|
||||
<InputText label="Attribute" value={condition.condition_data.attribute ?? ""} onChange={attribute => onChange({
|
||||
<InputText label="Attribute" value={condition.attribute ?? ""} onChange={attribute => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
attribute
|
||||
}
|
||||
attribute
|
||||
})} />
|
||||
<InputNumber
|
||||
label="Above"
|
||||
value={condition.condition_data.above ? Number(condition.condition_data.above) : undefined}
|
||||
value={condition.above ? Number(condition.above) : undefined}
|
||||
onChange={above => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
above: above ? String(above) : undefined,
|
||||
}
|
||||
above: above ? String(above) : undefined,
|
||||
})}
|
||||
/>
|
||||
<InputNumber
|
||||
label="Below"
|
||||
value={condition.condition_data.below ? Number(condition.condition_data.below) : undefined}
|
||||
value={condition.below ? Number(condition.below) : undefined}
|
||||
onChange={below => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
below: below ? String(below) : undefined,
|
||||
}
|
||||
below: below ? String(below) : undefined,
|
||||
})}
|
||||
/>
|
||||
<InputTextArea label="Template" value={condition.condition_data.value_template ?? ""} onChange={value_template => onChange({
|
||||
<InputTextArea label="Template" value={condition.value_template ?? ""} onChange={value_template => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
value_template
|
||||
}
|
||||
value_template
|
||||
})} resizable />
|
||||
</div>
|
||||
}
|
||||
@@ -132,7 +108,7 @@ export const LogicViewer: Editor<LogicCondition> = ({
|
||||
}) => {
|
||||
|
||||
return <>
|
||||
{condition.condition_data.conditions.map((c, i) => {
|
||||
{condition.conditions.map((c, i) => {
|
||||
return <ConditionNode
|
||||
key={i}
|
||||
condition={c}
|
||||
@@ -151,50 +127,35 @@ export const StateEditor: Editor<StateCondition> = ({
|
||||
condition,
|
||||
}) => {
|
||||
return <div>
|
||||
<InputText label="Alias" value={condition.condition_data.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
<InputText label="Alias" value={condition.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
alias
|
||||
}
|
||||
alias
|
||||
})} />
|
||||
<InputEntity
|
||||
value={condition.condition_data.entity_id}
|
||||
value={condition.entity_id}
|
||||
onChange={entity_id => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
entity_id
|
||||
}
|
||||
entity_id
|
||||
})}
|
||||
/>
|
||||
<InputText label="Attribute" value={condition.condition_data.attribute ?? ""} onChange={attribute => onChange({
|
||||
<InputText label="Attribute" value={condition.attribute ?? ""} onChange={attribute => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
attribute
|
||||
}
|
||||
attribute
|
||||
})} />
|
||||
<InputText
|
||||
label="State"
|
||||
value={String(condition.condition_data.state)}
|
||||
value={String(condition.state)}
|
||||
onChange={state => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
state,
|
||||
}
|
||||
state,
|
||||
})}
|
||||
/>
|
||||
<InputTime
|
||||
label="For"
|
||||
value={condition.condition_data.for}
|
||||
value={condition.for}
|
||||
onChange={_for => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
for: _for,
|
||||
}
|
||||
for: _for,
|
||||
})}
|
||||
/>
|
||||
</div>
|
||||
@@ -205,36 +166,27 @@ export const TimeEditor: Editor<TimeCondition> = ({
|
||||
condition,
|
||||
}) => {
|
||||
return <div>
|
||||
<InputText label="Alias" value={condition.condition_data.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
<InputText label="Alias" value={condition.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
alias
|
||||
}
|
||||
alias
|
||||
})} />
|
||||
<InputTime
|
||||
label="After"
|
||||
value={condition.condition_data.after}
|
||||
value={condition.after}
|
||||
onChange={after => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
after,
|
||||
}
|
||||
after,
|
||||
})}
|
||||
/>
|
||||
<InputTime
|
||||
label="Before"
|
||||
value={condition.condition_data.before}
|
||||
value={condition.before}
|
||||
onChange={before => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
before,
|
||||
}
|
||||
before,
|
||||
})}
|
||||
/>
|
||||
<b>Weekday not support for no, use yaml for now</b> {condition.condition_data.weekday}
|
||||
<b>Weekday not support for no, use yaml for now</b> {condition.weekday}
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -243,19 +195,13 @@ export const TriggerEditor: Editor<TriggerCondition> = ({
|
||||
condition,
|
||||
}) => {
|
||||
return <div>
|
||||
<InputText label="Alias" value={condition.condition_data.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
<InputText label="Alias" value={condition.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
alias
|
||||
}
|
||||
alias
|
||||
})} />
|
||||
<InputText label="Trigger ID" value={condition.condition_data.id} onChange={id => onChange({
|
||||
<InputText label="Trigger ID" value={condition.id} onChange={id => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
id
|
||||
}
|
||||
id
|
||||
})} />
|
||||
</div>
|
||||
}
|
||||
@@ -265,39 +211,27 @@ export const ZoneEditor: Editor<ZoneCondition> = ({
|
||||
condition,
|
||||
}) => {
|
||||
return <div>
|
||||
<InputText label="Alias" value={condition.condition_data.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
<InputText label="Alias" value={condition.alias ?? getDescriptionFromAutomationNode(condition)} onChange={alias => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
alias
|
||||
}
|
||||
alias
|
||||
})} />
|
||||
<InputText label="Zone" value={condition.condition_data.zone} onChange={zone => onChange({
|
||||
<InputText label="Zone" value={condition.zone} onChange={zone => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
zone
|
||||
}
|
||||
zone
|
||||
})} />
|
||||
<InputEntity
|
||||
value={condition.condition_data.entity_id}
|
||||
value={condition.entity_id}
|
||||
onChange={entity_id => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
entity_id
|
||||
}
|
||||
entity_id
|
||||
})}
|
||||
/>
|
||||
<InputText
|
||||
label="State"
|
||||
value={String(condition.condition_data.state)}
|
||||
value={String(condition.state)}
|
||||
onChange={state => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
state,
|
||||
}
|
||||
state,
|
||||
})}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -27,11 +27,8 @@ const Template: ComponentStory<typeof ConditionEditor> = ({ condition, ...args }
|
||||
export const TemplateViewer = Template.bind({})
|
||||
TemplateViewer.args = {
|
||||
condition: {
|
||||
$smType: 'condition',
|
||||
condition: 'template',
|
||||
condition_data: {
|
||||
value_template: "states('switch.light_kitchen') == 'on'"
|
||||
}
|
||||
value_template: "states('switch.light_kitchen') == 'on'"
|
||||
} as AutomationCondition,
|
||||
}
|
||||
|
||||
@@ -39,63 +36,42 @@ TemplateViewer.args = {
|
||||
export const NumericStateViewer = Template.bind({})
|
||||
NumericStateViewer.args = {
|
||||
condition: {
|
||||
$smType: 'condition',
|
||||
condition: "numeric_state",
|
||||
condition_data: {
|
||||
entity_id: "sensor.humidity_kitchen",
|
||||
above: "10",
|
||||
}
|
||||
entity_id: "sensor.humidity_kitchen",
|
||||
above: "10",
|
||||
} as AutomationCondition,
|
||||
}
|
||||
|
||||
export const LogicCondition = Template.bind({})
|
||||
LogicCondition.args = {
|
||||
condition: {
|
||||
$smType: 'condition',
|
||||
condition: 'or',
|
||||
condition_data: {
|
||||
conditions: [
|
||||
{
|
||||
$smType: 'condition',
|
||||
condition: 'numeric_state',
|
||||
condition_data: {
|
||||
entity_id: 'sensor.humidity_kitchen',
|
||||
conditions: [
|
||||
{
|
||||
condition: 'numeric_state',
|
||||
entity_id: 'sensor.humidity_kitchen',
|
||||
above: '60'
|
||||
},
|
||||
{
|
||||
condition: 'numeric_state',
|
||||
entity_id: 'sensor.humidity_living_room',
|
||||
above: '60'
|
||||
},
|
||||
{
|
||||
condition: 'and',
|
||||
conditions: [
|
||||
{
|
||||
condition: 'numeric_state',
|
||||
entity_id: 'sensor.humidity_bedroom',
|
||||
above: '60'
|
||||
},
|
||||
{
|
||||
condition: 'numeric_state',
|
||||
entity_id: 'sensor.humidity_bathroom',
|
||||
above: '60'
|
||||
}
|
||||
},
|
||||
{
|
||||
$smType: 'condition',
|
||||
condition: 'numeric_state',
|
||||
condition_data: {
|
||||
entity_id: 'sensor.humidity_living_room',
|
||||
above: '60'
|
||||
}
|
||||
},
|
||||
{
|
||||
$smType: 'condition',
|
||||
condition: 'and',
|
||||
condition_data: {
|
||||
conditions: [
|
||||
{
|
||||
$smType: 'condition',
|
||||
condition: 'numeric_state',
|
||||
condition_data: {
|
||||
entity_id: 'sensor.humidity_bedroom',
|
||||
above: '60'
|
||||
}
|
||||
},
|
||||
{
|
||||
$smType: 'condition',
|
||||
condition: 'numeric_state',
|
||||
condition_data: {
|
||||
entity_id: 'sensor.humidity_bathroom',
|
||||
above: '60'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
]
|
||||
} as AutomationCondition,
|
||||
}
|
||||
@@ -2,30 +2,24 @@ import { AutomationCondition, LogicCondition } from "types/automations/condition
|
||||
|
||||
|
||||
export const genUpdateMethods = (
|
||||
condition: LogicCondition,
|
||||
onChange: (c: LogicCondition) => void
|
||||
condition: LogicCondition,
|
||||
onChange: (c: LogicCondition) => void
|
||||
) => (i: number) => ({
|
||||
onUpdate: (update: AutomationCondition) => onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
conditions: [
|
||||
...condition.condition_data.conditions.slice(0, i),
|
||||
update,
|
||||
...condition.condition_data.conditions.slice(i+1)
|
||||
]
|
||||
}
|
||||
conditions: [
|
||||
...condition.conditions.slice(0, i),
|
||||
update,
|
||||
...condition.conditions.slice(i + 1)
|
||||
]
|
||||
}),
|
||||
onDelete: (which: 'root' | number) => {
|
||||
if (which === 'root') {
|
||||
onChange({
|
||||
...condition,
|
||||
condition_data: {
|
||||
...condition.condition_data,
|
||||
conditions: condition.condition_data.conditions.slice(0, i).concat(
|
||||
condition.condition_data.conditions.slice(i+1)
|
||||
)
|
||||
}
|
||||
conditions: condition.conditions.slice(0, i).concat(
|
||||
condition.conditions.slice(i + 1)
|
||||
)
|
||||
})
|
||||
} else {
|
||||
throw new Error("Not Implemented")
|
||||
|
||||
@@ -34,7 +34,7 @@ export const LogicViewer: Viewer<LogicCondition> = ({
|
||||
onChange,
|
||||
}) => {
|
||||
return <>
|
||||
{condition.condition_data.conditions.map((c, i) => {
|
||||
{condition.conditions.map((c, i) => {
|
||||
return <ConditionNode key={i} condition={c} displayMode
|
||||
{...genUpdateMethods(condition, onChange)(i)}
|
||||
/>
|
||||
|
||||
@@ -49,39 +49,28 @@ Simple.args = {
|
||||
...Simple.args,
|
||||
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",
|
||||
@@ -107,39 +96,28 @@ Complex.args = {
|
||||
...Complex.args,
|
||||
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",
|
||||
@@ -164,12 +142,9 @@ Complex.args = {
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "state",
|
||||
"condition_data": {
|
||||
"entity_id": "lights.bathroom",
|
||||
"state": "off",
|
||||
}
|
||||
"entity_id": "lights.bathroom",
|
||||
"state": "off",
|
||||
}
|
||||
],
|
||||
"sequence": [
|
||||
@@ -201,12 +176,9 @@ Complex.args = {
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "state",
|
||||
"condition_data": {
|
||||
"entity_id": "lights.bedroom",
|
||||
"state": "off",
|
||||
}
|
||||
"entity_id": "lights.bedroom",
|
||||
"state": "off",
|
||||
}
|
||||
],
|
||||
"sequence": [
|
||||
@@ -272,11 +244,8 @@ Complex.args = {
|
||||
}
|
||||
},
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "and",
|
||||
"condition_data": {
|
||||
"conditions": []
|
||||
}
|
||||
"conditions": []
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -72,8 +72,7 @@ export const DAGAutomationFlow: FC<Props> = ({
|
||||
onAdd: () => onTriggerUpdate([
|
||||
...trigger, {
|
||||
platform: 'event',
|
||||
$smType: 'trigger',
|
||||
event_type: 'test',
|
||||
event_type: '',
|
||||
}
|
||||
]),
|
||||
onDelete: i => onTriggerUpdate([
|
||||
|
||||
@@ -5,6 +5,7 @@ import { XYPosition } from 'react-flow-renderer';
|
||||
import { ChooseAction } from 'types/automations/actions';
|
||||
import { makeFlowCircle } from './DAGCircle';
|
||||
import { createOnRemoveForChooseNode, SequenceUpdater } from './updater';
|
||||
import { getNodeType } from 'utils/automations';
|
||||
|
||||
|
||||
export const sequenceToFlow = (
|
||||
@@ -34,7 +35,7 @@ export const sequenceToFlow = (
|
||||
};
|
||||
}
|
||||
// draw node
|
||||
if (node.$smType === 'action') {
|
||||
if ('$smType' in node && node.$smType === 'action') {
|
||||
if (node.action === 'choose') {
|
||||
position = addChooseNode(flowData, node, position, nodeId, dims, updater, i)
|
||||
} else {
|
||||
@@ -102,7 +103,7 @@ const addSingleNode = (
|
||||
label: getDescriptionFromAutomationNode(node),
|
||||
height: nodeHeight,
|
||||
width: nodeWidth,
|
||||
color: node.$smType === 'action' ? 'green' : 'blue',
|
||||
color: getNodeType(node) === 'action' ? 'green' : 'blue',
|
||||
hasInput: true,
|
||||
...opts,
|
||||
},
|
||||
|
||||
@@ -73,11 +73,8 @@ test('update conditions for node', () => {
|
||||
expect((mock.modalStates[0].node as AutomationSequenceNode[]).length).toBe(0)
|
||||
mock.modalStates[0].update([
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "and",
|
||||
"condition_data": {
|
||||
"conditions": [],
|
||||
}
|
||||
"conditions": [],
|
||||
}
|
||||
] as any)
|
||||
expect((mock.state[0] as ChooseAction).action_data.choose[0].conditions.length).toBe(1)
|
||||
|
||||
@@ -53,7 +53,7 @@ export const makeSequenceUpdater = (
|
||||
},
|
||||
makeChildUpdaterForChooseAction(i: number, j: number | null) {
|
||||
const node = sequence[i];
|
||||
if ((node.$smType === 'action') && (node.action === 'choose')) {
|
||||
if (('$smType' in node) && (node.$smType === 'action') && (node.action === 'choose')) {
|
||||
if (j !== null) {
|
||||
return makeSequenceUpdater(
|
||||
node.action_data.choose[j].sequence,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { useState } from "react";
|
||||
|
||||
import InputList from "./InputList";
|
||||
import { InputList } from "./InputList";
|
||||
|
||||
export default {
|
||||
title: 'Inputs/InputList',
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { FC } from "react";
|
||||
import InputWrapper from "./InputWrapper";
|
||||
import { useInputListStyles } from "./styles";
|
||||
|
||||
@@ -10,10 +11,10 @@ export interface Props<T extends string> {
|
||||
className?: string;
|
||||
label: string;
|
||||
}
|
||||
export default function InputList<T extends string>({
|
||||
export const InputList: FC<Props<any>> = ({
|
||||
current, options, onChange, label,
|
||||
...selattr
|
||||
}: Props<T>) {
|
||||
}) => {
|
||||
const { classes } = useInputListStyles({});
|
||||
return <InputWrapper label={label} labelSize={'small'} noMargin>
|
||||
<select
|
||||
|
||||
@@ -12,22 +12,16 @@ export default {
|
||||
args: {
|
||||
sequence: [
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "numeric_state",
|
||||
"condition_data": {
|
||||
"entity_id": [
|
||||
"sensor.kitchen_humidity"
|
||||
],
|
||||
"conditions": [],
|
||||
"above": "40"
|
||||
}
|
||||
"entity_id": [
|
||||
"sensor.kitchen_humidity"
|
||||
],
|
||||
"conditions": [],
|
||||
"above": "40"
|
||||
},
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "template",
|
||||
"condition_data": {
|
||||
"value_template": "states('switch.kitchen') == 'on'"
|
||||
}
|
||||
"value_template": "states('switch.kitchen') == 'on'"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -19,11 +19,8 @@ const makeDefault = (nodeType: keyof AutomationNodeMapping): AutomationNode => {
|
||||
}
|
||||
case 'condition':
|
||||
return {
|
||||
$smType: 'condition',
|
||||
condition: 'and',
|
||||
condition_data: {
|
||||
conditions: []
|
||||
}
|
||||
conditions: []
|
||||
}
|
||||
default:
|
||||
return {
|
||||
|
||||
@@ -5,12 +5,9 @@ import { OptionManager } from "./OptionManager";
|
||||
|
||||
export const ConditionLogicState: OptionManager<AutomationCondition> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'condition',
|
||||
condition: 'and',
|
||||
condition_data: {
|
||||
alias: "",
|
||||
conditions: [],
|
||||
}
|
||||
alias: "",
|
||||
conditions: [],
|
||||
}),
|
||||
isReady: _ => true,
|
||||
renderOptionList: (state, setState) => <ConditionEditor
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { AutomationAction } from 'types/automations/actions';
|
||||
import { AutomationCondition } from 'types/automations/conditions';
|
||||
export interface OptionManager<T> {
|
||||
defaultState(): T ;
|
||||
defaultState(): T;
|
||||
isReady(s: T): boolean;
|
||||
renderOptionList(s: T, set: (s:T) => void): JSX.Element;
|
||||
renderOptionList(s: T, set: (s: T) => void): JSX.Element;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,24 +11,21 @@ export const updateConditionData = <T extends AutomationCondition>(
|
||||
setState: (s: T) => void
|
||||
) => (
|
||||
state: T,
|
||||
data: Partial<T['condition_data']>,
|
||||
) => setState({
|
||||
...state,
|
||||
condition_data: {
|
||||
...state.condition_data,
|
||||
data: Partial<T>,
|
||||
) => setState({
|
||||
...state,
|
||||
...data,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
export const updateActionData = <T extends AutomationAction>(
|
||||
state: T,
|
||||
setState: (s: T) => void
|
||||
) => (
|
||||
data: Partial<T['action_data']>,
|
||||
) => setState({
|
||||
...state,
|
||||
) => setState({
|
||||
...state,
|
||||
action_data: {
|
||||
...state.action_data,
|
||||
...data,
|
||||
},
|
||||
})
|
||||
...state.action_data,
|
||||
...data,
|
||||
},
|
||||
})
|
||||
@@ -5,7 +5,6 @@ import InputText from 'components/Inputs/InputText';
|
||||
|
||||
export const TriggerDevice: OptionManager<AutomationTriggerDevice> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": 'device',
|
||||
"domain": "",
|
||||
|
||||
@@ -6,7 +6,6 @@ import InputYaml from "components/Inputs/InputYaml";
|
||||
|
||||
export const TriggerEventState: OptionManager<AutomationTriggerEvent> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": "event",
|
||||
"event_type": "",
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { OptionManager } from './OptionManager';
|
||||
import { AutomationTriggerHA } from 'types/automations/triggers';
|
||||
import InputList from "components/Inputs/InputList";
|
||||
import { InputList } from "components/Inputs/InputList";
|
||||
|
||||
|
||||
export const TriggerHAState: OptionManager<AutomationTriggerHA> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "HA Starts",
|
||||
"platform": "homeassistant",
|
||||
"event": "start",
|
||||
|
||||
@@ -5,7 +5,6 @@ import InputText from 'components/Inputs/InputText';
|
||||
|
||||
export const TriggerMQTTState: OptionManager<AutomationTriggerMQTT> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": "mqtt",
|
||||
"payload": "",
|
||||
|
||||
@@ -9,7 +9,6 @@ import InputTime from 'components/Inputs/InputTime';
|
||||
|
||||
export const TriggerNumericState: OptionManager<AutomationTriggerNumericState> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": 'numeric_state',
|
||||
"entity_id": "",
|
||||
|
||||
@@ -7,7 +7,6 @@ import InputTime from 'components/Inputs/InputTime';
|
||||
|
||||
export const TriggerState: OptionManager<AutomationTriggerState> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": 'state',
|
||||
"entity_id": "",
|
||||
|
||||
@@ -6,7 +6,6 @@ import InputTime from 'components/Inputs/InputTime';
|
||||
|
||||
export const TriggerTemplate: OptionManager<AutomationTriggerTemplate> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": 'template',
|
||||
"value_template": "",
|
||||
|
||||
@@ -5,7 +5,6 @@ import InputTime from 'components/Inputs/InputTime';
|
||||
|
||||
export const TriggerTime: OptionManager<AutomationTriggerTime> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": 'time',
|
||||
"at": {},
|
||||
|
||||
@@ -5,7 +5,6 @@ import InputText from 'components/Inputs/InputText';
|
||||
|
||||
export const TriggerTimePattern: OptionManager<AutomationTriggerTimePattern> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": 'time_pattern',
|
||||
}),
|
||||
|
||||
@@ -5,7 +5,6 @@ import InputText from 'components/Inputs/InputText';
|
||||
|
||||
export const TriggerWebhook: OptionManager<AutomationTriggerWebhook> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": 'webhook',
|
||||
"webhook_id": "",
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { OptionManager } from './OptionManager';
|
||||
import { AutomationTriggerZone } from 'types/automations/triggers';
|
||||
import InputText from 'components/Inputs/InputText';
|
||||
import InputList from 'components/Inputs/InputList';
|
||||
import { InputList } from 'components/Inputs/InputList';
|
||||
import { InputEntity } from 'components/Inputs/InputTextBubble';
|
||||
|
||||
|
||||
export const TriggerZone: OptionManager<AutomationTriggerZone> = {
|
||||
defaultState: () => ({
|
||||
$smType: 'trigger',
|
||||
"alias": "",
|
||||
"platform": 'zone',
|
||||
"zone": "",
|
||||
|
||||
@@ -2,6 +2,10 @@ import { useEffect, useState } from 'react';
|
||||
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
|
||||
import { AutomationNodeSubtype } from 'types/automations';
|
||||
import { getOptionManager } from './getOptionManager';
|
||||
import { getNodeType, getSubTypeList } from 'utils/automations';
|
||||
import { AutomationCondition } from 'types/automations/conditions';
|
||||
import { AutomationAction } from 'types/automations/actions';
|
||||
import { AutomationTrigger } from 'types/automations/triggers';
|
||||
|
||||
|
||||
export const useEditorNodeState = (node: AutomationNode) => {
|
||||
@@ -12,12 +16,13 @@ export const useEditorNodeState = (node: AutomationNode) => {
|
||||
useEffect(() => {
|
||||
setAllState({ node, isModified: false })
|
||||
}, [node])
|
||||
const setState = (node: AutomationNode) => setAllState(({ node, isModified: true }))
|
||||
const nodeType: AutomationNodeTypes = state.$smType ?? 'trigger';
|
||||
const setState = (node: AutomationNode) => setAllState(({ node, isModified: true }));
|
||||
const originalNodeType = getNodeType(node);
|
||||
const nodeType = getNodeType(state);
|
||||
const subType: AutomationNodeSubtype =
|
||||
state.$smType === 'action' ? state.action :
|
||||
state.$smType === 'condition' ? state.condition :
|
||||
state.platform;
|
||||
nodeType === 'action' ? (state as AutomationAction).action :
|
||||
nodeType === 'condition' ? (state as AutomationCondition).condition :
|
||||
(state as AutomationTrigger).platform;
|
||||
const subTypes = getSubTypeList(nodeType)
|
||||
const optionManager = getOptionManager(nodeType, subType as any);
|
||||
return {
|
||||
@@ -37,24 +42,22 @@ export const useEditorNodeState = (node: AutomationNode) => {
|
||||
return optionManager.isReady(state);
|
||||
},
|
||||
setNodeType(newType: AutomationNodeTypes) {
|
||||
if (newType === (node.$smType ?? 'trigger')) {
|
||||
if (node.$smType === 'action') {
|
||||
if (newType === originalNodeType) {
|
||||
if (originalNodeType === 'action') {
|
||||
setState({
|
||||
...state,
|
||||
$smType: 'action',
|
||||
action: node.action,
|
||||
action: (node as any).action,
|
||||
} as any)
|
||||
} else if (node.$smType === 'condition') {
|
||||
} else if (originalNodeType === 'condition') {
|
||||
setState({
|
||||
...state,
|
||||
$smType: 'condition',
|
||||
condition: node.condition
|
||||
condition: (node as any).condition
|
||||
} as any)
|
||||
} else {
|
||||
setState({
|
||||
...state,
|
||||
$smType: undefined,
|
||||
platform: node.platform
|
||||
platform: (node as any).platform
|
||||
} as any)
|
||||
}
|
||||
} else {
|
||||
@@ -66,23 +69,23 @@ export const useEditorNodeState = (node: AutomationNode) => {
|
||||
}
|
||||
},
|
||||
setSubType(newSubType: AutomationNodeSubtype<typeof nodeType>) {
|
||||
if (state.$smType === 'action') {
|
||||
if (nodeType === 'action') {
|
||||
setState({
|
||||
...state,
|
||||
action: newSubType,
|
||||
...getOptionManager(state.$smType, newSubType as any).defaultState()
|
||||
...getOptionManager(nodeType, newSubType as any).defaultState()
|
||||
} as any)
|
||||
} else if (state.$smType === 'condition') {
|
||||
} else if (nodeType === 'condition') {
|
||||
setState({
|
||||
...state,
|
||||
condition: newSubType,
|
||||
...getOptionManager(state.$smType, newSubType as any).defaultState()
|
||||
...getOptionManager(nodeType, newSubType as any).defaultState()
|
||||
} as any)
|
||||
} else {
|
||||
setState({
|
||||
...state,
|
||||
platform: newSubType,
|
||||
...getOptionManager(state.$smType, newSubType as any).defaultState()
|
||||
...getOptionManager(nodeType, newSubType as any).defaultState()
|
||||
} as any)
|
||||
}
|
||||
}
|
||||
@@ -90,44 +93,3 @@ export const useEditorNodeState = (node: AutomationNode) => {
|
||||
}
|
||||
|
||||
|
||||
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 [];
|
||||
}
|
||||
}
|
||||
@@ -40,13 +40,13 @@ const triggerTypes = [
|
||||
|
||||
export const getSubTypeList = <T extends AutomationNodeTypes>(
|
||||
nodeType: T
|
||||
): Array<AutomationNodeSubtype<T>| 'condition'> => {
|
||||
): Array<AutomationNodeSubtype<T> | 'condition'> => {
|
||||
if (nodeType === 'action') {
|
||||
return actionTypes as any;
|
||||
} else if (nodeType === 'condition') {
|
||||
return conditionTypes as any;
|
||||
} else if (nodeType === 'trigger') {
|
||||
return triggerTypes as any;
|
||||
return triggerTypes as any;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
@@ -54,11 +54,13 @@ export const getSubTypeList = <T extends AutomationNodeTypes>(
|
||||
export const getNodeSubType = <T extends AutomationNodeTypes>(
|
||||
node: AutomationNode<T>
|
||||
): AutomationNodeSubtype<T> => {
|
||||
if (node.$smType === 'action') {
|
||||
if ('$smType' in node && node.$smType === 'action') {
|
||||
return node.action as any;
|
||||
} else if(node.$smType === 'condition') {
|
||||
} else if ('condition' in node) {
|
||||
return node.condition as any;
|
||||
} else {
|
||||
} else if ('platform' in node) {
|
||||
return node.platform as any;
|
||||
} else {
|
||||
throw new Error("Invalid node type!")
|
||||
}
|
||||
}
|
||||
@@ -41,30 +41,21 @@ export const Condition = Action.bind({})
|
||||
Condition.args = {
|
||||
...Condition.args,
|
||||
node: {
|
||||
"$smType": "condition",
|
||||
"condition": "and",
|
||||
"condition_data": {
|
||||
"conditions": [
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "numeric_state",
|
||||
"condition_data": {
|
||||
"entity_id": [
|
||||
"sensor.kitchen_humidity"
|
||||
],
|
||||
"conditions": [],
|
||||
"above": "40"
|
||||
}
|
||||
},
|
||||
{
|
||||
"$smType": "condition",
|
||||
"condition": "template",
|
||||
"condition_data": {
|
||||
"value_template": "states('switch.kitchen') == 'on'"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": [
|
||||
"sensor.kitchen_humidity"
|
||||
],
|
||||
"conditions": [],
|
||||
"above": "40"
|
||||
},
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": "states('switch.kitchen') == 'on'"
|
||||
}
|
||||
]
|
||||
} as AutomationCondition,
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import "./index.css";
|
||||
import { FC } from "react";
|
||||
import { AutomationNode, AutomationNodeMapping } from 'types/automations/index';
|
||||
import { useEditorNodeState } from "./OptionManager";
|
||||
import InputList from "components/Inputs/InputList";
|
||||
import { InputList } from "components/Inputs/InputList";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
|
||||
export interface Props {
|
||||
|
||||
@@ -1,44 +1,41 @@
|
||||
import { AutomationTime } from "./common";
|
||||
|
||||
interface AutomationConditionNodeBase<Name extends String, Data extends Object> {
|
||||
$smType: 'condition';
|
||||
type AutomationConditionNodeBase<Name extends String, Data extends Object> = {
|
||||
condition: Name;
|
||||
condition_data: Data & {
|
||||
alias?: string;
|
||||
};
|
||||
}
|
||||
export interface LogicCondition extends AutomationConditionNodeBase<'and' | 'or' | 'not', {
|
||||
alias?: string;
|
||||
} & Data;
|
||||
export type LogicCondition = AutomationConditionNodeBase<'and' | 'or' | 'not', {
|
||||
conditions: AutomationCondition[];
|
||||
}> { }
|
||||
export interface NumericCondition extends AutomationConditionNodeBase<'numeric_state', {
|
||||
}>;
|
||||
export type NumericCondition = AutomationConditionNodeBase<'numeric_state', {
|
||||
entity_id: string | string[];
|
||||
above?: string;
|
||||
below?: string;
|
||||
attribute?: string;
|
||||
value_template?: string;
|
||||
}> { }
|
||||
export interface StateCondition extends AutomationConditionNodeBase<'state', {
|
||||
}>;
|
||||
export type StateCondition = AutomationConditionNodeBase<'state', {
|
||||
entity_id: string | string[];
|
||||
state: string | string[];
|
||||
for?: AutomationTime;
|
||||
attribute?: string;
|
||||
}> { }
|
||||
export interface TemplateCondition extends AutomationConditionNodeBase<'template', {
|
||||
}>;
|
||||
export type TemplateCondition = AutomationConditionNodeBase<'template', {
|
||||
value_template: string;
|
||||
}> { }
|
||||
export interface TimeCondition extends AutomationConditionNodeBase<'time', {
|
||||
}>;
|
||||
export type TimeCondition = AutomationConditionNodeBase<'time', {
|
||||
after?: AutomationTime;
|
||||
before?: AutomationTime;
|
||||
weekday?: Array<'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun'>;
|
||||
}> { }
|
||||
export interface TriggerCondition extends AutomationConditionNodeBase<'trigger', {
|
||||
}>;
|
||||
export type TriggerCondition = AutomationConditionNodeBase<'trigger', {
|
||||
id: string;
|
||||
}> { }
|
||||
export interface ZoneCondition extends AutomationConditionNodeBase<'zone', {
|
||||
}>;
|
||||
export type ZoneCondition = AutomationConditionNodeBase<'zone', {
|
||||
zone: string;
|
||||
entity_id: string | string[];
|
||||
state: string | string[];
|
||||
}> { }
|
||||
}>;
|
||||
export type AutomationCondition =
|
||||
| LogicCondition
|
||||
| NumericCondition
|
||||
|
||||
@@ -2,7 +2,6 @@ import { AutomationTime } from "./common";
|
||||
|
||||
interface AutomationTriggerBase {
|
||||
alias?: string;
|
||||
$smType: undefined | 'trigger';
|
||||
}
|
||||
|
||||
export interface AutomationTriggerEvent extends AutomationTriggerBase {
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
}
|
||||
@@ -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!")
|
||||
}
|
||||
|
||||
@@ -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
@@ -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
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user