mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-21 08:16:36 +00:00
Evaluate condition shorthands in editors (#12473)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
7ca379e0a1
commit
b8c55f2f65
@ -235,6 +235,10 @@ export interface TriggerCondition extends BaseCondition {
|
|||||||
|
|
||||||
type ShorthandBaseCondition = Omit<BaseCondition, "condition">;
|
type ShorthandBaseCondition = Omit<BaseCondition, "condition">;
|
||||||
|
|
||||||
|
export interface ShorthandAndConditionList extends ShorthandBaseCondition {
|
||||||
|
condition: Condition[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface ShorthandAndCondition extends ShorthandBaseCondition {
|
export interface ShorthandAndCondition extends ShorthandBaseCondition {
|
||||||
and: Condition[];
|
and: Condition[];
|
||||||
}
|
}
|
||||||
@ -260,10 +264,33 @@ export type Condition =
|
|||||||
|
|
||||||
export type ConditionWithShorthand =
|
export type ConditionWithShorthand =
|
||||||
| Condition
|
| Condition
|
||||||
|
| ShorthandAndConditionList
|
||||||
| ShorthandAndCondition
|
| ShorthandAndCondition
|
||||||
| ShorthandOrCondition
|
| ShorthandOrCondition
|
||||||
| ShorthandNotCondition;
|
| ShorthandNotCondition;
|
||||||
|
|
||||||
|
export const expandConditionWithShorthand = (
|
||||||
|
cond: ConditionWithShorthand
|
||||||
|
): Condition => {
|
||||||
|
if ("condition" in cond && Array.isArray(cond.condition)) {
|
||||||
|
return {
|
||||||
|
condition: "and",
|
||||||
|
conditions: cond.condition,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const condition of ["and", "or", "not"]) {
|
||||||
|
if (condition in cond) {
|
||||||
|
return {
|
||||||
|
condition,
|
||||||
|
conditions: cond[condition],
|
||||||
|
} as Condition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cond as Condition;
|
||||||
|
};
|
||||||
|
|
||||||
export const triggerAutomationActions = (
|
export const triggerAutomationActions = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entityId: string
|
entityId: string
|
||||||
|
@ -10,6 +10,7 @@ import "../../../../components/ha-select";
|
|||||||
import type { HaSelect } from "../../../../components/ha-select";
|
import type { HaSelect } from "../../../../components/ha-select";
|
||||||
import "../../../../components/ha-yaml-editor";
|
import "../../../../components/ha-yaml-editor";
|
||||||
import type { Condition } from "../../../../data/automation";
|
import type { Condition } from "../../../../data/automation";
|
||||||
|
import { expandConditionWithShorthand } from "../../../../data/automation";
|
||||||
import { haStyle } from "../../../../resources/styles";
|
import { haStyle } from "../../../../resources/styles";
|
||||||
import type { HomeAssistant } from "../../../../types";
|
import type { HomeAssistant } from "../../../../types";
|
||||||
import "./types/ha-automation-condition-and";
|
import "./types/ha-automation-condition-and";
|
||||||
@ -42,10 +43,14 @@ const OPTIONS = [
|
|||||||
export default class HaAutomationConditionEditor extends LitElement {
|
export default class HaAutomationConditionEditor extends LitElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
@property() public condition!: Condition;
|
@property() condition!: Condition;
|
||||||
|
|
||||||
@property() public yamlMode = false;
|
@property() public yamlMode = false;
|
||||||
|
|
||||||
|
private _processedCondition = memoizeOne((condition) =>
|
||||||
|
expandConditionWithShorthand(condition)
|
||||||
|
);
|
||||||
|
|
||||||
private _processedTypes = memoizeOne(
|
private _processedTypes = memoizeOne(
|
||||||
(localize: LocalizeFunc): [string, string][] =>
|
(localize: LocalizeFunc): [string, string][] =>
|
||||||
OPTIONS.map(
|
OPTIONS.map(
|
||||||
@ -60,7 +65,8 @@ export default class HaAutomationConditionEditor extends LitElement {
|
|||||||
);
|
);
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
const selected = OPTIONS.indexOf(this.condition.condition);
|
const condition = this._processedCondition(this.condition);
|
||||||
|
const selected = OPTIONS.indexOf(condition.condition);
|
||||||
const yamlMode = this.yamlMode || selected === -1;
|
const yamlMode = this.yamlMode || selected === -1;
|
||||||
return html`
|
return html`
|
||||||
${yamlMode
|
${yamlMode
|
||||||
@ -70,7 +76,7 @@ export default class HaAutomationConditionEditor extends LitElement {
|
|||||||
${this.hass.localize(
|
${this.hass.localize(
|
||||||
"ui.panel.config.automation.editor.conditions.unsupported_condition",
|
"ui.panel.config.automation.editor.conditions.unsupported_condition",
|
||||||
"condition",
|
"condition",
|
||||||
this.condition.condition
|
condition.condition
|
||||||
)}
|
)}
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
@ -90,7 +96,7 @@ export default class HaAutomationConditionEditor extends LitElement {
|
|||||||
.label=${this.hass.localize(
|
.label=${this.hass.localize(
|
||||||
"ui.panel.config.automation.editor.conditions.type_select"
|
"ui.panel.config.automation.editor.conditions.type_select"
|
||||||
)}
|
)}
|
||||||
.value=${this.condition.condition}
|
.value=${condition.condition}
|
||||||
naturalMenuWidth
|
naturalMenuWidth
|
||||||
@selected=${this._typeChanged}
|
@selected=${this._typeChanged}
|
||||||
>
|
>
|
||||||
@ -103,8 +109,8 @@ export default class HaAutomationConditionEditor extends LitElement {
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
${dynamicElement(
|
${dynamicElement(
|
||||||
`ha-automation-condition-${this.condition.condition}`,
|
`ha-automation-condition-${condition.condition}`,
|
||||||
{ hass: this.hass, condition: this.condition }
|
{ hass: this.hass, condition: condition }
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
`}
|
`}
|
||||||
@ -124,7 +130,7 @@ export default class HaAutomationConditionEditor extends LitElement {
|
|||||||
defaultConfig: Omit<Condition, "condition">;
|
defaultConfig: Omit<Condition, "condition">;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type !== this.condition.condition) {
|
if (type !== this._processedCondition(this.condition).condition) {
|
||||||
fireEvent(this, "value-changed", {
|
fireEvent(this, "value-changed", {
|
||||||
value: {
|
value: {
|
||||||
condition: type,
|
condition: type,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user