From c28796872fb58757284a548dd584d6efd5efaf6f Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 18 May 2022 22:37:25 -0400 Subject: [PATCH] condition validation --- webapp/src/types/automations/conditions.ts | 38 +++--------- webapp/src/types/validators/common.ts | 5 +- webapp/src/types/validators/conditions.ts | 71 ++++++++++++++++++++++ webapp/src/types/validators/index.ts | 3 +- webapp/src/utils/automations.ts | 16 +++++ 5 files changed, 101 insertions(+), 32 deletions(-) create mode 100644 webapp/src/types/validators/conditions.ts diff --git a/webapp/src/types/automations/conditions.ts b/webapp/src/types/automations/conditions.ts index 111637a..5fa00bd 100644 --- a/webapp/src/types/automations/conditions.ts +++ b/webapp/src/types/automations/conditions.ts @@ -1,5 +1,6 @@ import { AutomationTime } from "./common"; - +import * as st from 'superstruct'; +import * as v from "types/validators/conditions"; type AutomationConditionNodeBase = { condition: Name; alias?: string; @@ -7,35 +8,12 @@ type AutomationConditionNodeBase = { export type LogicCondition = AutomationConditionNodeBase<'and' | 'or' | 'not', { conditions: AutomationCondition[]; }>; -export type NumericCondition = AutomationConditionNodeBase<'numeric_state', { - entity_id: string | string[]; - above?: string; - below?: string; - attribute?: string; - value_template?: string; -}>; -export type StateCondition = AutomationConditionNodeBase<'state', { - entity_id: string | string[]; - state: string | string[]; - for?: AutomationTime; - attribute?: string; -}>; -export type TemplateCondition = AutomationConditionNodeBase<'template', { - value_template: string; -}>; -export type TimeCondition = AutomationConditionNodeBase<'time', { - after?: AutomationTime; - before?: AutomationTime; - weekday?: Array<'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun'>; -}>; -export type TriggerCondition = AutomationConditionNodeBase<'trigger', { - id: string; -}>; -export type ZoneCondition = AutomationConditionNodeBase<'zone', { - zone: string; - entity_id: string | string[]; - state: string | string[]; -}>; +export type NumericCondition = st.Infer; +export type StateCondition = st.Infer; +export type TemplateCondition = st.Infer; +export type TimeCondition = st.Infer; +export type TriggerCondition = st.Infer; +export type ZoneCondition = st.Infer; export type AutomationCondition = | LogicCondition | NumericCondition diff --git a/webapp/src/types/validators/common.ts b/webapp/src/types/validators/common.ts index 1314319..192235f 100644 --- a/webapp/src/types/validators/common.ts +++ b/webapp/src/types/validators/common.ts @@ -1,6 +1,9 @@ import * as st from 'superstruct' - +export const EntityId = st.union([ + st.string(), + st.array(st.string()), +]) export const AutomationTimeString = st.refine(st.string(), 'TimeString', (v, c) => { // https://www.home-assistant.io/docs/automation/trigger/#time-string const bits: string[] = v.split(':') diff --git a/webapp/src/types/validators/conditions.ts b/webapp/src/types/validators/conditions.ts new file mode 100644 index 0000000..5d2c4a5 --- /dev/null +++ b/webapp/src/types/validators/conditions.ts @@ -0,0 +1,71 @@ +import * as st from 'superstruct' +import { AutomationTime, EntityId } from './common'; + + +export const AutomationConditionNodeBase = st.type({ + alias: st.optional(st.string()), +}); +export const LogicCondition = st.intersection([ + AutomationConditionNodeBase, + st.type({ + condition: st.enums(['and', 'or', 'not']), + conditions: st.array(st.object()) + }) +]) +export const NumericCondition = st.intersection([ + AutomationConditionNodeBase, + st.type({ + condition: st.literal('numeric_state'), + entity_id: EntityId, + above: st.optional(st.string()), + below: st.optional(st.string()), + attribute: st.optional(st.string()), + value_template: st.optional(st.string()), + }) +]); +export const StateCondition = st.intersection([ + AutomationConditionNodeBase, + st.type({ + condition: st.literal('state'), + entity_id: EntityId, + state: st.union([st.array(st.string()), st.string()]), + for: st.optional(AutomationTime), + attribute: st.optional(st.string()), + }) +]); +export const TemplateCondition = st.intersection([ + AutomationConditionNodeBase, + st.type({ + condition: st.literal('template'), + value_template: st.string(), + }) +]); +export const TriggerCondition = st.intersection([ + AutomationConditionNodeBase, + st.type({ + condition: st.literal('trigger'), + id: st.string(), + }) +]); +export const TimeCondition = st.intersection([ + AutomationConditionNodeBase, + st.type({ + condition: st.literal('time'), + after: st.optional(AutomationTime), + before: st.optional(AutomationTime), + weekday: st.optional(st.enums([ + 'mon', 'tue', 'wed', + 'thu', 'fri', 'sat', + 'sun', + ])) + }) +]); +export const ZoneCondition = st.intersection([ + AutomationConditionNodeBase, + st.type({ + condition: st.literal('zone'), + zone: st.string(), + entity_id: EntityId, + state: st.union([st.array(st.string()), st.string()]), + }) +]); \ No newline at end of file diff --git a/webapp/src/types/validators/index.ts b/webapp/src/types/validators/index.ts index c10c473..3c0e79c 100644 --- a/webapp/src/types/validators/index.ts +++ b/webapp/src/types/validators/index.ts @@ -1,2 +1,3 @@ export * as actions from "./actions" -export * as triggers from "./triggers" \ No newline at end of file +export * as triggers from "./triggers" +export * as conditions from "./conditions" \ No newline at end of file diff --git a/webapp/src/utils/automations.ts b/webapp/src/utils/automations.ts index 053d368..6697949 100644 --- a/webapp/src/utils/automations.ts +++ b/webapp/src/utils/automations.ts @@ -152,6 +152,22 @@ export const validateNode = (node: AutomationNode): MiniFailure[] | null => { } else if (nodeSubType === 'device') { return getFailures(node, v.triggers.AutomationTriggerDevice) } + } else if (nodeType === 'condition') { + if (['and', 'or', 'not'].includes(nodeSubType)) { + return getFailures(node, v.conditions.LogicCondition) + } else if (nodeSubType === 'numeric_state') { + return getFailures(node, v.conditions.NumericCondition) + } else if (nodeSubType === 'state') { + return getFailures(node, v.conditions.StateCondition) + } else if (nodeSubType === 'template') { + return getFailures(node, v.conditions.TemplateCondition) + } else if (nodeSubType === 'trigger') { + return getFailures(node, v.conditions.TriggerCondition) + } else if (nodeSubType === 'time') { + return getFailures(node, v.conditions.TimeCondition) + } else if (nodeSubType === 'zone') { + return getFailures(node, v.conditions.ZoneCondition) + } } } catch (err) { console.warn(err)