From 3e188d1f87125a19d36b53a4713feceb06ddfee3 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 25 Apr 2022 01:00:28 -0700 Subject: [PATCH] Add shorthand condition to the gallery (#12400) --- .../src/pages/automation/editor-condition.ts | 12 +++++++++-- src/data/automation.ts | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/gallery/src/pages/automation/editor-condition.ts b/gallery/src/pages/automation/editor-condition.ts index 77e42e6171..8f8ee17604 100644 --- a/gallery/src/pages/automation/editor-condition.ts +++ b/gallery/src/pages/automation/editor-condition.ts @@ -8,7 +8,7 @@ import { mockEntityRegistry } from "../../../../demo/src/stubs/entity_registry"; import { mockDeviceRegistry } from "../../../../demo/src/stubs/device_registry"; import { mockAreaRegistry } from "../../../../demo/src/stubs/area_registry"; import { mockHassioSupervisor } from "../../../../demo/src/stubs/hassio_supervisor"; -import type { Condition } from "../../../../src/data/automation"; +import type { ConditionWithShorthand } from "../../../../src/data/automation"; import "../../../../src/panels/config/automation/condition/ha-automation-condition"; import { HaDeviceCondition } from "../../../../src/panels/config/automation/condition/types/ha-automation-condition-device"; import { HaLogicalCondition } from "../../../../src/panels/config/automation/condition/types/ha-automation-condition-logical"; @@ -20,7 +20,7 @@ import { HaTimeCondition } from "../../../../src/panels/config/automation/condit import { HaTriggerCondition } from "../../../../src/panels/config/automation/condition/types/ha-automation-condition-trigger"; import { HaZoneCondition } from "../../../../src/panels/config/automation/condition/types/ha-automation-condition-zone"; -const SCHEMAS: { name: string; conditions: Condition[] }[] = [ +const SCHEMAS: { name: string; conditions: ConditionWithShorthand[] }[] = [ { name: "State", conditions: [{ condition: "state", ...HaStateCondition.defaultConfig }], @@ -69,6 +69,14 @@ const SCHEMAS: { name: string; conditions: Condition[] }[] = [ name: "Trigger", conditions: [{ condition: "trigger", ...HaTriggerCondition.defaultConfig }], }, + { + name: "Shorthand", + conditions: [ + { and: HaLogicalCondition.defaultConfig.conditions }, + { or: HaLogicalCondition.defaultConfig.conditions }, + { not: HaLogicalCondition.defaultConfig.conditions }, + ], + }, ]; @customElement("demo-automation-editor-condition") diff --git a/src/data/automation.ts b/src/data/automation.ts index a73e0b1fb5..4e8694b6fa 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -233,6 +233,20 @@ export interface TriggerCondition extends BaseCondition { id: string; } +type ShorthandBaseCondition = Omit; + +export interface ShorthandAndCondition extends ShorthandBaseCondition { + and: Condition[]; +} + +export interface ShorthandOrCondition extends ShorthandBaseCondition { + or: Condition[]; +} + +export interface ShorthandNotCondition extends ShorthandBaseCondition { + not: Condition[]; +} + export type Condition = | StateCondition | NumericStateCondition @@ -244,6 +258,12 @@ export type Condition = | LogicalCondition | TriggerCondition; +export type ConditionWithShorthand = + | Condition + | ShorthandAndCondition + | ShorthandOrCondition + | ShorthandNotCondition; + export const triggerAutomationActions = ( hass: HomeAssistant, entityId: string