Use filter_entity and filter_attribute context (#15395)

This commit is contained in:
Paul Bottein 2023-02-20 14:21:02 +01:00 committed by GitHub
parent 6986c1c8b7
commit cf377558ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 65 deletions

View File

@ -29,7 +29,6 @@ export default class HaNumericStateCondition extends LitElement {
private _schema = memoizeOne( private _schema = memoizeOne(
( (
localize: LocalizeFunc, localize: LocalizeFunc,
entityId,
inputAboveIsEntity?: boolean, inputAboveIsEntity?: boolean,
inputBelowIsEntity?: boolean inputBelowIsEntity?: boolean
) => ) =>
@ -39,7 +38,6 @@ export default class HaNumericStateCondition extends LitElement {
name: "attribute", name: "attribute",
selector: { selector: {
attribute: { attribute: {
entity_id: entityId,
hide_attributes: [ hide_attributes: [
"access_token", "access_token",
"auto_update", "auto_update",
@ -106,6 +104,9 @@ export default class HaNumericStateCondition extends LitElement {
], ],
}, },
}, },
context: {
filter_entity: "entity_id",
},
}, },
{ {
name: "mode_above", name: "mode_above",
@ -212,7 +213,6 @@ export default class HaNumericStateCondition extends LitElement {
const schema = this._schema( const schema = this._schema(
this.hass.localize, this.hass.localize,
this.condition.entity_id,
inputAboveIsEntity, inputAboveIsEntity,
inputBelowIsEntity inputBelowIsEntity
); );

View File

@ -1,6 +1,5 @@
import { html, LitElement, PropertyValues } from "lit"; import { html, LitElement, PropertyValues } from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property } from "lit/decorators";
import memoizeOne from "memoize-one";
import { import {
assert, assert,
boolean, boolean,
@ -29,6 +28,54 @@ const stateConditionStruct = object({
enabled: optional(boolean()), enabled: optional(boolean()),
}); });
const SCHEMA = [
{ name: "entity_id", required: true, selector: { entity: {} } },
{
name: "attribute",
selector: {
attribute: {
hide_attributes: [
"access_token",
"available_modes",
"color_modes",
"editable",
"effect_list",
"entity_picture",
"fan_modes",
"fan_speed_list",
"forecast",
"friendly_name",
"hvac_modes",
"icon",
"operation_list",
"options",
"preset_modes",
"sound_mode_list",
"source_list",
"state_class",
"swing_modes",
"token",
],
},
},
context: {
filter_entity: "entity_id",
},
},
{
name: "state",
required: true,
selector: {
state: {},
},
context: {
filter_entity: "entity_id",
filter_attribute: "attribute",
},
},
{ name: "for", selector: { duration: {} } },
] as const;
@customElement("ha-automation-condition-state") @customElement("ha-automation-condition-state")
export class HaStateCondition extends LitElement implements ConditionElement { export class HaStateCondition extends LitElement implements ConditionElement {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@ -41,51 +88,6 @@ export class HaStateCondition extends LitElement implements ConditionElement {
return { entity_id: "", state: "" }; return { entity_id: "", state: "" };
} }
private _schema = memoizeOne(
(entityId, attribute) =>
[
{ name: "entity_id", required: true, selector: { entity: {} } },
{
name: "attribute",
selector: {
attribute: {
entity_id: entityId,
hide_attributes: [
"access_token",
"available_modes",
"color_modes",
"editable",
"effect_list",
"entity_picture",
"fan_modes",
"fan_speed_list",
"forecast",
"friendly_name",
"hvac_modes",
"icon",
"operation_list",
"options",
"preset_modes",
"sound_mode_list",
"source_list",
"state_class",
"swing_modes",
"token",
],
},
},
},
{
name: "state",
required: true,
selector: {
state: { entity_id: entityId, attribute: attribute },
},
},
{ name: "for", selector: { duration: {} } },
] as const
);
public shouldUpdate(changedProperties: PropertyValues) { public shouldUpdate(changedProperties: PropertyValues) {
if (changedProperties.has("condition")) { if (changedProperties.has("condition")) {
try { try {
@ -101,16 +103,12 @@ export class HaStateCondition extends LitElement implements ConditionElement {
protected render() { protected render() {
const trgFor = createDurationData(this.condition.for); const trgFor = createDurationData(this.condition.for);
const data = { ...this.condition, for: trgFor }; const data = { ...this.condition, for: trgFor };
const schema = this._schema(
this.condition.entity_id,
this.condition.attribute
);
return html` return html`
<ha-form <ha-form
.hass=${this.hass} .hass=${this.hass}
.data=${data} .data=${data}
.schema=${schema} .schema=${SCHEMA}
.disabled=${this.disabled} .disabled=${this.disabled}
@value-changed=${this._valueChanged} @value-changed=${this._valueChanged}
.computeLabel=${this._computeLabelCallback} .computeLabel=${this._computeLabelCallback}
@ -138,7 +136,7 @@ export class HaStateCondition extends LitElement implements ConditionElement {
} }
private _computeLabelCallback = ( private _computeLabelCallback = (
schema: SchemaUnion<ReturnType<typeof this._schema>> schema: SchemaUnion<typeof SCHEMA>
): string => { ): string => {
switch (schema.name) { switch (schema.name) {
case "entity_id": case "entity_id":

View File

@ -25,7 +25,6 @@ export class HaNumericStateTrigger extends LitElement {
private _schema = memoizeOne( private _schema = memoizeOne(
( (
localize: LocalizeFunc, localize: LocalizeFunc,
entityId,
inputAboveIsEntity?: boolean, inputAboveIsEntity?: boolean,
inputBelowIsEntity?: boolean inputBelowIsEntity?: boolean
) => ) =>
@ -35,7 +34,6 @@ export class HaNumericStateTrigger extends LitElement {
name: "attribute", name: "attribute",
selector: { selector: {
attribute: { attribute: {
entity_id: entityId,
hide_attributes: [ hide_attributes: [
"access_token", "access_token",
"auto_update", "auto_update",
@ -102,6 +100,9 @@ export class HaNumericStateTrigger extends LitElement {
], ],
}, },
}, },
context: {
filter_entity: "entity_id",
},
}, },
{ {
name: "mode_above", name: "mode_above",
@ -231,7 +232,6 @@ export class HaNumericStateTrigger extends LitElement {
const schema = this._schema( const schema = this._schema(
this.hass.localize, this.hass.localize,
this.trigger.entity_id,
inputAboveIsEntity, inputAboveIsEntity,
inputBelowIsEntity inputBelowIsEntity
); );

View File

@ -67,7 +67,7 @@ export class HuiWeatherForecastCardEditor
} }
private _schema = memoizeOne( private _schema = memoizeOne(
(entity: string, localize: LocalizeFunc, hasForecast?: boolean) => (localize: LocalizeFunc, hasForecast?: boolean) =>
[ [
{ {
name: "entity", name: "entity",
@ -81,7 +81,8 @@ export class HuiWeatherForecastCardEditor
schema: [ schema: [
{ {
name: "secondary_info_attribute", name: "secondary_info_attribute",
selector: { attribute: { entity_id: entity } }, selector: { attribute: {} },
context: { filter_entity: "entity" },
}, },
{ name: "theme", selector: { theme: {} } }, { name: "theme", selector: { theme: {} } },
], ],
@ -125,11 +126,7 @@ export class HuiWeatherForecastCardEditor
return html``; return html``;
} }
const schema = this._schema( const schema = this._schema(this.hass.localize, this._has_forecast);
this._config.entity,
this.hass.localize,
this._has_forecast
);
const data: WeatherForecastCardConfig = { const data: WeatherForecastCardConfig = {
show_current: true, show_current: true,