From 3c23e6a1c3e459849b3b9801ed2e4026a504f978 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Tue, 24 Jan 2023 05:45:56 -0800 Subject: [PATCH] =?UTF-8?q?Add=20UI=20support=20for=20entities=20in=20nume?= =?UTF-8?q?ric=5Fstate=20above/below=20conditions=20&=E2=80=A6=20(#14966)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes undefined --- .../ha-automation-condition-numeric_state.ts | 145 +++++++++++++++--- .../ha-automation-trigger-numeric_state.ts | 145 +++++++++++++++--- src/translations/en.json | 10 +- 3 files changed, 255 insertions(+), 45 deletions(-) diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-numeric_state.ts b/src/panels/config/automation/condition/types/ha-automation-condition-numeric_state.ts index 9c7647f6b6..07a767437a 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-numeric_state.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-numeric_state.ts @@ -1,7 +1,8 @@ import { html, LitElement } from "lit"; -import { customElement, property } from "lit/decorators"; +import { customElement, property, state } from "lit/decorators"; import memoizeOne from "memoize-one"; import { fireEvent } from "../../../../../common/dom/fire_event"; +import type { LocalizeFunc } from "../../../../../common/translations/localize"; import "../../../../../components/ha-form/ha-form"; import type { SchemaUnion } from "../../../../../components/ha-form/types"; import { NumericStateCondition } from "../../../../../data/automation"; @@ -15,6 +16,10 @@ export default class HaNumericStateCondition extends LitElement { @property({ type: Boolean }) public disabled = false; + @state() private _inputAboveIsEntity?: boolean; + + @state() private _inputBelowIsEntity?: boolean; + public static get defaultConfig() { return { entity_id: "", @@ -22,7 +27,12 @@ export default class HaNumericStateCondition extends LitElement { } private _schema = memoizeOne( - (entityId) => + ( + localize: LocalizeFunc, + entityId, + inputAboveIsEntity?: boolean, + inputBelowIsEntity?: boolean + ) => [ { name: "entity_id", required: true, selector: { entity: {} } }, { @@ -98,27 +108,87 @@ export default class HaNumericStateCondition extends LitElement { }, }, { - name: "above", - selector: { - number: { - mode: "box", - min: Number.MIN_SAFE_INTEGER, - max: Number.MAX_SAFE_INTEGER, - step: 0.1, - }, - }, + name: "mode_above", + type: "select", + required: true, + options: [ + [ + "value", + localize( + "ui.panel.config.automation.editor.conditions.type.numeric_state.type_value" + ), + ], + [ + "input", + localize( + "ui.panel.config.automation.editor.conditions.type.numeric_state.type_input" + ), + ], + ], }, + ...(inputAboveIsEntity + ? ([ + { + name: "above", + selector: { + entity: { domain: ["input_number", "number", "sensor"] }, + }, + }, + ] as const) + : ([ + { + name: "above", + selector: { + number: { + mode: "box", + min: Number.MIN_SAFE_INTEGER, + max: Number.MAX_SAFE_INTEGER, + step: 0.1, + }, + }, + }, + ] as const)), { - name: "below", - selector: { - number: { - mode: "box", - min: Number.MIN_SAFE_INTEGER, - max: Number.MAX_SAFE_INTEGER, - step: 0.1, - }, - }, + name: "mode_below", + type: "select", + required: true, + options: [ + [ + "value", + localize( + "ui.panel.config.automation.editor.conditions.type.numeric_state.type_value" + ), + ], + [ + "input", + localize( + "ui.panel.config.automation.editor.conditions.type.numeric_state.type_input" + ), + ], + ], }, + ...(inputBelowIsEntity + ? ([ + { + name: "below", + selector: { + entity: { domain: ["input_number", "number", "sensor"] }, + }, + }, + ] as const) + : ([ + { + name: "below", + selector: { + number: { + mode: "box", + min: Number.MIN_SAFE_INTEGER, + max: Number.MAX_SAFE_INTEGER, + step: 0.1, + }, + }, + }, + ] as const)), { name: "value_template", selector: { template: {} }, @@ -127,12 +197,36 @@ export default class HaNumericStateCondition extends LitElement { ); public render() { - const schema = this._schema(this.condition.entity_id); + const inputAboveIsEntity = + this._inputAboveIsEntity ?? + (typeof this.condition.above === "string" && + ((this.condition.above as string).startsWith("input_number.") || + (this.condition.above as string).startsWith("number.") || + (this.condition.above as string).startsWith("sensor."))); + const inputBelowIsEntity = + this._inputBelowIsEntity ?? + (typeof this.condition.below === "string" && + ((this.condition.below as string).startsWith("input_number.") || + (this.condition.below as string).startsWith("number.") || + (this.condition.below as string).startsWith("sensor."))); + + const schema = this._schema( + this.hass.localize, + this.condition.entity_id, + inputAboveIsEntity, + inputBelowIsEntity + ); + + const data = { + mode_above: inputAboveIsEntity ? "input" : "value", + mode_below: inputBelowIsEntity ? "input" : "value", + ...this.condition, + }; return html` + ( + localize: LocalizeFunc, + entityId, + inputAboveIsEntity?: boolean, + inputBelowIsEntity?: boolean + ) => [ { name: "entity_id", required: true, selector: { entity: {} } }, { @@ -94,27 +104,87 @@ export class HaNumericStateTrigger extends LitElement { }, }, { - name: "above", - selector: { - number: { - mode: "box", - min: Number.MIN_SAFE_INTEGER, - max: Number.MAX_SAFE_INTEGER, - step: 0.1, - }, - }, + name: "mode_above", + type: "select", + required: true, + options: [ + [ + "value", + localize( + "ui.panel.config.automation.editor.triggers.type.numeric_state.type_value" + ), + ], + [ + "input", + localize( + "ui.panel.config.automation.editor.triggers.type.numeric_state.type_input" + ), + ], + ], }, + ...(inputAboveIsEntity + ? ([ + { + name: "above", + selector: { + entity: { domain: ["input_number", "number", "sensor"] }, + }, + }, + ] as const) + : ([ + { + name: "above", + selector: { + number: { + mode: "box", + min: Number.MIN_SAFE_INTEGER, + max: Number.MAX_SAFE_INTEGER, + step: 0.1, + }, + }, + }, + ] as const)), { - name: "below", - selector: { - number: { - mode: "box", - min: Number.MIN_SAFE_INTEGER, - max: Number.MAX_SAFE_INTEGER, - step: 0.1, - }, - }, + name: "mode_below", + type: "select", + required: true, + options: [ + [ + "value", + localize( + "ui.panel.config.automation.editor.triggers.type.numeric_state.type_value" + ), + ], + [ + "input", + localize( + "ui.panel.config.automation.editor.triggers.type.numeric_state.type_input" + ), + ], + ], }, + ...(inputBelowIsEntity + ? ([ + { + name: "below", + selector: { + entity: { domain: ["input_number", "number", "sensor"] }, + }, + }, + ] as const) + : ([ + { + name: "below", + selector: { + number: { + mode: "box", + min: Number.MIN_SAFE_INTEGER, + max: Number.MAX_SAFE_INTEGER, + step: 0.1, + }, + }, + }, + ] as const)), { name: "value_template", selector: { template: {} }, @@ -146,8 +216,32 @@ export class HaNumericStateTrigger extends LitElement { public render() { const trgFor = createDurationData(this.trigger.for); - const data = { ...this.trigger, for: trgFor }; - const schema = this._schema(this.trigger.entity_id); + const inputAboveIsEntity = + this._inputAboveIsEntity ?? + (typeof this.trigger.above === "string" && + ((this.trigger.above as string).startsWith("input_number.") || + (this.trigger.above as string).startsWith("number.") || + (this.trigger.above as string).startsWith("sensor."))); + const inputBelowIsEntity = + this._inputBelowIsEntity ?? + (typeof this.trigger.below === "string" && + ((this.trigger.below as string).startsWith("input_number.") || + (this.trigger.below as string).startsWith("number.") || + (this.trigger.below as string).startsWith("sensor."))); + + const schema = this._schema( + this.hass.localize, + this.trigger.entity_id, + inputAboveIsEntity, + inputBelowIsEntity + ); + + const data = { + mode_above: inputAboveIsEntity ? "input" : "value", + mode_below: inputBelowIsEntity ? "input" : "value", + ...this.trigger, + for: trgFor, + }; return html`