condition validation

This commit is contained in:
ari committed 2022-05-18 22:37:25 -04:00
1 parent 40690293a8
commit c28796872f
5 files changed
+101 -32

No files matched your search

+8 -30
View File
@@ -1,5 +1,6 @@
import { AutomationTime } from "./common";
import * as st from 'superstruct';
import * as v from "types/validators/conditions";
type AutomationConditionNodeBase<Name extends String, Data extends Object> = {
condition: Name;
alias?: string;
@@ -7,35 +8,12 @@ type AutomationConditionNodeBase<Name extends String, Data extends Object> = {
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<typeof v.NumericCondition>;
export type StateCondition = st.Infer<typeof v.StateCondition>;
export type TemplateCondition = st.Infer<typeof v.TemplateCondition>;
export type TimeCondition = st.Infer<typeof v.TimeCondition>;
export type TriggerCondition = st.Infer<typeof v.TriggerCondition>;
export type ZoneCondition = st.Infer<typeof v.ZoneCondition>;
export type AutomationCondition =
| LogicCondition
| NumericCondition
+4 -1
View File
@@ -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(':')
+71
View File
@@ -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()]),
})
]);
+2 -1
View File
@@ -1,2 +1,3 @@
export * as actions from "./actions"
export * as triggers from "./triggers"
export * as triggers from "./triggers"
export * as conditions from "./conditions"
+16
View File
@@ -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)