Add UI support for entities in numeric_state above/below conditions &… (#14966)

fixes undefined
This commit is contained in:
karwosts 2023-01-24 05:45:56 -08:00 committed by GitHub
parent 10369ff952
commit 3c23e6a1c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 255 additions and 45 deletions

View File

@ -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`
<ha-form
.hass=${this.hass}
.data=${this.condition}
.data=${data}
.schema=${schema}
.disabled=${this.disabled}
@value-changed=${this._valueChanged}
@ -144,6 +238,13 @@ export default class HaNumericStateCondition extends LitElement {
private _valueChanged(ev: CustomEvent): void {
ev.stopPropagation();
const newTrigger = ev.detail.value;
this._inputAboveIsEntity = newTrigger.mode_above === "input";
this._inputBelowIsEntity = newTrigger.mode_below === "input";
delete newTrigger.mode_above;
delete newTrigger.mode_below;
fireEvent(this, "value-changed", { value: newTrigger });
}

View File

@ -1,9 +1,10 @@
import { html, LitElement, PropertyValues } from "lit";
import { customElement, property } from "lit/decorators";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { createDurationData } from "../../../../../common/datetime/create_duration_data";
import { fireEvent } from "../../../../../common/dom/fire_event";
import { hasTemplate } from "../../../../../common/string/has-template";
import type { LocalizeFunc } from "../../../../../common/translations/localize";
import "../../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../../components/ha-form/types";
import type { NumericStateTrigger } from "../../../../../data/automation";
@ -17,8 +18,17 @@ export class HaNumericStateTrigger extends LitElement {
@property({ type: Boolean }) public disabled = false;
@state() private _inputAboveIsEntity?: boolean;
@state() private _inputBelowIsEntity?: boolean;
private _schema = memoizeOne(
(entityId) =>
(
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`
<ha-form
@ -164,6 +258,13 @@ export class HaNumericStateTrigger extends LitElement {
private _valueChanged(ev: CustomEvent): void {
ev.stopPropagation();
const newTrigger = ev.detail.value;
this._inputAboveIsEntity = newTrigger.mode_above === "input";
this._inputBelowIsEntity = newTrigger.mode_below === "input";
delete newTrigger.mode_above;
delete newTrigger.mode_below;
fireEvent(this, "value-changed", { value: newTrigger });
}

View File

@ -2066,7 +2066,11 @@
"label": "Numeric state",
"above": "Above",
"below": "Below",
"value_template": "Value template"
"mode_above": "Above mode",
"mode_below": "Below mode",
"value_template": "Value template",
"type_value": "Fixed number",
"type_input": "Numeric value of another entity"
},
"sun": {
"label": "Sun",
@ -2149,9 +2153,13 @@
"label": "Not"
},
"numeric_state": {
"type_value": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::type_value%]",
"type_input": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::type_input%]",
"label": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::label%]",
"above": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::above%]",
"below": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::below%]",
"mode_above": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::mode_above%]",
"mode_below": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::mode_below%]",
"value_template": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::value_template%]"
},
"or": {