removed $smType for conditions and trigger

This commit is contained in:
ari committed 2022-05-09 21:41:49 -04:00
1 parent ebd99c80a3
commit 47bf511bbf
41 files changed
+410 -723

No files matched your search

@@ -5,12 +5,9 @@ import { OptionManager } from "./OptionManager";
export const ConditionLogicState: OptionManager<AutomationCondition> = {
defaultState: () => ({
$smType: 'condition',
condition: 'and',
condition_data: {
alias: "",
conditions: [],
}
alias: "",
conditions: [],
}),
isReady: _ => true,
renderOptionList: (state, setState) => <ConditionEditor
@@ -1,9 +1,9 @@
import { AutomationAction } from 'types/automations/actions';
import { AutomationCondition } from 'types/automations/conditions';
export interface OptionManager<T> {
defaultState(): T ;
defaultState(): T;
isReady(s: T): boolean;
renderOptionList(s: T, set: (s:T) => void): JSX.Element;
renderOptionList(s: T, set: (s: T) => void): JSX.Element;
}
@@ -11,24 +11,21 @@ export const updateConditionData = <T extends AutomationCondition>(
setState: (s: T) => void
) => (
state: T,
data: Partial<T['condition_data']>,
) => setState({
...state,
condition_data: {
...state.condition_data,
data: Partial<T>,
) => setState({
...state,
...data,
},
})
})
export const updateActionData = <T extends AutomationAction>(
state: T,
setState: (s: T) => void
) => (
data: Partial<T['action_data']>,
) => setState({
...state,
) => setState({
...state,
action_data: {
...state.action_data,
...data,
},
})
...state.action_data,
...data,
},
})
@@ -5,7 +5,6 @@ import InputText from 'components/Inputs/InputText';
export const TriggerDevice: OptionManager<AutomationTriggerDevice> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": 'device',
"domain": "",
@@ -6,7 +6,6 @@ import InputYaml from "components/Inputs/InputYaml";
export const TriggerEventState: OptionManager<AutomationTriggerEvent> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": "event",
"event_type": "",
@@ -1,11 +1,10 @@
import { OptionManager } from './OptionManager';
import { AutomationTriggerHA } from 'types/automations/triggers';
import InputList from "components/Inputs/InputList";
import { InputList } from "components/Inputs/InputList";
export const TriggerHAState: OptionManager<AutomationTriggerHA> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "HA Starts",
"platform": "homeassistant",
"event": "start",
@@ -5,7 +5,6 @@ import InputText from 'components/Inputs/InputText';
export const TriggerMQTTState: OptionManager<AutomationTriggerMQTT> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": "mqtt",
"payload": "",
@@ -9,7 +9,6 @@ import InputTime from 'components/Inputs/InputTime';
export const TriggerNumericState: OptionManager<AutomationTriggerNumericState> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": 'numeric_state',
"entity_id": "",
@@ -7,7 +7,6 @@ import InputTime from 'components/Inputs/InputTime';
export const TriggerState: OptionManager<AutomationTriggerState> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": 'state',
"entity_id": "",
@@ -6,7 +6,6 @@ import InputTime from 'components/Inputs/InputTime';
export const TriggerTemplate: OptionManager<AutomationTriggerTemplate> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": 'template',
"value_template": "",
@@ -5,7 +5,6 @@ import InputTime from 'components/Inputs/InputTime';
export const TriggerTime: OptionManager<AutomationTriggerTime> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": 'time',
"at": {},
@@ -5,7 +5,6 @@ import InputText from 'components/Inputs/InputText';
export const TriggerTimePattern: OptionManager<AutomationTriggerTimePattern> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": 'time_pattern',
}),
@@ -5,7 +5,6 @@ import InputText from 'components/Inputs/InputText';
export const TriggerWebhook: OptionManager<AutomationTriggerWebhook> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": 'webhook',
"webhook_id": "",
@@ -1,13 +1,12 @@
import { OptionManager } from './OptionManager';
import { AutomationTriggerZone } from 'types/automations/triggers';
import InputText from 'components/Inputs/InputText';
import InputList from 'components/Inputs/InputList';
import { InputList } from 'components/Inputs/InputList';
import { InputEntity } from 'components/Inputs/InputTextBubble';
export const TriggerZone: OptionManager<AutomationTriggerZone> = {
defaultState: () => ({
$smType: 'trigger',
"alias": "",
"platform": 'zone',
"zone": "",
@@ -2,6 +2,10 @@ import { useEffect, useState } from 'react';
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
import { AutomationNodeSubtype } from 'types/automations';
import { getOptionManager } from './getOptionManager';
import { getNodeType, getSubTypeList } from 'utils/automations';
import { AutomationCondition } from 'types/automations/conditions';
import { AutomationAction } from 'types/automations/actions';
import { AutomationTrigger } from 'types/automations/triggers';
export const useEditorNodeState = (node: AutomationNode) => {
@@ -12,12 +16,13 @@ export const useEditorNodeState = (node: AutomationNode) => {
useEffect(() => {
setAllState({ node, isModified: false })
}, [node])
const setState = (node: AutomationNode) => setAllState(({ node, isModified: true }))
const nodeType: AutomationNodeTypes = state.$smType ?? 'trigger';
const setState = (node: AutomationNode) => setAllState(({ node, isModified: true }));
const originalNodeType = getNodeType(node);
const nodeType = getNodeType(state);
const subType: AutomationNodeSubtype =
state.$smType === 'action' ? state.action :
state.$smType === 'condition' ? state.condition :
state.platform;
nodeType === 'action' ? (state as AutomationAction).action :
nodeType === 'condition' ? (state as AutomationCondition).condition :
(state as AutomationTrigger).platform;
const subTypes = getSubTypeList(nodeType)
const optionManager = getOptionManager(nodeType, subType as any);
return {
@@ -37,24 +42,22 @@ export const useEditorNodeState = (node: AutomationNode) => {
return optionManager.isReady(state);
},
setNodeType(newType: AutomationNodeTypes) {
if (newType === (node.$smType ?? 'trigger')) {
if (node.$smType === 'action') {
if (newType === originalNodeType) {
if (originalNodeType === 'action') {
setState({
...state,
$smType: 'action',
action: node.action,
action: (node as any).action,
} as any)
} else if (node.$smType === 'condition') {
} else if (originalNodeType === 'condition') {
setState({
...state,
$smType: 'condition',
condition: node.condition
condition: (node as any).condition
} as any)
} else {
setState({
...state,
$smType: undefined,
platform: node.platform
platform: (node as any).platform
} as any)
}
} else {
@@ -66,23 +69,23 @@ export const useEditorNodeState = (node: AutomationNode) => {
}
},
setSubType(newSubType: AutomationNodeSubtype<typeof nodeType>) {
if (state.$smType === 'action') {
if (nodeType === 'action') {
setState({
...state,
action: newSubType,
...getOptionManager(state.$smType, newSubType as any).defaultState()
...getOptionManager(nodeType, newSubType as any).defaultState()
} as any)
} else if (state.$smType === 'condition') {
} else if (nodeType === 'condition') {
setState({
...state,
condition: newSubType,
...getOptionManager(state.$smType, newSubType as any).defaultState()
...getOptionManager(nodeType, newSubType as any).defaultState()
} as any)
} else {
setState({
...state,
platform: newSubType,
...getOptionManager(state.$smType, newSubType as any).defaultState()
...getOptionManager(nodeType, newSubType as any).defaultState()
} as any)
}
}
@@ -90,44 +93,3 @@ export const useEditorNodeState = (node: AutomationNode) => {
}
const getSubTypeList = <T extends AutomationNodeTypes>(nodeType: T): AutomationNodeSubtype<T>[] => {
switch (nodeType) {
case 'action':
return [
"service",
"repeat",
"wait",
"device",
"choose",
] as any
case "condition":
return [
'or',
'and',
'not',
'numeric_state',
'state',
'template',
'time',
'trigger',
'zone',
] as any
case 'trigger':
return [
'event',
'homeassistant',
'mqtt',
'numeric_state',
'state',
'tag',
'template',
'time',
'time_pattern',
'webhook',
'zone',
'device',
] as any
default:
return [];
}
}
@@ -40,13 +40,13 @@ const triggerTypes = [
export const getSubTypeList = <T extends AutomationNodeTypes>(
nodeType: T
): Array<AutomationNodeSubtype<T>| 'condition'> => {
): Array<AutomationNodeSubtype<T> | 'condition'> => {
if (nodeType === 'action') {
return actionTypes as any;
} else if (nodeType === 'condition') {
return conditionTypes as any;
} else if (nodeType === 'trigger') {
return triggerTypes as any;
return triggerTypes as any;
}
return [];
}
@@ -54,11 +54,13 @@ export const getSubTypeList = <T extends AutomationNodeTypes>(
export const getNodeSubType = <T extends AutomationNodeTypes>(
node: AutomationNode<T>
): AutomationNodeSubtype<T> => {
if (node.$smType === 'action') {
if ('$smType' in node && node.$smType === 'action') {
return node.action as any;
} else if(node.$smType === 'condition') {
} else if ('condition' in node) {
return node.condition as any;
} else {
} else if ('platform' in node) {
return node.platform as any;
} else {
throw new Error("Invalid node type!")
}
}
@@ -41,30 +41,21 @@ export const Condition = Action.bind({})
Condition.args = {
...Condition.args,
node: {
"$smType": "condition",
"condition": "and",
"condition_data": {
"conditions": [
{
"$smType": "condition",
"condition": "numeric_state",
"condition_data": {
"entity_id": [
"sensor.kitchen_humidity"
],
"conditions": [],
"above": "40"
}
},
{
"$smType": "condition",
"condition": "template",
"condition_data": {
"value_template": "states('switch.kitchen') == 'on'"
}
}
]
}
"conditions": [
{
"condition": "numeric_state",
"entity_id": [
"sensor.kitchen_humidity"
],
"conditions": [],
"above": "40"
},
{
"condition": "template",
"value_template": "states('switch.kitchen') == 'on'"
}
]
} as AutomationCondition,
}
+1 -1
View File
@@ -3,7 +3,7 @@ import "./index.css";
import { FC } from "react";
import { AutomationNode, AutomationNodeMapping } from 'types/automations/index';
import { useEditorNodeState } from "./OptionManager";
import InputList from "components/Inputs/InputList";
import { InputList } from "components/Inputs/InputList";
import { Button } from "components/Inputs/Button";
export interface Props {