updated zone condition
This commit is contained in:
1 parent
a32e365d86
commit
ab8ecfeefb
1 file changed
+239
-166
@@ -1,5 +1,14 @@
|
|||||||
import { FC } from "react";
|
import { FC } from "react";
|
||||||
import { AutomationCondition, LogicCondition, NumericCondition, StateCondition, TemplateCondition, TimeCondition, TriggerCondition, ZoneCondition } from 'types/automations/conditions';
|
import {
|
||||||
|
AutomationCondition,
|
||||||
|
LogicCondition,
|
||||||
|
NumericCondition,
|
||||||
|
StateCondition,
|
||||||
|
TemplateCondition,
|
||||||
|
TimeCondition,
|
||||||
|
TriggerCondition,
|
||||||
|
ZoneCondition,
|
||||||
|
} from "types/automations/conditions";
|
||||||
import InputNumber from "components/Inputs/InputNumber";
|
import InputNumber from "components/Inputs/InputNumber";
|
||||||
import InputText from "components/Inputs/InputText";
|
import InputText from "components/Inputs/InputText";
|
||||||
import { InputEntity } from "components/Inputs/InputEntities";
|
import { InputEntity } from "components/Inputs/InputEntities";
|
||||||
@@ -8,209 +17,273 @@ import { genUpdateMethods } from "./nestedUpdater";
|
|||||||
import { InputTime } from "components/Inputs/InputTime";
|
import { InputTime } from "components/Inputs/InputTime";
|
||||||
import InputYaml from "components/Inputs/InputYaml";
|
import InputYaml from "components/Inputs/InputYaml";
|
||||||
|
|
||||||
|
interface Editor<C extends AutomationCondition>
|
||||||
interface Editor<C extends AutomationCondition> extends FC<{
|
extends FC<{
|
||||||
condition: C,
|
condition: C;
|
||||||
onChange: (condition: C) => void;
|
onChange: (condition: C) => void;
|
||||||
}> { }
|
}> {}
|
||||||
|
|
||||||
|
|
||||||
export const getEditor = (condition: AutomationCondition): Editor<any> => {
|
export const getEditor = (condition: AutomationCondition): Editor<any> => {
|
||||||
switch (condition.condition) {
|
switch (condition.condition) {
|
||||||
case 'template':
|
case "template":
|
||||||
return TemplateEditor;
|
return TemplateEditor;
|
||||||
case 'and':
|
case "and":
|
||||||
return LogicViewer;
|
return LogicViewer;
|
||||||
case 'or':
|
case "or":
|
||||||
return LogicViewer;
|
return LogicViewer;
|
||||||
case 'not':
|
case "not":
|
||||||
return LogicViewer;
|
return LogicViewer;
|
||||||
case 'numeric_state':
|
case "numeric_state":
|
||||||
return NumericStateEditor;
|
return NumericStateEditor;
|
||||||
case 'state':
|
case "state":
|
||||||
return StateEditor;
|
return StateEditor;
|
||||||
case 'time':
|
case "time":
|
||||||
return TimeEditor;
|
return TimeEditor;
|
||||||
case 'trigger':
|
case "trigger":
|
||||||
return TriggerEditor;
|
return TriggerEditor;
|
||||||
case 'zone':
|
case "zone":
|
||||||
return ZoneEditor;
|
return ZoneEditor;
|
||||||
default:
|
default:
|
||||||
return () => <div>Not Ready</div>
|
return () => <div>Not Ready</div>;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const TemplateEditor: Editor<TemplateCondition> = ({
|
export const TemplateEditor: Editor<TemplateCondition> = ({
|
||||||
onChange,
|
onChange,
|
||||||
condition,
|
condition,
|
||||||
}) => {
|
}) => {
|
||||||
return <>
|
return (
|
||||||
<InputYaml label="Template" value={condition.value_template} onChange={value_template => onChange({
|
<>
|
||||||
...condition,
|
<InputYaml
|
||||||
value_template
|
label="Template"
|
||||||
})} />
|
value={condition.value_template}
|
||||||
</>
|
onChange={(value_template) =>
|
||||||
}
|
onChange({
|
||||||
|
...condition,
|
||||||
|
value_template,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const NumericStateEditor: Editor<NumericCondition> = ({
|
export const NumericStateEditor: Editor<NumericCondition> = ({
|
||||||
onChange,
|
onChange,
|
||||||
condition,
|
condition,
|
||||||
}) => {
|
}) => {
|
||||||
return <>
|
return (
|
||||||
<InputEntity
|
<>
|
||||||
value={condition.entity_id}
|
<InputEntity
|
||||||
multiple
|
value={condition.entity_id}
|
||||||
onChange={entity_id => onChange({
|
multiple
|
||||||
...condition,
|
onChange={(entity_id) =>
|
||||||
entity_id
|
onChange({
|
||||||
})}
|
...condition,
|
||||||
/>
|
entity_id,
|
||||||
<InputText label="Attribute" value={condition.attribute ?? ""} onChange={attribute => onChange({
|
})
|
||||||
...condition,
|
}
|
||||||
attribute
|
/>
|
||||||
})} />
|
<InputText
|
||||||
<InputNumber
|
label="Attribute"
|
||||||
label="Above"
|
value={condition.attribute ?? ""}
|
||||||
value={condition.above ? Number(condition.above) : undefined}
|
onChange={(attribute) =>
|
||||||
onChange={above => onChange({
|
onChange({
|
||||||
...condition,
|
...condition,
|
||||||
above: above ? String(above) : undefined,
|
attribute,
|
||||||
})}
|
})
|
||||||
/>
|
}
|
||||||
<InputNumber
|
/>
|
||||||
label="Below"
|
<InputNumber
|
||||||
value={condition.below ? Number(condition.below) : undefined}
|
label="Above"
|
||||||
onChange={below => onChange({
|
value={condition.above ? Number(condition.above) : undefined}
|
||||||
...condition,
|
onChange={(above) =>
|
||||||
below: below ? String(below) : undefined,
|
onChange({
|
||||||
})}
|
...condition,
|
||||||
/>
|
above: above ? String(above) : undefined,
|
||||||
<InputYaml label="Template" value={condition.value_template ?? ""} onChange={value_template => onChange({
|
})
|
||||||
...condition,
|
}
|
||||||
value_template
|
/>
|
||||||
})} />
|
<InputNumber
|
||||||
</>
|
label="Below"
|
||||||
}
|
value={condition.below ? Number(condition.below) : undefined}
|
||||||
|
onChange={(below) =>
|
||||||
|
onChange({
|
||||||
|
...condition,
|
||||||
|
below: below ? String(below) : undefined,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InputYaml
|
||||||
|
label="Template"
|
||||||
|
value={condition.value_template ?? ""}
|
||||||
|
onChange={(value_template) =>
|
||||||
|
onChange({
|
||||||
|
...condition,
|
||||||
|
value_template,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const LogicViewer: Editor<LogicCondition> = ({
|
export const LogicViewer: Editor<LogicCondition> = ({
|
||||||
condition,
|
condition,
|
||||||
onChange,
|
onChange,
|
||||||
}) => {
|
}) => {
|
||||||
|
return (
|
||||||
return <>
|
<>
|
||||||
{condition.conditions.map((c, i) => {
|
{condition.conditions.map((c, i) => {
|
||||||
return <ConditionNode
|
return (
|
||||||
key={i}
|
<ConditionNode
|
||||||
condition={c}
|
key={i}
|
||||||
displayMode={false}
|
condition={c}
|
||||||
{...genUpdateMethods(condition, onChange)(i)}
|
displayMode={false}
|
||||||
showDelete
|
{...genUpdateMethods(condition, onChange)(i)}
|
||||||
/>
|
showDelete
|
||||||
})}
|
/>
|
||||||
</>
|
);
|
||||||
}
|
})}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const StateEditor: Editor<StateCondition> = ({
|
export const StateEditor: Editor<StateCondition> = ({
|
||||||
onChange,
|
onChange,
|
||||||
condition,
|
condition,
|
||||||
}) => {
|
}) => {
|
||||||
return <>
|
return (
|
||||||
<InputEntity
|
<>
|
||||||
value={condition.entity_id}
|
<InputEntity
|
||||||
multiple
|
value={condition.entity_id}
|
||||||
onChange={entity_id => onChange({
|
multiple
|
||||||
...condition,
|
onChange={(entity_id) =>
|
||||||
entity_id
|
onChange({
|
||||||
})}
|
...condition,
|
||||||
/>
|
entity_id,
|
||||||
<InputText label="Attribute" value={condition.attribute ?? ""} onChange={attribute => onChange({
|
})
|
||||||
...condition,
|
}
|
||||||
attribute
|
/>
|
||||||
})} />
|
<InputText
|
||||||
<InputText
|
label="Attribute"
|
||||||
label="State"
|
value={condition.attribute ?? ""}
|
||||||
value={String(condition.state)}
|
onChange={(attribute) =>
|
||||||
onChange={state => onChange({
|
onChange({
|
||||||
...condition,
|
...condition,
|
||||||
state,
|
attribute,
|
||||||
})}
|
})
|
||||||
/>
|
}
|
||||||
<InputTime
|
/>
|
||||||
label="For"
|
<InputText
|
||||||
value={condition.for}
|
label="State"
|
||||||
onChange={_for => onChange({
|
value={String(condition.state)}
|
||||||
...condition,
|
onChange={(state) =>
|
||||||
for: _for,
|
onChange({
|
||||||
})}
|
...condition,
|
||||||
/>
|
state,
|
||||||
</>
|
})
|
||||||
}
|
}
|
||||||
|
/>
|
||||||
|
<InputTime
|
||||||
|
label="For"
|
||||||
|
value={condition.for}
|
||||||
|
onChange={(_for) =>
|
||||||
|
onChange({
|
||||||
|
...condition,
|
||||||
|
for: _for,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const TimeEditor: Editor<TimeCondition> = ({
|
export const TimeEditor: Editor<TimeCondition> = ({ onChange, condition }) => {
|
||||||
onChange,
|
return (
|
||||||
condition,
|
<>
|
||||||
}) => {
|
<InputTime
|
||||||
return <>
|
label="After"
|
||||||
<InputTime
|
value={condition.after}
|
||||||
label="After"
|
onChange={(after) =>
|
||||||
value={condition.after}
|
onChange({
|
||||||
onChange={after => onChange({
|
...condition,
|
||||||
...condition,
|
after,
|
||||||
after,
|
})
|
||||||
})}
|
}
|
||||||
/>
|
/>
|
||||||
<InputTime
|
<InputTime
|
||||||
label="Before"
|
label="Before"
|
||||||
value={condition.before}
|
value={condition.before}
|
||||||
onChange={before => onChange({
|
onChange={(before) =>
|
||||||
...condition,
|
onChange({
|
||||||
before,
|
...condition,
|
||||||
})}
|
before,
|
||||||
/>
|
})
|
||||||
<b>Weekday not support for no, use yaml for now</b> {condition.weekday}
|
}
|
||||||
</>
|
/>
|
||||||
}
|
<b>Weekday not support for no, use yaml for now</b> {condition.weekday}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const TriggerEditor: Editor<TriggerCondition> = ({
|
export const TriggerEditor: Editor<TriggerCondition> = ({
|
||||||
onChange,
|
onChange,
|
||||||
condition,
|
condition,
|
||||||
}) => {
|
}) => {
|
||||||
return <>
|
return (
|
||||||
<InputText label="Trigger ID" value={condition.id} onChange={id => onChange({
|
<>
|
||||||
...condition,
|
<InputText
|
||||||
id
|
label="Trigger ID"
|
||||||
})} />
|
value={condition.id}
|
||||||
</>
|
onChange={(id) =>
|
||||||
}
|
onChange({
|
||||||
|
...condition,
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const ZoneEditor: Editor<ZoneCondition> = ({
|
export const ZoneEditor: Editor<ZoneCondition> = ({ onChange, condition }) => {
|
||||||
onChange,
|
return (
|
||||||
condition,
|
<>
|
||||||
}) => {
|
<InputEntity
|
||||||
return <>
|
label="Zone"
|
||||||
<InputText label="Zone" value={condition.zone} onChange={zone => onChange({
|
value={condition.zone}
|
||||||
...condition,
|
multiple={false}
|
||||||
zone
|
onChange={(zone) =>
|
||||||
})} />
|
onChange({
|
||||||
<InputEntity
|
...condition,
|
||||||
value={condition.entity_id}
|
zone: zone ?? "",
|
||||||
multiple
|
})
|
||||||
onChange={entity_id => onChange({
|
}
|
||||||
...condition,
|
restrictToDomain={["zone"]}
|
||||||
entity_id
|
/>
|
||||||
})}
|
<InputEntity
|
||||||
/>
|
label="Entity with Location"
|
||||||
<InputText
|
value={condition.entity_id}
|
||||||
label="State"
|
multiple
|
||||||
value={String(condition.state)}
|
onChange={(entity_id) =>
|
||||||
onChange={state => onChange({
|
onChange({
|
||||||
...condition,
|
...condition,
|
||||||
state,
|
entity_id,
|
||||||
})}
|
})
|
||||||
/>
|
}
|
||||||
</>
|
restrictToDomain={["person", "device_tracker"]}
|
||||||
}
|
/>
|
||||||
|
<InputText
|
||||||
|
label="State"
|
||||||
|
value={String(condition.state)}
|
||||||
|
onChange={(state) =>
|
||||||
|
onChange({
|
||||||
|
...condition,
|
||||||
|
state,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
Reference in new issue
Block a user