added more state managers

This commit is contained in:
Ari Sosnovsky committed 2021-11-03 09:27:21 -04:00
1 parent 08a50051d5
commit f66895d0eb
14 files changed
+266 -14

No files matched your search

+2
View File
@@ -1,5 +1,6 @@
{
"dependencies": {
"js-yaml": "^4.1.0",
"parcel": "^2.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
@@ -9,6 +10,7 @@
"@parcel/transformer-sass": "^2.0.0",
"@parcel/transformer-yaml": "^2.0.0",
"@types/jest": "^27.0.2",
"@types/js-yaml": "^4.0.4",
"@types/node": "^16.11.6",
"@types/react": "^17.0.33",
"@types/react-dom": "^17.0.10",
@@ -0,0 +1,40 @@
import InputBoolean from "../../components/InputBoolean";
import InputYaml from "../../components/InputYaml";
import InputNumber from "../../components/InputNumber";
import InputText from "../../components/InputText";
import { BaseState } from "./BaseState";
import { LogicCondition } from "../types/condition";
import InputList from "../../components/InputList";
export default class ConditionLogicState extends BaseState<LogicCondition> {
defaultState = () => ({
alias: "",
condition: 'and',
conditions: [],
} as LogicCondition)
isReady(state: LogicCondition): boolean {
return state.alias !== '' &&
(state?.conditions)?.length > 0
}
renderOptionList(state: LogicCondition): JSX.Element {
return <div className="state-manager-options">
<InputText
label="Description"
value={state.alias}
onChange={alias => this.setState({...state, alias})}
/>
<InputList
label="Condition"
current={state.condition}
options={['and', 'or', 'not']}
onChange={(condition: any) => this.setState({...state, condition})}
/>
<InputYaml
label="Sequence"
value={state.conditions}
onChange={conditions => this.setState({...state, conditions})}
/>
</div>
}
}
@@ -0,0 +1,37 @@
import InputBoolean from "../../components/InputBoolean";
import InputYaml from "../../components/InputYaml";
import InputNumber from "../../components/InputNumber";
import InputText from "../../components/InputText";
import { ChooseScript } from "../types/script";
import { BaseState } from "./BaseState";
export default class ScriptChooseState extends BaseState<ChooseScript> {
defaultState = () => ({
alias: "Choose",
default: [],
choose: [],
})
isReady(state: ChooseScript): boolean {
return state.alias !== ''
}
renderOptionList(state: ChooseScript): JSX.Element {
return <div className="state-manager-options">
<InputText
label="Description"
value={state.alias}
onChange={alias => this.setState({...state, alias})}
/>
<InputYaml
label="Choose Sequence"
value={state.choose}
onChange={choose => this.setState({...state, choose})}
/>
<InputYaml
label="Default Sequence"
value={state.default}
onChange={d => this.setState({...state, default: d})}
/>
</div>
}
}
@@ -0,0 +1,51 @@
import InputBoolean from "../../components/InputBoolean";
import InputYaml from "../../components/InputYaml";
import InputNumber from "../../components/InputNumber";
import InputText from "../../components/InputText";
import { DeviceScript } from "../types/script";
import { BaseState } from "./BaseState";
export default class ScriptDeviceState extends BaseState<DeviceScript> {
defaultState = () => ({
alias: "Device",
type: "",
entity_id: "",
device_id: "",
domain: "",
})
isReady(state: DeviceScript): boolean {
return state.alias !== '' &&
state.type !== "" &&
state.entity_id !== "" &&
state.device_id !== "" &&
state.domain !== ""
}
renderOptionList(state: DeviceScript): JSX.Element {
return <div className="state-manager-options">
<InputText
label="Description"
value={state.alias}
onChange={alias => this.setState({...state, alias})}
/>
<InputText
textBoxFor="device_id"
label="Device"
value={state.device_id}
onChange={device_id => this.setState({...state, device_id})}
/>
<InputText
textBoxFor="type"
label="Action"
value={state.type}
onChange={type => this.setState({...state, type})}
/>
<InputText
textBoxFor="entity_id"
label="Entity ID"
value={state.entity_id}
onChange={entity_id => this.setState({...state, entity_id, domain: entity_id.split('.')[0]})}
/>
</div>
}
}
@@ -0,0 +1,39 @@
import InputBoolean from "../../components/InputBoolean";
import InputYaml from "../../components/InputYaml";
import InputNumber from "../../components/InputNumber";
import InputText from "../../components/InputText";
import { FireEventScript } from "../types/script";
import { BaseState } from "./BaseState";
export default class ScriptEventState extends BaseState<FireEventScript> {
defaultState = () => ({
alias: "Fire Event",
event: "",
event_data: {},
})
isReady(state: FireEventScript): boolean {
return state.alias !== '' &&
state.event !== ""
}
renderOptionList(state: FireEventScript): JSX.Element {
return <div className="state-manager-options">
<InputText
label="Description"
value={state.alias}
onChange={alias => this.setState({...state, alias})}
/>
<InputText
textBoxFor="event"
label="Event"
value={state.event}
onChange={event => this.setState({...state, event})}
/>
<InputYaml
label="Event Data"
value={state.event_data}
onChange={event_data => this.setState({...state, event_data})}
/>
</div>
}
}
@@ -1,5 +1,6 @@
import InputNumber from "../../components/InputNumber";
import InputText from "../../components/InputText";
import InputYaml from "../../components/InputYaml";
import { RepeatScript } from "../types/script";
import { BaseState } from "./BaseState";
@@ -28,9 +29,11 @@ export default class ScriptRepeatState extends BaseState<RepeatScript> {
value={state?.repeat?.count}
onChange={count => this.setState({...state, repeat: {...state.repeat, count}})}
/>
<div>
{JSON.stringify(state?.repeat?.sequence)}
</div>
<InputYaml
label="Sequence"
value={state?.repeat?.sequence ?? {}}
onChange={sequence => this.setState({...state, repeat: {...state.repeat, sequence}})}
/>
</div>
}
}
@@ -25,7 +25,7 @@ export default class ScriptWaitState extends BaseState<WaitScript> {
/>
<InputText
textBoxFor="template"
label="Timeout"
label="Template"
value={state.wait_template}
onChange={wait_template => this.setState({...state, wait_template})}
/>
@@ -1,4 +1,8 @@
import { AutomationNodeType, NodeSubType } from "../types"
import ConditionLogicState from "./ConditionLogic"
import ScriptChooseState from "./ScriptChoose"
import ScriptDeviceState from "./ScriptDevice"
import ScriptEventState from "./ScriptEvent"
import ScriptRepeatState from "./ScriptRepeat"
import ScriptCallServiceState from "./ScriptService"
import ScriptWaitState from "./ScriptWait"
@@ -16,6 +20,18 @@ export const getBaseState = <T extends AutomationNodeType>(
return ScriptRepeatState;
case "Wait":
return ScriptWaitState;
case "Event":
return ScriptEventState;
case "Device":
return ScriptDeviceState;
case "Choose":
return ScriptChooseState;
}
}
if (node_type === 'condition') {
switch (node_subtype) {
case 'Logic':
return ConditionLogicState
}
}
throw new Error(`Invalid or Not implemented node type ${node_type} -> ${node_subtype}`)
+1 -3
View File
@@ -57,9 +57,7 @@ export type AutomationCondition =
| GenericCondition
;
export const conditionTypes = [
'And' ,
'Or' ,
'Not',
'Logic',
'Numeric State',
'State',
'Template',
+3 -3
View File
@@ -33,11 +33,11 @@ export const determineNodeTypes = (action: AutomationNode<any>) : [AutomationNod
if ('condition' in action) {
switch (action.condition) {
case 'and':
return ['condition', 'And']
return ['condition', 'Logic']
case 'or':
return ['condition', 'Or']
return ['condition', 'Logic']
case 'not':
return ['condition', 'Not']
return ['condition', 'Logic']
case 'numeric_state':
return ['condition', 'Numeric State']
case 'state':
+7 -4
View File
@@ -2,15 +2,18 @@ import { SelectHTMLAttributes } from "react";
import InputWrapper from "./InputWrapper";
export interface Props {
current: string;
options: readonly string[];
export interface Props<T extends string> {
current: T;
options: readonly T[];
onChange: (n: string) => void;
id?: string;
className?: string;
label: string;
}
export default function InputList({current, options, onChange, label, ...selattr}: Props) {
export default function InputList<T extends string>({
current, options, onChange, label,
...selattr
}: Props<T>) {
return <InputWrapper label={label}>
<select {...selattr} value={current} onChange={e => {
e.preventDefault();
+39
View File
@@ -0,0 +1,39 @@
import InputWrapper from "./InputWrapper";
import * as yaml from "js-yaml";
import { useEffect, useRef, useState } from "react";
export interface Props<T> {
label: string;
value: T;
onChange: (v: T) => void;
}
export default function InputYaml<T>({
label,
value={} as any,
onChange,
}: Props<T>) {
const [text, setText] = useState("");
useEffect(() => {
setText(yaml.dump(value));
}, [value]);
const delayId = useRef<null | number>(null);
const delayedUpdate = (update: string) => {
if (delayId.current) {
window.clearTimeout(delayId.current);
delayId.current = null;
}
delayId.current = window.setTimeout(() => {
onChange(yaml.load(update))
}, 1000);
}
return <InputWrapper label={label}>
<textarea
value={text}
onChange={e => {
e.preventDefault();
setText(e.target.value);
delayedUpdate(e.target.value);
}}
/>
</InputWrapper>
}
+7
View File
@@ -99,6 +99,13 @@ input[type="number"]::-webkit-inner-spin-button,
::-webkit-outer-spin-button {
opacity: 0;
}
textarea {
@extend button;
cursor: text;
margin: 0;
width: 100%;
height: 100%;
}
span.checkbox {
height: 16px;
width: 16px;
+17
View File
@@ -1327,6 +1327,11 @@
jest-diff "^27.0.0"
pretty-format "^27.0.0"
"@types/js-yaml@^4.0.4":
version "4.0.4"
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.4.tgz#cc38781257612581a1a0eb25f1709d2b06812fce"
integrity sha512-AuHubXUmg0AzkXH0Mx6sIxeY/1C110mm/EkE/gB1sTRz3h2dao2W/63q42SlVST+lICxz5Oki2hzYA6+KnnieQ==
"@types/node@*", "@types/node@^16.11.6":
version "16.11.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.6.tgz#6bef7a2a0ad684cf6e90fcfe31cecabd9ce0a3ae"
@@ -1543,6 +1548,11 @@ argparse@^1.0.7:
dependencies:
sprintf-js "~1.0.2"
argparse@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==
array-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/array-equal/-/array-equal-1.0.0.tgz#8c2a5ef2472fd9ea742b04c77a75093ba2757c93"
@@ -4209,6 +4219,13 @@ js-yaml@^3.10.0, js-yaml@^3.13.1:
argparse "^1.0.7"
esprima "^4.0.0"
js-yaml@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==
dependencies:
argparse "^2.0.1"
jsbn@~0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"