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