fixed typing for repeat
This commit is contained in:
1 parent
595aa2dfa1
commit
f392cf17fd
5 files changed
+135
-54
No files matched your search
@@ -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: {},
|
||||
},
|
||||
};
|
||||
@@ -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<RepeatAction> = {
|
||||
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 <>
|
||||
<InputNumber
|
||||
label="How many times?"
|
||||
value={state.repeat.count}
|
||||
onChange={(count = 1) => update({ repeat: { ...state.repeat, count } })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Sequence"
|
||||
value={state.repeat.sequence ?? []}
|
||||
onChange={sequence => update({ repeat: { ...state.repeat, sequence } })}
|
||||
/>
|
||||
</>
|
||||
}
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<InputNumber
|
||||
label="How many times?"
|
||||
value={state.repeat.count}
|
||||
onChange={(count = 1) =>
|
||||
update({ repeat: { ...state.repeat, count } })
|
||||
}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Sequence"
|
||||
value={state.repeat.sequence ?? []}
|
||||
onChange={(sequence) =>
|
||||
update({ repeat: { ...state.repeat, sequence } })
|
||||
}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Condition"
|
||||
value={state.repeat.while ?? []}
|
||||
onChange={(sequence) =>
|
||||
update({ repeat: { ...state.repeat, sequence } })
|
||||
}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -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 extends Object> = Data &
|
||||
st.Infer<typeof v.AutomationActionNodeBase>;
|
||||
st.Infer<typeof v.AutomationActionNodeBase>;
|
||||
|
||||
export type ServiceAction = st.Infer<typeof v.ServiceAction>;
|
||||
export type RepeatAction = AutomationActionNodeBase<{
|
||||
repeat: {
|
||||
count: number;
|
||||
sequence: AutomationAction[];
|
||||
}
|
||||
}>
|
||||
repeat: {
|
||||
count?: number;
|
||||
while: AutomationCondition[];
|
||||
sequence: AutomationAction[];
|
||||
};
|
||||
}>;
|
||||
export type WaitAction = st.Infer<typeof v.WaitAction>;
|
||||
export type DelayAction = st.Infer<typeof v.DelayAction>;
|
||||
export type FireEventAction = st.Infer<typeof v.FireEventAction>;
|
||||
export type DeviceAction = st.Infer<typeof v.DeviceAction>;
|
||||
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";
|
||||
@@ -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()),
|
||||
}),
|
||||
}),
|
||||
|
||||
@@ -75,6 +75,7 @@ test("get repeat action description", () => {
|
||||
getDescriptionFromAutomationNode(
|
||||
{
|
||||
repeat: {
|
||||
while: [],
|
||||
count: 10,
|
||||
sequence: [],
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user