diff --git a/webapp/src/components/AutomationEditor/index.stories.tsx b/webapp/src/components/AutomationEditor/index.stories.tsx index 9860d14..8016148 100644 --- a/webapp/src/components/AutomationEditor/index.stories.tsx +++ b/webapp/src/components/AutomationEditor/index.stories.tsx @@ -168,3 +168,77 @@ BadAutomationInvalidSequence.args = { ] as any, }, }; + +export const RepeatExample = Template.bind({}); +RepeatExample.args = { + ...RepeatExample.args, + automation: { + metadata: { + id: "1654976151462", + alias: "Get out of baby room", + description: "", + mode: "single", + }, + trigger: [ + { + type: "opened", + platform: "device", + device_id: "9b33663d2700da8efe0c31c946448f77", + entity_id: "binary_sensor.door_baby", + domain: "binary_sensor", + }, + ], + condition: [], + sequence: [ + { + service: "notify.mobile_app_levas_phone", + data: { + message: "Someone is in Korie’s room!!!", + }, + }, + { + repeat: { + while: [ + { + type: "is_open", + condition: "device", + device_id: "9b33663d2700da8efe0c31c946448f77", + entity_id: "binary_sensor.door_baby", + domain: "binary_sensor", + }, + ], + sequence: [ + { + service: "notify.alexa_media", + data: { + message: + "Get the fuck out of the baby’s bedroom you sick fuck! This room belongs to Korie! Close the door! I said close. The. Door!", + data: null, + type: "tts", + target: "media_player.baby_bedroom_dot", + }, + }, + { + delay: { + hours: 0, + minutes: 0, + seconds: 7, + milliseconds: 0, + }, + }, + ], + }, + }, + { + service: "light.turn_off", + target: { + entity_id: ["light.switch_baby"], + }, + data: { + flash: "short", + }, + }, + ], + tags: {}, + }, +}; diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx index dd38eb1..703686e 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx @@ -1,37 +1,43 @@ import InputNumber from "components/Inputs/InputNumber"; import InputYaml from "components/Inputs/InputYaml"; import { RepeatAction } from "types/automations/actions"; -import { OptionManager, updateActionData } from './OptionManager'; - +import { OptionManager, updateActionData } from "./OptionManager"; export const ActionRepeatState: OptionManager = { defaultState: () => ({ alias: "Repeat", repeat: { - count: 1, + while: [], sequence: [], - } + }, }), - isReady: ({ - alias, - repeat, - }) => ( - alias !== '' && - repeat?.sequence.length > 0 - ), + isReady: ({ alias, repeat }) => alias !== "" && repeat?.sequence.length > 0, renderOptionList: (state, setState) => { const update = updateActionData(state, setState); - return <> - update({ repeat: { ...state.repeat, count } })} - /> - update({ repeat: { ...state.repeat, sequence } })} - /> - - } -} + return ( + <> + + update({ repeat: { ...state.repeat, count } }) + } + /> + + update({ repeat: { ...state.repeat, sequence } }) + } + /> + + update({ repeat: { ...state.repeat, sequence } }) + } + /> + + ); + }, +}; diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts index 4c725fa..da9589c 100644 --- a/webapp/src/types/automations/actions.ts +++ b/webapp/src/types/automations/actions.ts @@ -1,43 +1,42 @@ import { AutomationSequenceNode } from "."; import { AutomationCondition } from "./conditions"; import * as v from "types/validators/actions"; -import * as st from 'superstruct'; +import * as st from "superstruct"; type AutomationActionNodeBase = Data & - st.Infer; + st.Infer; export type ServiceAction = st.Infer; export type RepeatAction = AutomationActionNodeBase<{ - repeat: { - count: number; - sequence: AutomationAction[]; - } -}> + repeat: { + count?: number; + while: AutomationCondition[]; + sequence: AutomationAction[]; + }; +}>; export type WaitAction = st.Infer; export type DelayAction = st.Infer; export type FireEventAction = st.Infer; export type DeviceAction = st.Infer; export type ChooseAction = AutomationActionNodeBase<{ - choose: Array<{ - conditions: AutomationCondition[]; - sequence: AutomationSequenceNode[]; - }>; - default?: AutomationSequenceNode[]; -}> + choose: Array<{ + conditions: AutomationCondition[]; + sequence: AutomationSequenceNode[]; + }>; + default?: AutomationSequenceNode[]; +}>; export type AutomationAction = - | ServiceAction - | RepeatAction - | WaitAction - | DelayAction - | FireEventAction - | DeviceAction - | ChooseAction - ; + | ServiceAction + | RepeatAction + | WaitAction + | DelayAction + | FireEventAction + | DeviceAction + | ChooseAction; export type ActionType = - | 'service' - | 'repeat' - | 'wait' - | 'event' - | 'device' - | 'choose' - ; + | "service" + | "repeat" + | "wait" + | "event" + | "device" + | "choose"; diff --git a/webapp/src/types/validators/actions.ts b/webapp/src/types/validators/actions.ts index 6f069d7..8a75983 100644 --- a/webapp/src/types/validators/actions.ts +++ b/webapp/src/types/validators/actions.ts @@ -21,7 +21,8 @@ export const RepeatAction = st.intersection([ AutomationActionNodeBase, st.type({ repeat: st.object({ - count: st.number(), + count: st.optional(st.number()), + while: st.array(st.object()), sequence: st.array(st.object()), }), }), diff --git a/webapp/src/utils/formatting.test.ts b/webapp/src/utils/formatting.test.ts index dae0344..da3c79f 100644 --- a/webapp/src/utils/formatting.test.ts +++ b/webapp/src/utils/formatting.test.ts @@ -75,6 +75,7 @@ test("get repeat action description", () => { getDescriptionFromAutomationNode( { repeat: { + while: [], count: 10, sequence: [], },