mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-14 12:56:37 +00:00
Add ha-form context (#12062)
This commit is contained in:
parent
5d3d15072f
commit
d5010dda9e
@ -139,6 +139,7 @@ const SCHEMAS: {
|
|||||||
{
|
{
|
||||||
name: "Attribute",
|
name: "Attribute",
|
||||||
selector: { attribute: { entity_id: "" } },
|
selector: { attribute: { entity_id: "" } },
|
||||||
|
context: { filter_entity: "entity" },
|
||||||
},
|
},
|
||||||
{ name: "Device", selector: { device: {} } },
|
{ name: "Device", selector: { device: {} } },
|
||||||
{ name: "Duration", selector: { duration: {} } },
|
{ name: "Duration", selector: { duration: {} } },
|
||||||
|
@ -106,6 +106,7 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
.disabled=${this.disabled}
|
.disabled=${this.disabled}
|
||||||
.helper=${this._computeHelper(item)}
|
.helper=${this._computeHelper(item)}
|
||||||
.required=${item.required || false}
|
.required=${item.required || false}
|
||||||
|
.context=${this._generateContext(item)}
|
||||||
></ha-selector>`
|
></ha-selector>`
|
||||||
: dynamicElement(`ha-form-${item.type}`, {
|
: dynamicElement(`ha-form-${item.type}`, {
|
||||||
schema: item,
|
schema: item,
|
||||||
@ -115,6 +116,7 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
hass: this.hass,
|
hass: this.hass,
|
||||||
computeLabel: this.computeLabel,
|
computeLabel: this.computeLabel,
|
||||||
computeHelper: this.computeHelper,
|
computeHelper: this.computeHelper,
|
||||||
|
context: this._generateContext(item),
|
||||||
})}
|
})}
|
||||||
`;
|
`;
|
||||||
})}
|
})}
|
||||||
@ -122,6 +124,20 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _generateContext(
|
||||||
|
schema: HaFormSchema
|
||||||
|
): Record<string, any> | undefined {
|
||||||
|
if (!schema.context) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const context = {};
|
||||||
|
for (const [context_key, data_key] of Object.entries(schema.context)) {
|
||||||
|
context[context_key] = this.data[data_key];
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
protected createRenderRoot() {
|
protected createRenderRoot() {
|
||||||
const root = super.createRenderRoot();
|
const root = super.createRenderRoot();
|
||||||
// attach it as soon as possible to make sure we fetch all events.
|
// attach it as soon as possible to make sure we fetch all events.
|
||||||
|
@ -24,6 +24,7 @@ export interface HaFormBaseSchema {
|
|||||||
// This value will be set initially when form is loaded
|
// This value will be set initially when form is loaded
|
||||||
suggested_value?: HaFormData;
|
suggested_value?: HaFormData;
|
||||||
};
|
};
|
||||||
|
context?: Record<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HaFormGridSchema extends HaFormBaseSchema {
|
export interface HaFormGridSchema extends HaFormBaseSchema {
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import "../entity/ha-entity-attribute-picker";
|
import "../entity/ha-entity-attribute-picker";
|
||||||
import { html, LitElement } from "lit";
|
import { html, LitElement, PropertyValues } from "lit";
|
||||||
import { customElement, property } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import { AttributeSelector } from "../../data/selector";
|
import { AttributeSelector } from "../../data/selector";
|
||||||
import { SubscribeMixin } from "../../mixins/subscribe-mixin";
|
import { SubscribeMixin } from "../../mixins/subscribe-mixin";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
|
|
||||||
@customElement("ha-selector-attribute")
|
@customElement("ha-selector-attribute")
|
||||||
export class HaSelectorAttribute extends SubscribeMixin(LitElement) {
|
export class HaSelectorAttribute extends SubscribeMixin(LitElement) {
|
||||||
@ -17,11 +18,16 @@ export class HaSelectorAttribute extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public disabled = false;
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
|
@property() public context?: {
|
||||||
|
filter_entity?: string;
|
||||||
|
};
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
return html`
|
return html`
|
||||||
<ha-entity-attribute-picker
|
<ha-entity-attribute-picker
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.entityId=${this.selector.attribute.entity_id}
|
.entityId=${this.selector.attribute.entity_id ||
|
||||||
|
this.context?.filter_entity}
|
||||||
.value=${this.value}
|
.value=${this.value}
|
||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
.disabled=${this.disabled}
|
.disabled=${this.disabled}
|
||||||
@ -29,6 +35,47 @@ export class HaSelectorAttribute extends SubscribeMixin(LitElement) {
|
|||||||
></ha-entity-attribute-picker>
|
></ha-entity-attribute-picker>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected updated(changedProps: PropertyValues): void {
|
||||||
|
super.updated(changedProps);
|
||||||
|
if (
|
||||||
|
// No need to filter value if no value
|
||||||
|
!this.value ||
|
||||||
|
// Only adjust value if we used the context
|
||||||
|
this.selector.attribute.entity_id ||
|
||||||
|
// Only check if context has changed
|
||||||
|
!changedProps.has("context")
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const oldContext = changedProps.get("context") as this["context"];
|
||||||
|
|
||||||
|
if (
|
||||||
|
!this.context ||
|
||||||
|
oldContext?.filter_entity === this.context.filter_entity
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate that that the attribute is still valid for this entity, else unselect.
|
||||||
|
let invalid = false;
|
||||||
|
if (this.context.filter_entity) {
|
||||||
|
const stateObj = this.hass.states[this.context.filter_entity];
|
||||||
|
|
||||||
|
if (!(stateObj && this.value in stateObj.attributes)) {
|
||||||
|
invalid = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
invalid = this.value !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (invalid) {
|
||||||
|
fireEvent(this, "value-changed", {
|
||||||
|
value: undefined,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
|
@ -42,6 +42,8 @@ export class HaSelector extends LitElement {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public required = true;
|
@property({ type: Boolean }) public required = true;
|
||||||
|
|
||||||
|
@property() public context?: Record<string, any>;
|
||||||
|
|
||||||
public focus() {
|
public focus() {
|
||||||
this.shadowRoot?.getElementById("selector")?.focus();
|
this.shadowRoot?.getElementById("selector")?.focus();
|
||||||
}
|
}
|
||||||
@ -61,6 +63,7 @@ export class HaSelector extends LitElement {
|
|||||||
disabled: this.disabled,
|
disabled: this.disabled,
|
||||||
required: this.required,
|
required: this.required,
|
||||||
helper: this.helper,
|
helper: this.helper,
|
||||||
|
context: this.context,
|
||||||
id: "selector",
|
id: "selector",
|
||||||
})}
|
})}
|
||||||
`;
|
`;
|
||||||
|
@ -31,7 +31,7 @@ export interface EntitySelector {
|
|||||||
|
|
||||||
export interface AttributeSelector {
|
export interface AttributeSelector {
|
||||||
attribute: {
|
attribute: {
|
||||||
entity_id: string;
|
entity_id?: string;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user