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(
(
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
);

View File

@ -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`
<ha-form
.hass=${this.hass}
.data=${data}
.schema=${schema}
.schema=${SCHEMA}
.disabled=${this.disabled}
@value-changed=${this._valueChanged}
.computeLabel=${this._computeLabelCallback}
@ -138,7 +136,7 @@ export class HaStateCondition extends LitElement implements ConditionElement {
}
private _computeLabelCallback = (
schema: SchemaUnion<ReturnType<typeof this._schema>>
schema: SchemaUnion<typeof SCHEMA>
): string => {
switch (schema.name) {
case "entity_id":

View File

@ -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
);

View File

@ -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,