added more support for trigger types
This commit is contained in:
1 parent
1cb40288b1
commit
754cc2ef98
9 files changed
+172
-55
No files matched your search
@@ -0,0 +1,39 @@
|
|||||||
|
import { OptionManager } from "./OptionManager";
|
||||||
|
import { AutomationTriggerCalendar } from "types/automations/triggers";
|
||||||
|
import { InputList } from "components/Inputs/InputList";
|
||||||
|
import InputText from "components/Inputs/Base/InputText";
|
||||||
|
|
||||||
|
export const TriggerCalendarState: OptionManager<AutomationTriggerCalendar> = {
|
||||||
|
defaultState: (): AutomationTriggerCalendar => ({
|
||||||
|
platform: "calendar",
|
||||||
|
event: "sunset",
|
||||||
|
}),
|
||||||
|
isReady: () => true,
|
||||||
|
Component: ({ state, setState }) => {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<InputList
|
||||||
|
label="Event Type"
|
||||||
|
current={state.event}
|
||||||
|
options={["start", "end"]}
|
||||||
|
onChange={(event) =>
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
event,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
label="Offset"
|
||||||
|
value={state.offset ?? ""}
|
||||||
|
onChange={(s) =>
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
offset: !s ? undefined : s,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { OptionManager } from "./OptionManager";
|
||||||
|
import { AutomationTriggerSun } from "types/automations/triggers";
|
||||||
|
import { InputList } from "components/Inputs/InputList";
|
||||||
|
import InputText from "components/Inputs/Base/InputText";
|
||||||
|
|
||||||
|
export const TriggerSunState: OptionManager<AutomationTriggerSun> = {
|
||||||
|
defaultState: (): AutomationTriggerSun => ({
|
||||||
|
platform: "sun",
|
||||||
|
event: "sunset",
|
||||||
|
}),
|
||||||
|
isReady: () => true,
|
||||||
|
Component: ({ state, setState }) => {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<InputList
|
||||||
|
label="Event Type"
|
||||||
|
current={state.event}
|
||||||
|
options={["sunset", "sunrise"]}
|
||||||
|
onChange={(event) =>
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
event,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
label="Offset"
|
||||||
|
value={state.offset ?? ""}
|
||||||
|
onChange={(s) =>
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
offset: !s ? undefined : s,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -8,7 +8,7 @@ export const TriggerWebhook: OptionManager<AutomationTriggerWebhook> = {
|
|||||||
webhook_id: "",
|
webhook_id: "",
|
||||||
}),
|
}),
|
||||||
isReady: () => true,
|
isReady: () => true,
|
||||||
renderOptionList: (state, setState) => {
|
Component: ({ state, setState }) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<InputText
|
<InputText
|
||||||
@@ -21,6 +21,10 @@ export const TriggerWebhook: OptionManager<AutomationTriggerWebhook> = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
<span>
|
||||||
|
You can run this automation by sending an HTTP POST request to
|
||||||
|
http://your-home-assistant-ip/api/webhook/{state.webhook_id}.
|
||||||
|
</span>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ import { TriggerZone } from "./TriggerZone";
|
|||||||
import { TriggerDevice } from "./TriggerDevice";
|
import { TriggerDevice } from "./TriggerDevice";
|
||||||
import { ActionStopState } from "./ActionStop";
|
import { ActionStopState } from "./ActionStop";
|
||||||
import { ActionParallelState } from "./ActionParallel";
|
import { ActionParallelState } from "./ActionParallel";
|
||||||
|
import { TriggerSunState } from "./TriggerSun";
|
||||||
|
import { TriggerCalendarState } from "./TriggerCalendar";
|
||||||
|
|
||||||
export const getOptionManager = <T extends AutomationNodeTypes>(
|
export const getOptionManager = <T extends AutomationNodeTypes>(
|
||||||
nodeType: T,
|
nodeType: T,
|
||||||
@@ -58,6 +60,10 @@ export const getOptionManager = <T extends AutomationNodeTypes>(
|
|||||||
return TriggerHAState;
|
return TriggerHAState;
|
||||||
case "mqtt":
|
case "mqtt":
|
||||||
return TriggerMQTTState;
|
return TriggerMQTTState;
|
||||||
|
case "sun":
|
||||||
|
return TriggerSunState;
|
||||||
|
case "calendar":
|
||||||
|
return TriggerCalendarState;
|
||||||
case "numeric_state":
|
case "numeric_state":
|
||||||
return TriggerNumericState;
|
return TriggerNumericState;
|
||||||
case "state":
|
case "state":
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
import { ComponentStory } from "@storybook/react";
|
||||||
import { NodeEditor } from ".";
|
import { NodeEditor, NodeEditorProps } from ".";
|
||||||
|
|
||||||
import { AutomationCondition } from "types/automations/conditions";
|
import { AutomationCondition } from "types/automations/conditions";
|
||||||
import { Button } from "components/Inputs/Buttons/Button";
|
import { Button } from "components/Inputs/Buttons/Button";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { Modal } from "components/Modal";
|
import { Modal } from "components/Modal";
|
||||||
import { Page } from "components/Page";
|
import { Page } from "components/Page";
|
||||||
|
import { makeStory } from "devUtils";
|
||||||
|
|
||||||
export default {
|
const { componentMeta, make } = makeStory({
|
||||||
|
meta: {
|
||||||
title: "NodeEditor",
|
title: "NodeEditor",
|
||||||
component: NodeEditor,
|
|
||||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
|
||||||
args: {
|
args: {
|
||||||
node: {
|
node: {
|
||||||
data: {},
|
data: {},
|
||||||
@@ -19,9 +19,8 @@ export default {
|
|||||||
target: "",
|
target: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
} as ComponentMeta<typeof NodeEditor>;
|
},
|
||||||
|
Component: (props: NodeEditorProps) => {
|
||||||
const Base: ComponentStory<typeof NodeEditor> = (props) => {
|
|
||||||
const [state, setState] = useState(props.node);
|
const [state, setState] = useState(props.node);
|
||||||
return (
|
return (
|
||||||
<Page>
|
<Page>
|
||||||
@@ -35,23 +34,22 @@ const Base: ComponentStory<typeof NodeEditor> = (props) => {
|
|||||||
/>
|
/>
|
||||||
</Page>
|
</Page>
|
||||||
);
|
);
|
||||||
};
|
},
|
||||||
export const Action = Base.bind({});
|
});
|
||||||
export const SingleOption = Action.bind({});
|
|
||||||
SingleOption.args = {
|
export default componentMeta;
|
||||||
...SingleOption.args,
|
|
||||||
|
export const Action = make({});
|
||||||
|
export const SingleOption = make({
|
||||||
allowedTypes: ["action"],
|
allowedTypes: ["action"],
|
||||||
};
|
});
|
||||||
export const Condition = Action.bind({});
|
export const Condition = make({
|
||||||
Condition.args = {
|
|
||||||
...Condition.args,
|
|
||||||
node: {
|
node: {
|
||||||
condition: "and",
|
condition: "and",
|
||||||
conditions: [
|
conditions: [
|
||||||
{
|
{
|
||||||
condition: "numeric_state",
|
condition: "numeric_state",
|
||||||
entity_id: ["sensor.kitchen_humidity"],
|
entity_id: ["sensor.kitchen_humidity"],
|
||||||
conditions: [],
|
|
||||||
above: "40",
|
above: "40",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -59,26 +57,22 @@ Condition.args = {
|
|||||||
value_template: "states('switch.kitchen') == 'on'",
|
value_template: "states('switch.kitchen') == 'on'",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
} as AutomationCondition,
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
export const UnSupported = Action.bind({});
|
export const UnSupported = make({
|
||||||
UnSupported.args = {
|
|
||||||
...UnSupported.args,
|
|
||||||
node: {
|
node: {
|
||||||
condition: "bargs",
|
condition: "bargs",
|
||||||
platform: "rouge",
|
platform: "rouge",
|
||||||
} as any,
|
} as any,
|
||||||
};
|
});
|
||||||
|
|
||||||
export const DeviceTriggerExample = Action.bind({});
|
export const DeviceTriggerExample = make({
|
||||||
DeviceTriggerExample.args = {
|
|
||||||
...DeviceTriggerExample.args,
|
|
||||||
node: {
|
node: {
|
||||||
platform: "device",
|
platform: "device",
|
||||||
device_id: "bd83b10cb1c7aa028c12dade5b4e87d5",
|
device_id: "bd83b10cb1c7aa028c12dade5b4e87d5",
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
export const InAModal: ComponentStory<typeof NodeEditor> = (props) => {
|
export const InAModal: ComponentStory<typeof NodeEditor> = (props) => {
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
@@ -92,19 +86,15 @@ export const InAModal: ComponentStory<typeof NodeEditor> = (props) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const ServiceExample = Action.bind({});
|
export const ServiceExample = make({
|
||||||
ServiceExample.args = {
|
|
||||||
...ServiceExample.args,
|
|
||||||
node: {
|
node: {
|
||||||
target: {},
|
target: {},
|
||||||
data: {},
|
data: {},
|
||||||
service: "",
|
service: "",
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
export const ServiceWithDataExample = Action.bind({});
|
export const ServiceWithDataExample = make({
|
||||||
ServiceWithDataExample.args = {
|
|
||||||
...ServiceWithDataExample.args,
|
|
||||||
node: {
|
node: {
|
||||||
target: {
|
target: {
|
||||||
entity: "light.switch_baby",
|
entity: "light.switch_baby",
|
||||||
@@ -114,37 +104,29 @@ ServiceWithDataExample.args = {
|
|||||||
},
|
},
|
||||||
service: "light.turn_on",
|
service: "light.turn_on",
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
export const DelayExample = Action.bind({});
|
export const DelayExample = make({
|
||||||
DelayExample.args = {
|
|
||||||
...DelayExample.args,
|
|
||||||
node: {
|
node: {
|
||||||
wait_template: "",
|
wait_template: "",
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
export const StateTriggerExample = Action.bind({});
|
export const StateTriggerExample = make({
|
||||||
StateTriggerExample.args = {
|
|
||||||
...StateTriggerExample.args,
|
|
||||||
node: {
|
node: {
|
||||||
platform: "state",
|
platform: "state",
|
||||||
entity_id: "binary_sensor.door_main",
|
entity_id: "binary_sensor.door_main",
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
export const DeviceActionFilledExample = Action.bind({});
|
export const DeviceActionFilledExample = make({
|
||||||
DeviceActionFilledExample.args = {
|
|
||||||
...DeviceActionFilledExample.args,
|
|
||||||
node: {
|
node: {
|
||||||
type: "toggle",
|
type: "toggle",
|
||||||
device_id: "c9711be86b0301955f72380509285485",
|
device_id: "c9711be86b0301955f72380509285485",
|
||||||
},
|
} as any,
|
||||||
};
|
});
|
||||||
|
|
||||||
export const NotifyPhoneViaDeviceAction = Action.bind({});
|
export const NotifyPhoneViaDeviceAction = make({
|
||||||
NotifyPhoneViaDeviceAction.args = {
|
|
||||||
...NotifyPhoneViaDeviceAction.args,
|
|
||||||
node: {
|
node: {
|
||||||
device_id: "2a98db84af7526c7ec3cc7aebd1a9e1c",
|
device_id: "2a98db84af7526c7ec3cc7aebd1a9e1c",
|
||||||
domain: "mobile_app",
|
domain: "mobile_app",
|
||||||
@@ -152,4 +134,11 @@ NotifyPhoneViaDeviceAction.args = {
|
|||||||
title: "Hassio Status",
|
title: "Hassio Status",
|
||||||
message: "Hassio Is turning off...",
|
message: "Hassio Is turning off...",
|
||||||
} as any,
|
} as any,
|
||||||
};
|
});
|
||||||
|
|
||||||
|
export const Sun = make({
|
||||||
|
node: {
|
||||||
|
platform: "sun",
|
||||||
|
event: "sunset",
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -8,6 +8,20 @@ const subtypeDescMap: Partial<Record<AutomationNodeSubtype, string>> = {
|
|||||||
repeat: "repeat a sequence until or while some conditions are met",
|
repeat: "repeat a sequence until or while some conditions are met",
|
||||||
device: "device related actions",
|
device: "device related actions",
|
||||||
service: "call a service",
|
service: "call a service",
|
||||||
|
event: "Fires when an event is being received.",
|
||||||
|
homeassistant: "Fires when Home Assistant starts up or shuts down.",
|
||||||
|
mqtt: "Fires when a specific message is received on given MQTT topic.",
|
||||||
|
sun: "Fires when the sun is setting or rising.",
|
||||||
|
numeric_state:
|
||||||
|
"Fires when the numeric value of an entity's state or attribute changes.",
|
||||||
|
state: "Fires when an entity's state or attribute changes",
|
||||||
|
template: "Evaluate a custom template",
|
||||||
|
time: "Specify a certain time to run",
|
||||||
|
webhook:
|
||||||
|
"Webhook trigger fires when a web request is made to the webhook endpoint",
|
||||||
|
zone: "Specify when an entity enters or leaves a zone",
|
||||||
|
tag: "Fires when a tag is scanned.",
|
||||||
|
calendar: "Fires when a Calendar event starts or ends.",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getNodeSubTypeDescription = (
|
export const getNodeSubTypeDescription = (
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ export type AutomationTriggerZone = st.Infer<typeof v.AutomationTriggerZone>;
|
|||||||
export type AutomationTriggerDevice = st.Infer<
|
export type AutomationTriggerDevice = st.Infer<
|
||||||
typeof v.AutomationTriggerDevice
|
typeof v.AutomationTriggerDevice
|
||||||
>;
|
>;
|
||||||
|
export type AutomationTriggerSun = st.Infer<typeof v.AutomationTriggerSun>;
|
||||||
|
export type AutomationTriggerCalendar = st.Infer<
|
||||||
|
typeof v.AutomationTriggerCalendar
|
||||||
|
>;
|
||||||
|
|
||||||
export type ActionTriggerMapping = {
|
export type ActionTriggerMapping = {
|
||||||
homeassistant: AutomationTriggerHA;
|
homeassistant: AutomationTriggerHA;
|
||||||
@@ -39,6 +43,8 @@ export type ActionTriggerMapping = {
|
|||||||
time: AutomationTriggerTime;
|
time: AutomationTriggerTime;
|
||||||
webhook: AutomationTriggerWebhook;
|
webhook: AutomationTriggerWebhook;
|
||||||
zone: AutomationTriggerZone;
|
zone: AutomationTriggerZone;
|
||||||
|
sun: AutomationTriggerSun;
|
||||||
|
calendar: AutomationTriggerCalendar;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TriggerType = keyof ActionTriggerMapping;
|
export type TriggerType = keyof ActionTriggerMapping;
|
||||||
@@ -54,4 +60,6 @@ export type AutomationTrigger =
|
|||||||
| AutomationTriggerTime
|
| AutomationTriggerTime
|
||||||
| AutomationTriggerWebhook
|
| AutomationTriggerWebhook
|
||||||
| AutomationTriggerZone
|
| AutomationTriggerZone
|
||||||
| AutomationTriggerDevice;
|
| AutomationTriggerDevice
|
||||||
|
| AutomationTriggerSun
|
||||||
|
| AutomationTriggerCalendar;
|
||||||
@@ -96,6 +96,20 @@ export const AutomationTriggerZone = st.type({
|
|||||||
event: st.enums(["enter", "leave"]),
|
event: st.enums(["enter", "leave"]),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const AutomationTriggerSun = st.type({
|
||||||
|
...AutomationTriggerBase,
|
||||||
|
platform: st.literal("sun"),
|
||||||
|
event: st.enums(["sunset", "sunrise"]),
|
||||||
|
offset: st.optional(st.string()),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const AutomationTriggerCalendar = st.type({
|
||||||
|
...AutomationTriggerBase,
|
||||||
|
platform: st.literal("calendar"),
|
||||||
|
event: st.enums(["start", "end"]),
|
||||||
|
offset: st.optional(st.string()),
|
||||||
|
});
|
||||||
|
|
||||||
export const AutomationTriggerDevice = st.type({
|
export const AutomationTriggerDevice = st.type({
|
||||||
...AutomationTriggerBase,
|
...AutomationTriggerBase,
|
||||||
..._AutomationDeviceState,
|
..._AutomationDeviceState,
|
||||||
@@ -103,6 +117,7 @@ export const AutomationTriggerDevice = st.type({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const AutomationTrigger = st.union([
|
export const AutomationTrigger = st.union([
|
||||||
|
AutomationTriggerSun,
|
||||||
AutomationTriggerEvent,
|
AutomationTriggerEvent,
|
||||||
AutomationTriggerHA,
|
AutomationTriggerHA,
|
||||||
AutomationTriggerMQTT,
|
AutomationTriggerMQTT,
|
||||||
|
|||||||
@@ -73,6 +73,8 @@ export const getSubTypeList = <T extends AutomationNodeTypes>(nodeType: T) => {
|
|||||||
"zone",
|
"zone",
|
||||||
"event",
|
"event",
|
||||||
"homeassistant",
|
"homeassistant",
|
||||||
|
"sun",
|
||||||
|
"calendar",
|
||||||
"mqtt",
|
"mqtt",
|
||||||
"tag",
|
"tag",
|
||||||
"template",
|
"template",
|
||||||
@@ -161,6 +163,7 @@ export const TriggerValidators: Record<TriggerType, any> = {
|
|||||||
time: v.triggers.AutomationTriggerTime,
|
time: v.triggers.AutomationTriggerTime,
|
||||||
webhook: v.triggers.AutomationTriggerWebhook,
|
webhook: v.triggers.AutomationTriggerWebhook,
|
||||||
zone: v.triggers.AutomationTriggerZone,
|
zone: v.triggers.AutomationTriggerZone,
|
||||||
|
sun: v.triggers.AutomationTriggerSun,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const ActionValidators: Record<ActionType, any> = {
|
export const ActionValidators: Record<ActionType, any> = {
|
||||||
|
|||||||
Reference in new issue
Block a user