From cf377558ae8f4f801eb3e5616a9ece18f7e582cf Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Mon, 20 Feb 2023 14:21:02 +0100 Subject: [PATCH] Use filter_entity and filter_attribute context (#15395) --- .../ha-automation-condition-numeric_state.ts | 6 +- .../types/ha-automation-condition-state.ts | 102 +++++++++--------- .../ha-automation-trigger-numeric_state.ts | 6 +- .../hui-weather-forecast-card-editor.ts | 11 +- 4 files changed, 60 insertions(+), 65 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 07a767437a..0a9003c72e 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 @@ -29,7 +29,6 @@ export default class HaNumericStateCondition extends LitElement { private _schema = memoizeOne( ( localize: LocalizeFunc, - entityId, inputAboveIsEntity?: boolean, inputBelowIsEntity?: boolean ) => @@ -39,7 +38,6 @@ export default class HaNumericStateCondition extends LitElement { name: "attribute", selector: { attribute: { - entity_id: entityId, hide_attributes: [ "access_token", "auto_update", @@ -106,6 +104,9 @@ export default class HaNumericStateCondition extends LitElement { ], }, }, + context: { + filter_entity: "entity_id", + }, }, { name: "mode_above", @@ -212,7 +213,6 @@ export default class HaNumericStateCondition extends LitElement { const schema = this._schema( this.hass.localize, - this.condition.entity_id, inputAboveIsEntity, inputBelowIsEntity ); diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-state.ts b/src/panels/config/automation/condition/types/ha-automation-condition-state.ts index 1be65ec571..71b945562f 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-state.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-state.ts @@ -1,6 +1,5 @@ import { html, LitElement, PropertyValues } from "lit"; import { customElement, property } from "lit/decorators"; -import memoizeOne from "memoize-one"; import { assert, boolean, @@ -29,6 +28,54 @@ const stateConditionStruct = object({ 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") export class HaStateCondition extends LitElement implements ConditionElement { @property({ attribute: false }) public hass!: HomeAssistant; @@ -41,51 +88,6 @@ export class HaStateCondition extends LitElement implements ConditionElement { 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) { if (changedProperties.has("condition")) { try { @@ -101,16 +103,12 @@ export class HaStateCondition extends LitElement implements ConditionElement { protected render() { const trgFor = createDurationData(this.condition.for); const data = { ...this.condition, for: trgFor }; - const schema = this._schema( - this.condition.entity_id, - this.condition.attribute - ); return html` > + schema: SchemaUnion ): string => { switch (schema.name) { case "entity_id": diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-numeric_state.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-numeric_state.ts index ef4e74d07e..3d54f15cca 100644 --- a/src/panels/config/automation/trigger/types/ha-automation-trigger-numeric_state.ts +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-numeric_state.ts @@ -25,7 +25,6 @@ export class HaNumericStateTrigger extends LitElement { private _schema = memoizeOne( ( localize: LocalizeFunc, - entityId, inputAboveIsEntity?: boolean, inputBelowIsEntity?: boolean ) => @@ -35,7 +34,6 @@ export class HaNumericStateTrigger extends LitElement { name: "attribute", selector: { attribute: { - entity_id: entityId, hide_attributes: [ "access_token", "auto_update", @@ -102,6 +100,9 @@ export class HaNumericStateTrigger extends LitElement { ], }, }, + context: { + filter_entity: "entity_id", + }, }, { name: "mode_above", @@ -231,7 +232,6 @@ export class HaNumericStateTrigger extends LitElement { const schema = this._schema( this.hass.localize, - this.trigger.entity_id, inputAboveIsEntity, inputBelowIsEntity ); diff --git a/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts index 9ed5b09774..264637c16a 100644 --- a/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts @@ -67,7 +67,7 @@ export class HuiWeatherForecastCardEditor } private _schema = memoizeOne( - (entity: string, localize: LocalizeFunc, hasForecast?: boolean) => + (localize: LocalizeFunc, hasForecast?: boolean) => [ { name: "entity", @@ -81,7 +81,8 @@ export class HuiWeatherForecastCardEditor schema: [ { name: "secondary_info_attribute", - selector: { attribute: { entity_id: entity } }, + selector: { attribute: {} }, + context: { filter_entity: "entity" }, }, { name: "theme", selector: { theme: {} } }, ], @@ -125,11 +126,7 @@ export class HuiWeatherForecastCardEditor return html``; } - const schema = this._schema( - this._config.entity, - this.hass.localize, - this._has_forecast - ); + const schema = this._schema(this.hass.localize, this._has_forecast); const data: WeatherForecastCardConfig = { show_current: true,